Ejscript has an efficient eventing framework to support asynchronous programming. It consists of event dispatchers, listening objects, event objects and timers.

The Ejscript event service allows event queues to be created and for interested parties to listen for events of interest. Events objects can then be sent to the event dispatcher and relayed onto subscribing listeners. This implements the classic Publish and Subscribe pattern.

Event Emitter

An event emitter is responsible for managing an event queue and for dispatching events to all registered listening objects. Event emitters are created by instantiating the Emitter class. Applications can create any number of event dispatchers.

var events = new Emitter

Once created, the Emitter instance can be used by interested parties who wish to "listen" for events. They do this by invoking the "o" method and supplying a callback function that will be invoked to receive the event.

events.on(function (e: Event) {
    print("Got Event:", e)

Dispatching Events

An event is dispatched to listening parties by calling the fire method of the Emitter object.

events.fire("weather", "Sunny")

When fireis called, the event information is passed to all listening parties who will have their registered callback function invoked with the supplied event information.

Scheduling Timers

Ejscript also supports timer events which can be scheduled to run after a given delay.

Timer(10 * 1000, function (args) {

A timer event will not be run unless the application is waiting in either App.serviceEvents, App.sleep or otherwise waiting in the Ejscript main event loop.

© Embedthis Software. All rights reserved.