Constructor
new ve.EventSequencer()
#
EventSequencer class with on-event and after-event listeners.
After-event listeners are fired as soon as possible after the corresponding native event. They are similar to the setTimeout(f, 0) idiom, except that they are guaranteed to execute before any subsequent on-event listener. Therefore, events are executed in the 'right order'.
This matters when many events are added to the task queue in one go. For instance, browsers often queue 'keydown' and 'keypress' in immediate sequence, so a setTimeout(f, 0) defined in the keydown listener will run after the keypress listener (i.e. in the 'wrong' order). EventSequencer ensures that this does not happen.
All these listeners receive the jQuery event as an argument. If an on-event listener needs to pass information to a corresponding after-event listener, it can do so by adding properties into the jQuery event itself.
There are also 'onLoop' and 'afterLoop' listeners, which only fire once per Javascript event loop iteration, respectively before and after all the other listeners fire.
There is special handling for sequences (keydown,keypress), where the keypress handler is called before the native keydown action happens. In this case, after-keydown handlers fire after on-keypress handlers.
For further event loop / task queue information, see: http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#event-loops
- Source:
Properties
afterListenersForEvent
#
Properties:
- Source:
afterLoopListeners
#
afterLoopOneListeners
#
afterLoopTimeoutId
#
afterOneListenersForEvent
#
Properties:
- Source:
doneOnLoop
#
onListenersForEvent
#
Properties:
- Source:
onLoopListeners
#
pendingCalls
#
Methods
after(listeners) → {ve.EventSequencer}chainable
#
Add listeners to be fired as soon as possible after the native action
Parameters:
Name | Type | Description |
---|---|---|
listeners |
Object.<string, function()> | Function for each event |
- Source:
Returns:
- Type
- ve.EventSequencer
afterEvent(eventName, ev)private
#
Generic after listener method which gets queued
Parameters:
Name | Type | Description |
---|---|---|
eventName |
string | Javascript name of the event, e.g. 'keydown' |
ev |
jQuery.Event | The browser event |
- Source:
afterLoop(listeners) → {ve.EventSequencer}chainable
#
Add listeners to be fired at the end of the Javascript event loop iteration
Parameters:
Name | Type | Description |
---|---|---|
listeners |
function | Array.<function()> | Listener(s) that take no arguments |
- Source:
Returns:
- Type
- ve.EventSequencer
afterLoopOne(listeners) → {ve.EventSequencer}chainable
#
Add listeners to be fired once, at the end of the Javascript event loop iteration
Parameters:
Name | Type | Description |
---|---|---|
listeners |
function | Array.<function()> | Listener(s) that take no arguments |
- Source:
Returns:
- Type
- ve.EventSequencer
afterOne(listeners) → {ve.EventSequencer}chainable
#
Add listeners to be fired once, as soon as possible after the native action
Parameters:
- Source:
Returns:
- Type
- ve.EventSequencer
attach($node) → {ve.EventSequencer}chainable
#
Attach to a node, to listen to its jQuery events
Parameters:
Name | Type | Description |
---|---|---|
$node |
jQuery | The node to attach to |
- Source:
Returns:
- Type
- ve.EventSequencer
callListener(timing, eventName, i, listener, ev)
#
Single method to perform all listener calls, for ease of debugging
Parameters:
Name | Type | Description |
---|---|---|
timing |
string | on|after|afterOne|onLoop|afterLoop|afterLoopOne |
eventName |
string | Name of the event |
i |
number | The sequence of the listener |
listener |
function | The listener to call |
ev |
jQuery.Event | The browser event |
- Source:
cancelPostponed(timeoutId)
#
Cancel a postponed call.
This is a separate function because that makes it easier to replace when testing
Parameters:
Name | Type | Description |
---|---|---|
timeoutId |
number | Unique postponed timeout id |
- Source:
detach() → {ve.EventSequencer}chainable
#
Detach from a node (if attached), to stop listen to its jQuery events
- Source:
Returns:
- Type
- ve.EventSequencer
doAfterLoop(myTimeoutId)private
#
Call each afterLoopListener once, unless the setTimeout is already cancelled
Parameters:
Name | Type | Description |
---|---|---|
myTimeoutId |
number | The calling setTimeout id |
- Source:
doOnLoop()private
#
Call each onLoopListener once
- Source:
on(listeners) → {ve.EventSequencer}chainable
#
Add listeners to be fired just before the browser native action
Parameters:
Name | Type | Description |
---|---|---|
listeners |
Object.<string, function()> | Function for each event |
- Source:
Returns:
- Type
- ve.EventSequencer
onEvent(eventName, ev)private
#
Generic listener method which does the sequencing
Parameters:
Name | Type | Description |
---|---|---|
eventName |
string | Javascript name of the event, e.g. 'keydown' |
ev |
jQuery.Event | The browser event |
- Source:
onLoop(listeners) → {ve.EventSequencer}chainable
#
Add listeners to be fired at the start of the Javascript event loop iteration
Parameters:
Name | Type | Description |
---|---|---|
listeners |
function | Array.<function()> | Listener(s) that take no arguments |
- Source:
Returns:
- Type
- ve.EventSequencer
postpone(callback) → {number}
#
Make a postponed call.
This is a separate function because that makes it easier to replace when testing
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | The function to call |
- Source:
Returns:
Unique postponed timeout id
- Type
- number
resetAfterLoopTimeout()private
#
Push any pending doAfterLoop to end of task queue (cancel, then re-set)
- Source:
runPendingCalls(eventName)private
#
Run any pending listeners, and clear the pending queue
Parameters:
Name | Type | Description |
---|---|---|
eventName |
string | The name of the event currently being triggered |
- Source:
makeEventHandler(name) → {function}privateinner
#
Generate an event handler for a specific event
Parameters:
Name | Type | Description |
---|---|---|
name |
string | The event's name |
- Source:
Returns:
An event handler
- Type
- function