1
|
-export default {
|
|
|
2
|
- listeners: [],
|
|
|
3
|
- on(type, handle) {
|
|
|
4
|
- // first remove and add
|
|
|
5
|
- this.off(type, handle);
|
|
|
6
|
-
|
|
|
7
|
- if (typeof handle === 'function') {
|
|
|
8
|
- console.debug(`listen event ${type}`);
|
|
|
9
|
- this.listeners.push([type, handle]);
|
|
|
10
|
- }
|
|
|
11
|
- },
|
|
|
12
|
- off(type, handle) {
|
|
|
13
|
- let index = -1;
|
|
|
14
|
-
|
|
|
15
|
- if (typeof handle === 'function' &&
|
|
|
16
|
- this.listeners.some(([itype, ihandle]) => {
|
|
|
17
|
- index++;
|
|
|
18
|
- return itype === type && handle.name && ihandle.name === handle.name;
|
|
|
19
|
- })
|
|
|
20
|
- ) {
|
|
|
21
|
- this.listeners.splice(index, 1);
|
|
|
22
|
- }
|
|
|
23
|
- },
|
|
|
24
|
- emit(type, ...params) {
|
|
|
25
|
- this.listeners.forEach(([listenType, handle]) => type === listenType && handle(...params));
|
|
|
26
|
- console.debug(`receive event ${type}: ${JSON.stringify(params)}`);
|
|
|
27
|
- },
|
|
|
28
|
- removeAllListeners() {
|
|
|
29
|
- this.listeners = [];
|
|
|
30
|
- }
|
|
|
31
|
-}; |
1
|
+/**
|
|
|
2
|
+ * description: 参考借鉴 Node.js 的 EventEmitter
|
|
|
3
|
+ * 对象管理 event 事件
|
|
|
4
|
+ * @func on 监听
|
|
|
5
|
+ * @func emit 触发
|
|
|
6
|
+ * @func once 监听只执行一次
|
|
|
7
|
+ * @func remove 移除某个类型的某个事件
|
|
|
8
|
+ * @func removeAll 移除某个类型的全部事件/所有类型的全部事件
|
|
|
9
|
+ */
|
|
|
10
|
+class EventEmitter {
|
|
|
11
|
+ constructor() {
|
|
|
12
|
+ this._events = Object.create(null);
|
|
|
13
|
+ this._eventsCount = 0;
|
|
|
14
|
+ }
|
|
|
15
|
+
|
|
|
16
|
+ /**
|
|
|
17
|
+ * emit 触发事件
|
|
|
18
|
+ * @param type 事件名
|
|
|
19
|
+ * @param ...args 参数
|
|
|
20
|
+ */
|
|
|
21
|
+ emit(type) {
|
|
|
22
|
+ let events, handler, len, isFn;
|
|
|
23
|
+
|
|
|
24
|
+ events = this._events;
|
|
|
25
|
+ handler = events[type];
|
|
|
26
|
+ console.log(events);
|
|
|
27
|
+
|
|
|
28
|
+ if (!handler)
|
|
|
29
|
+ return false;
|
|
|
30
|
+
|
|
|
31
|
+ isFn = typeof handler === 'function';
|
|
|
32
|
+ len = arguments.length;
|
|
|
33
|
+
|
|
|
34
|
+ // 优化性能
|
|
|
35
|
+ switch(len) {
|
|
|
36
|
+ case 0:
|
|
|
37
|
+ throw new Error('"emit" not have arguments');
|
|
|
38
|
+ break;
|
|
|
39
|
+ case 1:
|
|
|
40
|
+ emitNone(handler, isFn, this);
|
|
|
41
|
+ break;
|
|
|
42
|
+ case 2:
|
|
|
43
|
+ emitOne(handler, isFn, this, arguments[1]);
|
|
|
44
|
+ break;
|
|
|
45
|
+ case 3:
|
|
|
46
|
+ emitTwo(handler, isFn, this, arguments[1], arguments[2]);
|
|
|
47
|
+ break;
|
|
|
48
|
+ case 4:
|
|
|
49
|
+ emitThree(handler, isFn, this, arguments[1], arguments[2], arguments[3]);
|
|
|
50
|
+ break;
|
|
|
51
|
+ default:
|
|
|
52
|
+ args = new Array(len - 1);
|
|
|
53
|
+ for (let i = 1; i < len; i++) {
|
|
|
54
|
+ args[i - 1] = arguments[i];
|
|
|
55
|
+ }
|
|
|
56
|
+ emitMany(handler, isFn, this, args);
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ return true;
|
|
|
60
|
+ }
|
|
|
61
|
+
|
|
|
62
|
+ one(type, listener,) {
|
|
|
63
|
+ let events;
|
|
|
64
|
+ if (typeof listener !== 'function')
|
|
|
65
|
+ throw new Error('"listener" argument must be a function');
|
|
|
66
|
+
|
|
|
67
|
+ events = this._events;
|
|
|
68
|
+ console.log(events);
|
|
|
69
|
+ if (typeof events === 'undefined') {
|
|
|
70
|
+ events = this._events = Object.create(null);
|
|
|
71
|
+ this._eventsCount = 0;
|
|
|
72
|
+ }
|
|
|
73
|
+
|
|
|
74
|
+ events[type] = listener;
|
|
|
75
|
+ ++this._eventsCount;
|
|
|
76
|
+ return this;
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ _addListener(type, listener, prepend) {
|
|
|
80
|
+ let events;
|
|
|
81
|
+ let existing;
|
|
|
82
|
+ if (typeof listener !== 'function')
|
|
|
83
|
+ throw new Error('"listener" argument must be a function');
|
|
|
84
|
+
|
|
|
85
|
+ events = this._events;
|
|
|
86
|
+ console.log(events);
|
|
|
87
|
+
|
|
|
88
|
+ if (typeof events === 'undefined') {
|
|
|
89
|
+ events = this._events = Object.create(null);
|
|
|
90
|
+ this._eventsCount = 0;
|
|
|
91
|
+ } else {
|
|
|
92
|
+ existing = events[type];
|
|
|
93
|
+ }
|
|
|
94
|
+
|
|
|
95
|
+ if (!existing) {
|
|
|
96
|
+ existing = events[type] = listener;
|
|
|
97
|
+ ++this._eventsCount;
|
|
|
98
|
+ } else {
|
|
|
99
|
+ if (typeof existing === 'function') {
|
|
|
100
|
+ existing = events[type] = prepend ? [listener, existing] : [existing, listener];
|
|
|
101
|
+ } else if (prepend) {
|
|
|
102
|
+ existing.unshift(listener);
|
|
|
103
|
+ } else {
|
|
|
104
|
+ existing.push(listener);
|
|
|
105
|
+ }
|
|
|
106
|
+ }
|
|
|
107
|
+
|
|
|
108
|
+ return this;
|
|
|
109
|
+ }
|
|
|
110
|
+
|
|
|
111
|
+ /**
|
|
|
112
|
+ * on 监听事件
|
|
|
113
|
+ * @param type 事件名
|
|
|
114
|
+ * @param listener 监听函数
|
|
|
115
|
+ */
|
|
|
116
|
+ on(type, listener) {
|
|
|
117
|
+ return this._addListener(type, listener, false);
|
|
|
118
|
+ }
|
|
|
119
|
+
|
|
|
120
|
+ /**
|
|
|
121
|
+ * once 监听只执行一次事件
|
|
|
122
|
+ * @param type 事件名
|
|
|
123
|
+ * @param listener 只执行一次的监听函数
|
|
|
124
|
+ */
|
|
|
125
|
+ once(type, listener) {
|
|
|
126
|
+ if (typeof listener !== 'function')
|
|
|
127
|
+ throw new Error('"listener" argument must be a function');
|
|
|
128
|
+ this.on(type, _onceWrap(this, type, listener));
|
|
|
129
|
+ return this;
|
|
|
130
|
+ }
|
|
|
131
|
+
|
|
|
132
|
+ /**
|
|
|
133
|
+ * prepend 优先监听事件
|
|
|
134
|
+ * @param type 事件名
|
|
|
135
|
+ * @param listener 要优先的监听函数
|
|
|
136
|
+ */
|
|
|
137
|
+ prepend(type, listener) {
|
|
|
138
|
+ return this._addListener(type, listener, true);
|
|
|
139
|
+ }
|
|
|
140
|
+
|
|
|
141
|
+ /**
|
|
|
142
|
+ * prependOnce 优先只执行一次的监听事件
|
|
|
143
|
+ * @param type 事件名
|
|
|
144
|
+ * @param listener 要优先且只执行一次的监听函数
|
|
|
145
|
+ */
|
|
|
146
|
+ prependOnce(type, listener) {
|
|
|
147
|
+ if (typeof listener !== 'function')
|
|
|
148
|
+ throw new TypeError('"listener" argument must be a function');
|
|
|
149
|
+ this.prepend(type, _onceWrap(this, type, listener));
|
|
|
150
|
+ return this;
|
|
|
151
|
+ }
|
|
|
152
|
+
|
|
|
153
|
+ /**
|
|
|
154
|
+ * remove 移除的监听事件
|
|
|
155
|
+ * @param type 事件名
|
|
|
156
|
+ * @param listener 要移除的监听函数(可选)
|
|
|
157
|
+ */
|
|
|
158
|
+ remove(type, listener) {
|
|
|
159
|
+ let list, events, position, originalListener;
|
|
|
160
|
+
|
|
|
161
|
+ if (typeof listener !== 'function')
|
|
|
162
|
+ throw new TypeError('"listener" argument must be a function');
|
|
|
163
|
+
|
|
|
164
|
+ events = this._events;
|
|
|
165
|
+ if (!events) {
|
|
|
166
|
+ return this;
|
|
|
167
|
+ }
|
|
|
168
|
+
|
|
|
169
|
+ list = events[type];
|
|
|
170
|
+ if (!list) {
|
|
|
171
|
+ return this;
|
|
|
172
|
+ }
|
|
|
173
|
+
|
|
|
174
|
+ if (list === listener || list.listener === listener) {
|
|
|
175
|
+ if (--this._eventsCount === 0) {
|
|
|
176
|
+ this._events = Object.create(null);
|
|
|
177
|
+ } else {
|
|
|
178
|
+ delete events[type];
|
|
|
179
|
+ }
|
|
|
180
|
+ } else if (typeof list !== 'function') {
|
|
|
181
|
+ position = -1;
|
|
|
182
|
+
|
|
|
183
|
+ for (let i = list.length - 1; i >= 0; i--) {
|
|
|
184
|
+ if (list[i] === listener || list[i].listener === listener) {
|
|
|
185
|
+ originalListener = list[i].listener;
|
|
|
186
|
+ position = i;
|
|
|
187
|
+ break;
|
|
|
188
|
+ }
|
|
|
189
|
+ }
|
|
|
190
|
+
|
|
|
191
|
+ if (position < 0) {
|
|
|
192
|
+ return this;
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
|
195
|
+ if (position === 0) {
|
|
|
196
|
+ list.shift();
|
|
|
197
|
+ } else {
|
|
|
198
|
+ spliceOne(list, position);
|
|
|
199
|
+ }
|
|
|
200
|
+
|
|
|
201
|
+ if (list.length === 1) {
|
|
|
202
|
+ events[type] = list[0];
|
|
|
203
|
+ }
|
|
|
204
|
+ }
|
|
|
205
|
+
|
|
|
206
|
+ return this;
|
|
|
207
|
+ }
|
|
|
208
|
+
|
|
|
209
|
+ /**
|
|
|
210
|
+ * removeAll 移除某事件名的全部监听事件/移除全部事件名的事件
|
|
|
211
|
+ * @param type 事件名
|
|
|
212
|
+ */
|
|
|
213
|
+ removeAll(type) {
|
|
|
214
|
+ let listeners, events;
|
|
|
215
|
+
|
|
|
216
|
+ events = this._events;
|
|
|
217
|
+
|
|
|
218
|
+ if (!events)
|
|
|
219
|
+ return this;
|
|
|
220
|
+
|
|
|
221
|
+ if (arguments.length === 0) {
|
|
|
222
|
+ var keys = Object.keys(events);
|
|
|
223
|
+ var key;
|
|
|
224
|
+ for (let i = 0; i < keys.length; ++i) {
|
|
|
225
|
+ key = keys[i];
|
|
|
226
|
+ this.removeAll(key);
|
|
|
227
|
+ }
|
|
|
228
|
+ this._events = Object.create(null);
|
|
|
229
|
+ this._eventsCount = 0;
|
|
|
230
|
+ return this;
|
|
|
231
|
+ }
|
|
|
232
|
+
|
|
|
233
|
+ listeners = events[type];
|
|
|
234
|
+
|
|
|
235
|
+ if (typeof listeners === 'function') {
|
|
|
236
|
+ this.remove(type, listeners);
|
|
|
237
|
+ } else if (listeners !== undefined) {
|
|
|
238
|
+ for (let i = listeners.length - 1; i >= 0; i--) {
|
|
|
239
|
+ this.remove(type, listeners[i]);
|
|
|
240
|
+ }
|
|
|
241
|
+ }
|
|
|
242
|
+
|
|
|
243
|
+ return this;
|
|
|
244
|
+ }
|
|
|
245
|
+
|
|
|
246
|
+ eventNames() {
|
|
|
247
|
+ return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : [];
|
|
|
248
|
+ }
|
|
|
249
|
+}
|
|
|
250
|
+
|
|
|
251
|
+
|
|
|
252
|
+const emitNone = (handler, isFn, self) => {
|
|
|
253
|
+ if (isFn) {
|
|
|
254
|
+ handler.call(self);
|
|
|
255
|
+ } else {
|
|
|
256
|
+ let len = handler.length;
|
|
|
257
|
+ let listeners = arrayClone(handler, len);
|
|
|
258
|
+ for (let i = 0; i < len; ++i)
|
|
|
259
|
+ listeners[i].call(self);
|
|
|
260
|
+ }
|
|
|
261
|
+}
|
|
|
262
|
+
|
|
|
263
|
+const emitOne = (handler, isFn, self, arg1) => {
|
|
|
264
|
+ if (isFn) {
|
|
|
265
|
+ handler.call(self, arg1);
|
|
|
266
|
+ } else {
|
|
|
267
|
+ let len = handler.length;
|
|
|
268
|
+ let listeners = arrayClone(handler, len);
|
|
|
269
|
+ for (let i = 0; i < len; ++i)
|
|
|
270
|
+ listeners[i].call(self, arg1);
|
|
|
271
|
+ }
|
|
|
272
|
+}
|
|
|
273
|
+
|
|
|
274
|
+const emitTwo = (handler, isFn, self, arg1, arg2) => {
|
|
|
275
|
+ if (isFn) {
|
|
|
276
|
+ handler.call(self, arg1, arg2);
|
|
|
277
|
+ } else {
|
|
|
278
|
+ let len = handler.length;
|
|
|
279
|
+ let listeners = arrayClone(handler, len);
|
|
|
280
|
+ for (let i = 0; i < len; ++i)
|
|
|
281
|
+ listeners[i].call(self, arg1, arg2);
|
|
|
282
|
+ }
|
|
|
283
|
+}
|
|
|
284
|
+
|
|
|
285
|
+const emitThree = (handler, isFn, self, arg1, arg2, arg3) => {
|
|
|
286
|
+ if (isFn) {
|
|
|
287
|
+ handler.call(self, arg1, arg2, arg3);
|
|
|
288
|
+ } else {
|
|
|
289
|
+ let len = handler.length;
|
|
|
290
|
+ let listeners = arrayClone(handler, len);
|
|
|
291
|
+ for (let i = 0; i < len; ++i)
|
|
|
292
|
+ listeners[i].call(self, arg1, arg2, arg3);
|
|
|
293
|
+ }
|
|
|
294
|
+}
|
|
|
295
|
+
|
|
|
296
|
+const emitMany = (handler, isFn, self, args) => {
|
|
|
297
|
+ if (isFn) {
|
|
|
298
|
+ handler.apply(self, args);
|
|
|
299
|
+ } else {
|
|
|
300
|
+ let len = handler.length;
|
|
|
301
|
+ let listeners = arrayClone(handler, len);
|
|
|
302
|
+ for (let i = 0; i < len; ++i)
|
|
|
303
|
+ listeners[i].apply(self, args);
|
|
|
304
|
+ }
|
|
|
305
|
+}
|
|
|
306
|
+
|
|
|
307
|
+const spliceOne = (list, index) => {
|
|
|
308
|
+ for (; index + 1 < list.length; index++)
|
|
|
309
|
+ list[index] = list[index + 1];
|
|
|
310
|
+ list.pop();
|
|
|
311
|
+}
|
|
|
312
|
+
|
|
|
313
|
+function onceWrapper(...args) {
|
|
|
314
|
+ if (!this.fired) {
|
|
|
315
|
+ this.target.remove(this.type, this.wrapFn);
|
|
|
316
|
+ this.fired = true;
|
|
|
317
|
+ Reflect.apply(this.listener, this.target, args);
|
|
|
318
|
+ }
|
|
|
319
|
+}
|
|
|
320
|
+
|
|
|
321
|
+const _onceWrap = (target, type, listener) => {
|
|
|
322
|
+ var state = { fired: false, wrapFn: undefined, target, type, listener };
|
|
|
323
|
+ var wrapped = onceWrapper.bind(state);
|
|
|
324
|
+ wrapped.listener = listener;
|
|
|
325
|
+ state.wrapFn = wrapped;
|
|
|
326
|
+ return wrapped;
|
|
|
327
|
+}
|
|
|
328
|
+
|
|
|
329
|
+function arrayClone(arr, n) {
|
|
|
330
|
+ var copy = new Array(n);
|
|
|
331
|
+ for (var i = 0; i < n; ++i)
|
|
|
332
|
+ copy[i] = arr[i];
|
|
|
333
|
+ return copy;
|
|
|
334
|
+}
|
|
|
335
|
+
|
|
|
336
|
+const event = new EventEmitter();
|
|
|
337
|
+
|
|
|
338
|
+export default event; |