/
event.js
189 lines (163 loc) · 3.84 KB
/
event.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Keeps a list of bindings/callbacks that can be added using **push()** and
* removed using **remove()**. *trigger()* executes all callbacks one by one in registration order.
*
* @param [spec] {Object}
* @param [my] {Object}
* @returns {event}
*/
export default function event(spec, my) {
my ||= {};
let bindings = [];
// #### Public API
let that = {};
/**
* Binds callback to event. The callback will be invoked
* whenever the event is fired. Avoid adding the same callback
* twice.
*
* @param callback {function}
* @returns {eventBinding}
*/
that.register = function (callback) {
return bindCallback(callback);
};
/**
* Binds a callback to an event
*
* @param spec.callback {function} Callback to execute on event
* @param spec.event {event} Event to bind callback to
* @returns {eventBinding}
*/
// eslint-disable-next-line no-shadow -- we should fix that later
function eventBinding(spec) {
spec ||= {};
// eslint-disable-next-line no-shadow -- we should fix that later
let that = {};
let callback = spec.callback;
// eslint-disable-next-line no-shadow -- we should fix that later
let event = spec.event;
/**
* Is bound to an event
* @returns {boolean}
*/
that.isBound = function () {
return event !== undefined;
};
/**
* Remove itself from event, if bound.
*/
that.unbind = function () {
if (that.isBound()) {
event.unregister(that);
event = undefined;
}
};
/**
* @param anEvent
*/
that.bind = function (anEvent) {
that.unbind();
if (anEvent) {
event = anEvent;
}
};
/**
* Executes connected callback
* @param params
*/
that.execute = function (params) {
if (callback) {
callback.apply(that, params);
}
};
/**
* Returns true if and only if the receiver is triggering
* the callback given as parameter.
*
* @param cb {function} callback to test against
* @returns {boolean}
*/
that.isForCallback = function (cb) {
return callback === cb;
};
return that;
}
/**
* Like on() except callback will only be fired once
*
* @param callback {function}
* @returns {eventBinding}
*/
that.registerOnce = function (callback) {
let onceBinding = eventBinding({
callback: function () {
my.remove(onceBinding);
callback.apply(that, arguments);
},
});
bindings.push(onceBinding);
return onceBinding;
};
/**
* Removed "binding" attached to event.
* @param name {String} Name of event
* @param binding {eventBinding} Binding
*/
that.unregister = function (binding) {
my.remove(binding);
};
/**
* Trigger event by executing all callbacks one by one in registration order.
*
* @param arguments {Object|Object[]} Arguments passed to callback of each binding
*/
that.trigger = function () {
let params = Array.prototype.slice.call(arguments);
bindings.forEach((binding) => {
binding.execute(params);
});
};
/**
* Unbind all callbacks bound to this event.
*/
that.dispose = function () {
bindings.slice().forEach((binding) => {
binding.unbind();
});
};
/**
* @param binding {eventBinding}
*/
my.push = function (binding) {
bindings.push(binding);
binding.bind(that);
};
/**
* @param binding {eventBinding}
*/
my.remove = function (binding) {
bindings.splice(bindings.indexOf(binding), 1);
};
/**
* Create and add callback binding to the receiver. Avoid
* adding the same callback twice.
*
* @param callback
* @returns {eventBinding}
*/
function bindCallback(callback) {
// eslint-disable-next-line no-shadow -- we should fix that later
let binding = bindings.find((binding) =>
binding.isForCallback(callback),
);
// Don't register the same callback twice:
if (binding) {
return binding;
}
binding = eventBinding({ callback, event: that });
bindings.push(binding);
return binding;
}
return that;
}