|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
var PREFIX = 'fn_'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
var Callbacks = function Callbacks (namespace) { |
|
this.collection = {}; |
|
this.index = 0; |
|
this.namespace = namespace; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Callbacks.prototype.register = function register (fn) { |
|
var self = this; |
|
var id = this.index++; |
|
this.collection[id] = fn; |
|
|
|
this.namespace.once(PREFIX + id, function callbackListener (arg1, arg2, arg3) { |
|
self.exec(id, arg1, arg2, arg3); |
|
}); |
|
|
|
return id; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Callbacks.prototype.exec = function exec (id, arg1, arg2, arg3) { |
|
if (! this.collection.hasOwnProperty(id)) return; |
|
this.collection[id](arg1, arg2, arg3); |
|
delete this.collection[id]; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Callbacks.prototype.getFn = function getFn (id) { |
|
var self = this; |
|
return function callback (arg1, arg2, arg3) { |
|
self.namespace.emit(PREFIX + id, arg1, arg2, arg3); |
|
}; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
Callbacks.prototype.release = function release () { |
|
delete this.collection; |
|
delete this.index; |
|
delete this.namespace; |
|
}; |
|
|
|
|
|
module.exports = Callbacks; |
|
|