davidpp / event_engine_js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

event_engine_js

ExpEval

A simple expression evaluator.
  • Supported operations are: || && == != <= < >= > + - * / % ^
  • The use of paranthesis are also supported
  • Variable names start with an under-score (e.g _thisIsAVariable)
  • Everything else is considered a constant
  • White spaces are allowed between symbols
``` var expression ="_a + 2 == _b && 3 - 2 == _c"; var test_eval = new ExpEval(expression, { onNeedValue : function(valueName) { switch(valueName) { case "a" : return 1; case "b" : return 3; case "c" : return 1; } } }); test_eval.evaluate(); // will return true ```

MessageTracker

Emitter / Listener based message bus.

An emiter registers itself to the bus by calling the setEmitter method with a specific code (message identifier) that it emits.

A listener registers for a specific code by calling the addListener method, and it can stop listening for that message by calling the removeListener method.

When a listener registers for a message, the MessageTracker finds the emitter for that message and calls it's addListener method with the arguments the actual listener passed in. So it acts as the middle man for the addListener calls not the actual messages.

Emitters have to implement the following functions:

function addListener(listener, options)
function removeListener(listener, options)

Listeners have to implement the specific callback functions that are defined by the emitter of the message they subscribe to.

Example

//Create a message tracker
var message_tracker = new MessageTracker();

//Set the emitter for the message "my_message"
message_tracker.setEmitter("my_message", {
    
    addListener : function(listener, options) {
        add listener to the local listeners list
    },

    removeListener : function(listener, options) {
        remove listener from the local listeners list
    },
    
    someFunctionToCallTheEvent : function() {
        for(var key in this.__listeners)
            this.__listeners[key].onMyMessage("what's up?");
    }
});

//Add a listener for the "my_message" message
message_tracker.addListener("my_message", {
    onMyMessage : function(some_var) {
        // message received, do something with it...
    }
});

ValueTracker

Keeps track of the values that are given to it and calls the subscribed listeners when a value changes.

Emits

onValueChanged: ```onValueChanged(code, value)```

TriggerTracker

Triggers are a entities that turn on and off based on the conditions that are defined for them. When their state changes, they emit messages.

Emits

onTriggerFired: ```onTriggerFired(code)```
onTriggerReleased: ```onTriggerReleased(code)```

Example Trigger Definition

"SPEED_OVER_DRIVE_LIMIT" : {
    "options" : {
        "conditions" : "_speed>30"
    }
}

SPEED_OVER_DRIVE_LIMIT: This is the name of the trigger. Other entities that'll use this trigger will reference it by this name.
options: Contains the configuration options for this trigger.
conditions: This expression defines the condition that turns this trigger "ON". When these conditions are not met the trigger will be "OFF". When the trigger switches from "ON->OFF" it sends an "onTriggerRelease" event. When the trigger switches from "OFF->ON" it sends an "onTriggerFire" event.

EventTracker

Events are entities that turn on and off based on the triggers they are listening to and the time constraints that they are setup with. An event switches on when it's raise_trigger switches on and stays that way for raise_delay milliseconds. It switches off when it's reset_trigger switches on and stays that way for resey_delay milliseconds. Event will emit an "onEventRaised" message when it switches on, it will emit an "onEventReset" when it switches off. If the "repeat_interval" is greater than 0 then based on its value, an additional "onEventRepeat" message will be emitted periodically.

Emits

onEventReseted: ```onEventReseted(code)```
onEventRaised: ```onEventRaised(code)```
onEventRepeated: ```onEventRepeated(code)```

Example Event Definition

"DRIVE" : {
  "options" : {
    "raise_trigger" : "SPEED_OVER_DRIVE_LIMIT",
    "raise_delay" : 5000,

    "reset_trigger" : "SPEED_BELOW_STOP_LIMIT",
    "reset_delay" : 10000,

    "repeat_interval" : 5000
  }
}

DRIVE: Name of the event. This will be used to refer to this event.
options: Contains the configuration options
raise_trigger: trigger that will shitch this event on
raise_delay: raise_trigger has to stay on for this many milliseconds before this event switches on
reset_trigger: trigger that will shitch this event off
reset_delay: reset_trigger has to stay on for this many milliseconds before this event switches off
repeat_interval: An additional message will be emited every repeat_interval milliseconds if this is greater than 0

TransactionManager

Transactions capture information and emmit an onTransactionFinalized message when they are invoked.
A transaction can be invoked automatically buy binding it to an event or it can be invoked manually by calling the invoke method of the TransactionManager.
If the transaction is set to manual but still is bound to an event than the event is used to enable or disable the transaction

Emits

onTransactionFinalized: ```onTransactionFinalized(code, captured_data)```
Emitted when the transaction is invoked and all the data it needs to capture is captured.
onTransactionEnabled: ```onTransactionEnabled(code)```
Emitted when the bound event condition is satisfied if the transaction is manual.
onTransactionDisabled: ```onTransactionDisabled(code)```
Emitted when the bound event condition is not satisfied if the transaction is manual.
onTransactionInputRequired: ```onTransactionInputRequired(transaction_instance, list_of_required_values)```
Emitted when the transaction needs to capture additional data that can't be obtained from the ValueTracker (i.e. user inputs)

Example Transact Definition

"DRIVE_START" : {
    "options" : {
        "automatic" : true,
        
        "listen_to" : {
            "event" : "DRIVE",
            "stage" : "RAISE"
        },
        
        "capture" : {
            "system" : ["speed", "odometer"],
            "user" : ["user_name", "password"]
        }
    }
}

DRIVE_START: Name of the transaction
options: Contains the configuration options for the transaction
automatic: Declares the transaction as eigther automatic or manual
listen_to: The event binding options
event: Name of the event to invoke this transaction (or enable it if the transaction is manual)
stage: Stage (raise, reset, repeat) of the event to invoke this transaction (or enable it if the transaction is manual. only raise and reset are relevant for manual events)
capture: Data capture configuration
system: Data to be captured from the system (a.k.a. ValueTracker)
user: Data to be captured from the user

ActivityTracker

Activities are stateful entities that have start and end operations that are triggered automatically by binding them to transactions. There can be multiple activities going on at the same time. these can be different activities or multiple instances of the same activity.

Emits

onActivityStarted: ```onActivityStarted(code, end_transact, activity_instance)```
When an activity starts
code: the activity name
end_transaction: the name of the transaction that will end this activity
activity_instance: the activity instance that has a guid and holds the data that was captured
onActivityEnded: ```onActivityEnded(code, activity_instance)```
When activity ends
code: the activity name
activity_instance: the activity instance that has a guid and holds the data that was captured
onActivityPick: ```onActivityPick(code, activity_instance, data)```
When there is multiple instances of an activity available when the end transaction is invoked. The listener of this message has to tell the system which activity is the one that needs to be ended. The listener does this by returning true when the activity_instance is the one it wants to end
code: the activity name
activity_instance: the activity instance that has a guid and holds the data that was captured
data: the data that was captured by the end transaction

Example Activity Definition

"LOADING" : {
    "options" : {
        "start_transact" : "LOAD_START",
        "end_transact" : "LOAD_END"
    }
}

LOADING: Name of this activity
options: Contains the configuration options
start_transact: The transaction that'll cause this activity to start
end_transact: The transaction that'll cause this activity to end

About