-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.js
162 lines (147 loc) · 5.09 KB
/
callbacks.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD
define(['underscore'], factory);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = factory(require('underscore'));
} else {
// Browser globals (root is window)
root.Callbacks = factory(_);
}
}(this, function (_) {
'use strict';
/**
* @class Callbacks
* @constructor
*/
var Callbacks = function() {
this._subscriptions = [];
this._bufferEntries = 0;
this._bufferArguments = null;
};
Callbacks.prototype = {
/** @type Array */
_subscriptions: null,
/**
* Add callback.
*
* @param {Function} handler
* @param {Object} [scope]
* @returns {Callbacks}
*/
add: function(handler, scope) {
var index = this._findHandlerIndex(handler, scope);
if (index < 0) {
this._subscriptions.push({
handler: handler,
scope: scope
});
}
return this;
},
once: function(handler, scope) {
var index = this._findHandlerIndex(handler, scope);
if (index < 0) {
this._subscriptions.push({
handler: function(handler, scope, data) {
handler.call(scope, data);
this._removeHandler(handler, scope);
}.bind(this, handler, scope),
originalHandler: handler,
scope: scope
});
}
return this;
},
/**
* Fire (trigger) event.
*
* @param {Object} [args...]
* @returns {Callbacks}
*/
fire: function(args) {
if (this._subscriptions.length) {
args = _.toArray(arguments);
if (this._bufferEntries > 0) {
this._bufferArguments = args;
return this;
}
// clone subscriptions because `once` handlers may modify this._subscriptions during the loop
var subscriptions = this._subscriptions.slice();
for (var i = 0, len = subscriptions.length; i < len; i++) {
var subscription = subscriptions[i];
subscription.handler.apply(subscription.scope, args);
}
}
return this;
},
/**
* Unsubscribe. Remove handler or entire scope.
* - callbacks.remove(handler, scope) - remove handler with exactly that scope
* - callbacks.remove(handler) - remove handler with undefined scope
* - callbacks.remove(scope) - remove all handlers with that scope
*
* @param {Function} [handler]
* @param {Object} [scope]
* @returns {Callbacks}
*/
remove: function(handler, scope) {
if (_.isFunction(handler)) {
this._removeHandler(handler, scope);
} else {
scope = handler;
this._removeScope(scope);
}
return this;
},
removeAll: function() {
this._subscriptions = [];
return this;
},
/**
* Perform operation `f` which calls `fire()` multiple times, and then call `fire()` only once.
* Sort of `debounce`.
*
* @param {Function} f
* @param {Object} [scope]
* @returns {*}
*/
buffer: function(f, scope) {
this._bufferEntries++;
try {
return f.call(scope);
} finally {
this._bufferEntries--;
if (this._bufferEntries === 0 && this._bufferArguments !== null) {
this.fire.apply(this, this._bufferArguments);
this._bufferArguments = null;
}
}
},
_findHandlerIndex: function(handler, scope) {
return _.findIndex(this._subscriptions, function(subscription) {
return subscription.scope === scope &&
(subscription.handler === handler || subscription.originalHandler === handler);
});
},
_removeHandler: function(handler, scope) {
var index = this._findHandlerIndex(handler, scope);
if (index >= 0) {
this._subscriptions.splice(index, 1);
}
},
_removeScope: function(scope) {
this._subscriptions = _.reject(this._subscriptions, function(subscription) {
return subscription.scope === scope;
});
}
};
Callbacks.prototype.empty = Callbacks.prototype.removeAll;
Callbacks.prototype.on = Callbacks.prototype.add;
Callbacks.prototype.one = Callbacks.prototype.once;
Callbacks.prototype.off = Callbacks.prototype.remove;
Callbacks.prototype.trigger = Callbacks.prototype.fire;
return Callbacks;
}));