-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignal.js
53 lines (44 loc) · 1.2 KB
/
Signal.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
48
49
50
51
52
53
var MM = MM || {};
MM.Signal = (function() {
function Signal() {
this.bindings = [];
}
Signal.prototype.add = function(callback, scope, addOnce) {
if(callback == null || typeof callback != "function") {
throw new Error("Invalid callback");
return;
}
addOnce = addOnce || false;
this.bindings.unshift({callback: callback, scope: scope, addOnce: addOnce});
}
Signal.prototype.addOnce = function(callback, scope) {
this.add(callback, scope, true);
}
Signal.prototype.dispatch = function() {
var args = Array.prototype.slice.call(arguments), i = this.bindings.length - 1, binding;
for(; i >= 0; i--) {
binding = this.bindings[i];
binding.callback.apply(binding.scope, args);
if(binding.addOnce) {
this.bindings.splice(i, 1);
}
}
}
Signal.prototype.remove = function(callback, scope) {
var i = this.bindings.length - 1, binding;
for(; i >= 0; i--) {
binding = this.bindings[i];
if(binding.callback == callback && binding.scope == scope) {
this.bindings.splice(i, 1);
}
}
}
Signal.prototype.removeAll = function() {
this.bindings = [];
}
return Signal;
})();
// browserify & webpack
if(typeof module === "object") {
module.exports = MM.Signal;
}