-
Notifications
You must be signed in to change notification settings - Fork 3
/
events.js
176 lines (156 loc) · 4.91 KB
/
events.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
import EventHandle from './event-handle.js';
/**
* @callback HandleEvent
* Callback used by {@link Events} and {@link EventHandle} functions. Note the callback is limited to 8 arguments.
* @param {*} [arg1] - First argument that is passed from caller.
* @param {*} [arg2] - Second argument that is passed from caller.
* @param {*} [arg3] - Third argument that is passed from caller.
* @param {*} [arg4] - Fourth argument that is passed from caller.
* @param {*} [arg5] - Fifth argument that is passed from caller.
* @param {*} [arg6] - Sixth argument that is passed from caller.
* @param {*} [arg7] - Seventh argument that is passed from caller.
* @param {*} [arg8] - Eighth argument that is passed from caller.
*/
/**
* Base class for event handling.
*/
class Events {
constructor() {
// _world
Object.defineProperty(
this,
'_events', {
enumerable: false,
configurable: false,
writable: true,
value: { }
}
);
this._suspendEvents = false;
this._additionalEmitters = [];
}
/**
* If true the observer will not emit events when values are set.
*
* @type {boolean}
*/
set suspendEvents(value) {
this._suspendEvents = !!value;
}
get suspendEvents() {
return this._suspendEvents;
}
/**
* @param {string} name - Name
* @param {HandleEvent} fn - Callback function
* @returns {EventHandle} EventHandle
*/
on(name, fn) {
const events = this._events[name];
if (events === undefined) {
this._events[name] = [fn];
} else {
if (events.indexOf(fn) === -1)
events.push(fn);
}
return new EventHandle(this, name, fn);
}
/**
* @param {string} name - Name
* @param {HandleEvent} fn - Callback function
* @returns {EventHandle} EventHandle
*/
once(name, fn) {
const evt = this.on(name, (arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) => {
fn.call(this, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
evt.unbind();
});
return evt;
}
/**
* @param {string} name - Name
* @param {*} [arg0] - First argument
* @param {*} [arg1] - Second argument
* @param {*} [arg2] - Third argument
* @param {*} [arg3] - Fourth argument
* @param {*} [arg4] - Fifth argument
* @param {*} [arg5] - Sixth argument
* @param {*} [arg6] - Seventh argument
* @param {*} [arg7] - Eights argument
* @returns {Events} Self for chaining.
*/
emit(name, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) {
if (this._suspendEvents) return this;
let events = this._events[name];
if (events && events.length) {
events = events.slice(0);
for (let i = 0; i < events.length; i++) {
if (!events[i])
continue;
try {
events[i].call(this, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
} catch (ex) {
console.info('%c%s %c(event error)', 'color: #06f', name, 'color: #f00');
console.log(ex.stack);
}
}
}
if (this._additionalEmitters.length) {
const emitters = this._additionalEmitters.slice();
emitters.forEach((emitter) => {
emitter.emit(name, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
});
}
return this;
}
/**
* @param {string} name - Name
* @param {HandleEvent} fn - Callback function
* @returns {Events} - This
*/
unbind(name, fn) {
if (name) {
const events = this._events[name];
if (!events)
return this;
if (fn) {
const i = events.indexOf(fn);
if (i !== -1) {
if (events.length === 1) {
delete this._events[name];
} else {
events.splice(i, 1);
}
}
} else {
delete this._events[name];
}
} else {
this._events = { };
}
return this;
}
/**
* Adds another emitter. Any events fired by this instance
* will also be fired on the additional emitter.
*
* @param {Events} emitter - The emitter
*/
addEmitter(emitter) {
if (!this._additionalEmitters.includes(emitter)) {
this._additionalEmitters.push(emitter);
}
}
/**
* Removes emitter.
*
* @param {Events} emitter - The emitter
*/
removeEmitter(emitter) {
const idx = this._additionalEmitters.indexOf(emitter);
if (idx !== -1) {
this._additionalEmitters.splice(idx, 1);
}
}
}
export default Events;