mirror of
https://github.com/flynx/guaranteeEvents.git
synced 2025-10-29 02:50:11 +00:00
initial code moved from ImageGrid...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
07ae64c139
commit
7e95066be6
86
index.js
Normal file
86
index.js
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
/**********************************************************************
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
**********************************************************************/
|
||||||
|
|
||||||
|
// Guarantee that every event handler gets every event...
|
||||||
|
//
|
||||||
|
// guaranteeEvents('event', emitter)
|
||||||
|
// guaranteeEvents('eventA eventB ...', emitter)
|
||||||
|
// guaranteeEvents(['eventA', 'eventB', ...], emitter)
|
||||||
|
// -> emitter
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// This will add a .clearGuarantedQueue(..) method to the emitter that
|
||||||
|
// will clear the event queue for a specific event.
|
||||||
|
//
|
||||||
|
// Clear event(s) queue(s)...
|
||||||
|
// emitter.clearGuarantedQueue('event')
|
||||||
|
// emitter.clearGuarantedQueue('eventA eventB ...')
|
||||||
|
// emitter.clearGuarantedQueue(['eventA', 'eventB', ...])
|
||||||
|
// -> emitter
|
||||||
|
//
|
||||||
|
// Clear all queues...
|
||||||
|
// emitter.clearGuarantedQueue('*')
|
||||||
|
// -> emitter
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// NOTE: the seen stack might get quite big, this is not recommended for
|
||||||
|
// long running emitters...
|
||||||
|
//
|
||||||
|
var guaranteeEvents =
|
||||||
|
module.exports =
|
||||||
|
function(names, emitter){
|
||||||
|
names = typeof(names) == typeof('str') ? names.split(/\s+/g) : names
|
||||||
|
|
||||||
|
// add ability to clear the queue...
|
||||||
|
if(emitter.clearGuarantedQueue == null){
|
||||||
|
emitter.clearGuarantedQueue = function(names){
|
||||||
|
names = names == '*' ? Object.keys(this._guaranteed_queue)
|
||||||
|
: typeof(names) == typeof('str') ? names.split(/\s+/g)
|
||||||
|
: names
|
||||||
|
|
||||||
|
var that = this
|
||||||
|
names.forEach(function(name){
|
||||||
|
if(name in that._guaranteed_queue){
|
||||||
|
that._guaranteed_queue[name] = []
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
emitter._guaranteed_queue = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
names.forEach(function(name){
|
||||||
|
// do not register twice...
|
||||||
|
if(emitter._guaranteed_queue[name] != null){
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var seen = emitter._guaranteed_queue[name] = []
|
||||||
|
|
||||||
|
emitter
|
||||||
|
// remember each event emitted...
|
||||||
|
.on(name, function(){
|
||||||
|
seen.push([].slice.apply(arguments))
|
||||||
|
})
|
||||||
|
// call the handler with the it events missed...
|
||||||
|
.on('newListener', function(evt, func){
|
||||||
|
if(evt == name && seen.length > 0){
|
||||||
|
var that = this
|
||||||
|
seen.forEach(function(args){
|
||||||
|
func.apply(that, args)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return emitter
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**********************************************************************
|
||||||
|
* vim:set ts=4 sw=4 : */
|
||||||
Loading…
x
Reference in New Issue
Block a user