Select in ActionScript

ActionScript doesn’t have built-in support for concurrent programming like Go’s goroutines and channels. However, we can simulate similar behavior using timers and events. Here’s an example that approximates the functionality of the original code:

package {
    import flash.display.Sprite;
    import flash.events.TimerEvent;
    import flash.utils.Timer;

    public class SelectExample extends Sprite {
        private var timer1:Timer;
        private var timer2:Timer;
        private var count:int = 0;

        public function SelectExample() {
            // For our example we'll use two timers to simulate channels.
            timer1 = new Timer(1000, 1);
            timer2 = new Timer(2000, 1);

            // Each timer will trigger an event after some amount
            // of time, to simulate e.g. blocking operations
            // executing concurrently.
            timer1.addEventListener(TimerEvent.TIMER_COMPLETE, onTimer1Complete);
            timer2.addEventListener(TimerEvent.TIMER_COMPLETE, onTimer2Complete);

            timer1.start();
            timer2.start();
        }

        private function onTimer1Complete(event:TimerEvent):void {
            handleMessage("one");
        }

        private function onTimer2Complete(event:TimerEvent):void {
            handleMessage("two");
        }

        private function handleMessage(msg:String):void {
            trace("received", msg);
            count++;

            if (count == 2) {
                // Both messages have been received, so we're done
                trace("All messages received");
            }
        }
    }
}

This ActionScript code creates a class SelectExample that simulates the behavior of the original Go program. Here’s how it works:

  1. We create two Timer objects to simulate the two channels in the Go code. Each timer is set to trigger once after a specific delay (1 second and 2 seconds respectively).

  2. We add event listeners to each timer that will be called when the timer completes. These event listeners simulate the behavior of receiving a value from a channel.

  3. In the event listeners (onTimer1Complete and onTimer2Complete), we call a handleMessage function that prints the received message and keeps track of how many messages have been received.

  4. The handleMessage function simulates the behavior of the select statement in the Go code. It prints each message as it’s received and checks if both messages have been received.

To run this code, you would typically embed it in a Flash project and compile it with the ActionScript compiler. When executed, it will output:

received one
received two
All messages received

Note that unlike the Go version, which uses true concurrency with goroutines, this ActionScript version uses asynchronous timers to simulate concurrent behavior. The total execution time will still be about 2 seconds, as the timers run concurrently.

This example demonstrates how to handle multiple asynchronous operations in ActionScript, which is conceptually similar to selecting from multiple channels in Go, even though the underlying mechanisms are quite different.