new TriggersManager()
Triggers manager
At any moment of the javascript tag, library triggers can occur. It's possible to subscribe to it with the methods 'on' (see below) by giving them the trigger's name in question and the function to be called. The trigger name got a simple syntax : strings separated by a colon (':'). This sequence of trigger parts can be considered as a chaptering from the broader to the most detailed.
It allows a granular handling of the triggers: 'hit:send:ok', 'hit:send:error' and 'hit:send:warning' are all contained in the main event 'hit:*'. It's then possible to subscribe to a specific trigger or to a family of trigger.
Listeners structure
To structure the listeners we use a tree
If we take as an example the addition of these triggers:
- 'a:a:a'
- 'b:a'
- 'c:a'
- 'c:a:a'
- 'c:a:b'
- 'c:a:c'
- 'c:b'
The following tree is obtained:
a -- a -- a / / root -- b -- a \ a \ / c -- a - b \ \ \ c \ b
Each node (even root) corresponds to a listener which therefore contains an array of functions to execute when passing through this node.
IMPORTANT REMINDER: If trigger 'c:a:b' is raised then all functions which are part of root, root:c, root:c:a et root:c:a:b are executed.
Finally, here is the object corresponding to the tree represented previously (the property '*' corresponds to the value of the node):
{ "*": [], "a": { "*": [], "a": { "*": [], "a": {"*": []} } }, "b": { "*": [], "a": {"*": []} }, "c": { "*": [], "a": { "*": [], "a": {"*": []}, "b": {"*": []}, "c": {"*": []} }, "b": {"*": []} } }
Methods
-
emit(trigger, data)
-
Throw a trigger.
Name Type Description trigger
string Trigger that you want to emit data
object Data you want to transmit to listeners -
on(trigger, callback, singleUse)
-
Subscribe to a trigger.
Name Type Description trigger
string Trigger you want to subscribe callback
function Method that you want to be triggered singleUse
boolean if the callback method must be called once (the first time)