event.js
7.72 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* description: 参考借鉴 Node.js 的 EventEmitter
* 对象管理 event 事件
* @func on 监听
* @func emit 触发
* @func once 监听只执行一次
* @func remove 移除某个类型的某个事件
* @func removeAll 移除某个类型的全部事件/所有类型的全部事件
*/
class EventEmitter {
constructor() {
this._events = Object.create(null);
this._eventsCount = 0;
}
/**
* emit 触发事件
* @param type 事件名
* @param ...args 参数
*/
emit(type) {
let events, handler, len, isFn;
events = this._events;
handler = events[type];
console.log(events);
if (!handler)
return false;
isFn = typeof handler === 'function';
len = arguments.length;
// 优化性能
switch(len) {
case 0:
throw new Error('"emit" not have arguments');
break;
case 1:
emitNone(handler, isFn, this);
break;
case 2:
emitOne(handler, isFn, this, arguments[1]);
break;
case 3:
emitTwo(handler, isFn, this, arguments[1], arguments[2]);
break;
case 4:
emitThree(handler, isFn, this, arguments[1], arguments[2], arguments[3]);
break;
default:
args = new Array(len - 1);
for (let i = 1; i < len; i++) {
args[i - 1] = arguments[i];
}
emitMany(handler, isFn, this, args);
}
return true;
}
one(type, listener) {
let events;
if (typeof listener !== 'function')
throw new Error('"listener" argument must be a function');
events = this._events;
console.log(events);
if (typeof events === 'undefined') {
events = this._events = Object.create(null);
this._eventsCount = 0;
}
events[type] = listener;
++this._eventsCount;
return this;
}
_addListener(type, listener, prepend) {
let events;
let existing;
if (typeof listener !== 'function')
throw new Error('"listener" argument must be a function');
events = this._events;
console.log(events);
if (typeof events === 'undefined') {
events = this._events = Object.create(null);
this._eventsCount = 0;
} else {
existing = events[type];
}
if (!existing) {
existing = events[type] = listener;
++this._eventsCount;
} else {
if (typeof existing === 'function') {
existing = events[type] = prepend ? [listener, existing] : [existing, listener];
} else if (prepend) {
existing.unshift(listener);
} else {
existing.push(listener);
}
}
return this;
}
/**
* on 监听事件
* @param type 事件名
* @param listener 监听函数
*/
on(type, listener) {
return this._addListener(type, listener, false);
}
/**
* once 监听只执行一次事件
* @param type 事件名
* @param listener 只执行一次的监听函数
*/
once(type, listener) {
if (typeof listener !== 'function')
throw new Error('"listener" argument must be a function');
this.on(type, _onceWrap(this, type, listener));
return this;
}
/**
* prepend 优先监听事件
* @param type 事件名
* @param listener 要优先的监听函数
*/
prepend(type, listener) {
return this._addListener(type, listener, true);
}
/**
* prependOnce 优先只执行一次的监听事件
* @param type 事件名
* @param listener 要优先且只执行一次的监听函数
*/
prependOnce(type, listener) {
if (typeof listener !== 'function')
throw new TypeError('"listener" argument must be a function');
this.prepend(type, _onceWrap(this, type, listener));
return this;
}
/**
* remove 移除的监听事件
* @param type 事件名
* @param listener 要移除的监听函数(可选)
*/
remove(type, listener) {
let list, events, position, originalListener;
if (typeof listener !== 'function')
throw new TypeError('"listener" argument must be a function');
events = this._events;
if (!events) {
return this;
}
list = events[type];
if (!list) {
return this;
}
if (list === listener || list.listener === listener) {
if (--this._eventsCount === 0) {
this._events = Object.create(null);
} else {
delete events[type];
}
} else if (typeof list !== 'function') {
position = -1;
for (let i = list.length - 1; i >= 0; i--) {
if (list[i] === listener || list[i].listener === listener) {
originalListener = list[i].listener;
position = i;
break;
}
}
if (position < 0) {
return this;
}
if (position === 0) {
list.shift();
} else {
spliceOne(list, position);
}
if (list.length === 1) {
events[type] = list[0];
}
}
return this;
}
/**
* removeAll 移除某事件名的全部监听事件/移除全部事件名的事件
* @param type 事件名
*/
removeAll(type) {
let listeners, events;
events = this._events;
if (!events)
return this;
if (arguments.length === 0) {
var keys = Object.keys(events);
var key;
for (let i = 0; i < keys.length; ++i) {
key = keys[i];
this.removeAll(key);
}
this._events = Object.create(null);
this._eventsCount = 0;
return this;
}
listeners = events[type];
if (typeof listeners === 'function') {
this.remove(type, listeners);
} else if (listeners !== undefined) {
for (let i = listeners.length - 1; i >= 0; i--) {
this.remove(type, listeners[i]);
}
}
return this;
}
eventNames() {
return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : [];
}
}
const emitNone = (handler, isFn, self) => {
if (isFn) {
handler.call(self);
} else {
let len = handler.length;
let listeners = arrayClone(handler, len);
for (let i = 0; i < len; ++i)
listeners[i].call(self);
}
}
const emitOne = (handler, isFn, self, arg1) => {
if (isFn) {
handler.call(self, arg1);
} else {
let len = handler.length;
let listeners = arrayClone(handler, len);
for (let i = 0; i < len; ++i)
listeners[i].call(self, arg1);
}
}
const emitTwo = (handler, isFn, self, arg1, arg2) => {
if (isFn) {
handler.call(self, arg1, arg2);
} else {
let len = handler.length;
let listeners = arrayClone(handler, len);
for (let i = 0; i < len; ++i)
listeners[i].call(self, arg1, arg2);
}
}
const emitThree = (handler, isFn, self, arg1, arg2, arg3) => {
if (isFn) {
handler.call(self, arg1, arg2, arg3);
} else {
let len = handler.length;
let listeners = arrayClone(handler, len);
for (let i = 0; i < len; ++i)
listeners[i].call(self, arg1, arg2, arg3);
}
}
const emitMany = (handler, isFn, self, args) => {
if (isFn) {
handler.apply(self, args);
} else {
let len = handler.length;
let listeners = arrayClone(handler, len);
for (let i = 0; i < len; ++i)
listeners[i].apply(self, args);
}
}
const spliceOne = (list, index) => {
for (; index + 1 < list.length; index++)
list[index] = list[index + 1];
list.pop();
}
function onceWrapper(...args) {
if (!this.fired) {
this.target.remove(this.type, this.wrapFn);
this.fired = true;
Reflect.apply(this.listener, this.target, args);
}
}
const _onceWrap = (target, type, listener) => {
var state = { fired: false, wrapFn: undefined, target, type, listener };
var wrapped = onceWrapper.bind(state);
wrapped.listener = listener;
state.wrapFn = wrapped;
return wrapped;
}
function arrayClone(arr, n) {
var copy = new Array(n);
for (var i = 0; i < n; ++i)
copy[i] = arr[i];
return copy;
}
const event = new EventEmitter();
export default event;