forked from jeromeetienne/microevent.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microevent-debug.js
47 lines (44 loc) · 1.4 KB
/
microevent-debug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* MicroEvent.js debug
*
* # it is the same as MicroEvent.js but it adds checks to help you debug
*/
var MicroEvent = function(){}
MicroEvent.prototype = {
bind : function(event, fct){
this._events = this._events || {};
this._events[event] = this._events[event] || [];
this._events[event].push(fct);
},
unbind : function(event, fct){
console.assert(typeof fct === 'function');
this._events = this._events || {};
if( event in this._events === false ) return;
console.assert(this._events[event].indexOf(fct) !== -1);
this._events[event].splice(this._events[event].indexOf(fct), 1);
},
trigger : function(event /* , args... */){
this._events = this._events || {};
if( event in this._events === false ) return;
for(var i = 0; i < this._events[event].length; i++){
this._events[event][i].apply(this, Array.prototype.slice.call(arguments, 1))
}
}
};
/**
* mixin will delegate all MicroEvent.js function in the destination object
*
* - require('MicroEvent').mixin(Foobar) will make Foobar able to use MicroEvent
*
* @param {Object} the object which will support MicroEvent
*/
MicroEvent.mixin = function(destObject){
var props = ['bind', 'unbind', 'trigger'];
for(var i = 0; i < props.length; i ++){
destObject.prototype[props[i]] = MicroEvent.prototype[props[i]];
}
}
// export in common js
if( typeof module !== "undefined" && ('exports' in module)){
module.exports = MicroEvent
}