Non Blocking Channel Operations in AngelScript

void main()
{
    array<string> messages;
    array<bool> signals;

    // Here's a non-blocking receive. If a value is
    // available in `messages` then we'll process it.
    // If not, we'll immediately take the default case.
    if (!messages.isEmpty())
    {
        string msg = messages[messages.length() - 1];
        messages.removeAt(messages.length() - 1);
        print("received message " + msg);
    }
    else
    {
        print("no message received");
    }

    // A non-blocking send works similarly. Here `msg`
    // cannot be sent to the `messages` array, because
    // there's no receiver. Therefore, the default case is taken.
    string msg = "hi";
    if (false) // This condition simulates a non-blocking send
    {
        messages.insertLast(msg);
        print("sent message " + msg);
    }
    else
    {
        print("no message sent");
    }

    // We can use multiple conditions to implement a multi-way
    // non-blocking select. Here we attempt non-blocking receives
    // on both `messages` and `signals`.
    if (!messages.isEmpty())
    {
        string msg = messages[messages.length() - 1];
        messages.removeAt(messages.length() - 1);
        print("received message " + msg);
    }
    else if (!signals.isEmpty())
    {
        bool sig = signals[signals.length() - 1];
        signals.removeAt(signals.length() - 1);
        print("received signal " + (sig ? "true" : "false"));
    }
    else
    {
        print("no activity");
    }
}

This AngelScript code demonstrates non-blocking operations similar to the original example. However, AngelScript doesn’t have built-in channels or a select statement, so we’ve simulated these concepts using arrays and conditional statements.

Basic sends and receives on arrays are not blocking in AngelScript. We’ve implemented non-blocking “sends” and “receives” using conditional statements and array operations.

In the first block, we check if the messages array is not empty. If it’s not, we remove and process the last element. If it is empty, we print that no message was received.

The second block simulates a non-blocking send. Since AngelScript doesn’t have channels, we use a false condition to always take the “default” case.

In the last block, we implement a multi-way non-blocking select by checking both messages and signals arrays. If either has elements, we process them. If both are empty, we print that there’s no activity.

Note that this is a simplified simulation of the concepts in the original code. In a real AngelScript application, you might need to implement more sophisticated mechanisms for concurrent operations, depending on your specific requirements and the capabilities of your AngelScript environment.