eventmonitor is a simple JavaScript module for managing dependencies asynchronously by allowing functions to be triggered once their defined prereqs have been satisified.
This makes it easy to build non-blocking code without complex callback nesting, and also to tie a function's execution to multiple, independent preconditions.
To make something trackable, simply announce an event:
EVENT_MONITOR.announce('MY_EVENT');
Then create connectors to tie a given follower function's execution to one or more prereqs:
EVENT_MONITOR.connect(['MY_EVENT'], my_follower_func);
This is somewhat similar to a pub/sub model. However, completed events are reevaluated each time either an event is announced or a connector created, meaning that connectors can be instantiated even after their prereqs have occurred and will still trigger their followers.
-
Define your functions, announcing significant events.
function a () { console.log('a'); EVENT_MONITOR.announce('A'); } function b () { console.log('b'); EVENT_MONITOR.announce('B'); } function c () { console.log('c'); EVENT_MONITOR.announce('C'); } -
Connect them to prerequisite events.
// Make sure C is called after A and B. EVENT_MONITOR.connect(['A', 'B'], c); -
Let the event monitor call them.
// Announce event
EVENT_MONITOR.announce(event, {data_map});
// Create connector
EVENT_MONITOR.connect([prereq_arr], follower_fn, [args_arr], label);
// Directly provide data to event monitor
EVENT_MONITOR.provide(key, value);
// Enable logging
EVENT_MONITOR.debug = true;
-
Any arguments in the
args_arrparameter will be passed to the follower function; this parameter may be omitted if the function does not take arguments. -
Although data may be provided to via the
providefunction, it's more useful to send thedata_mapwhen announcing an event. When a connector's follower function is executed, event monitor will substitute items in the connector'sargs_arrwith the latest data which has been provided to it, if available. -
Connector names are completely optional but may be useful for tracing a certain sequence of events.