-
Notifications
You must be signed in to change notification settings - Fork 77
/
event.js
87 lines (71 loc) · 2.2 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
/**
* @authors : qieguo
* @date : 2016/12/09
* @version : 1.0
* @description : 一个简单的观察者模式事件系统实现
*/
const Noop = () => {};
class EventEmitter {
constructor() {
this.events = [];
}
on(event, handler = Noop) {
if (typeof this.events[event] === 'undefined') {
this.events[event] = [handler];
} else {
this.events[event].push(handler);
}
}
once(event, handler = Noop) {
const once = `once_${event}`;
if (typeof this.events[once] === 'undefined') {
this.events[once] = [handler];
} else {
this.events[once].push(handler);
}
}
emit(event, args) {
const once = `once_${event}`;
if (typeof this.events[once] !== 'undefined') {
this.events[once].forEach((handler) => {
handler(args);
});
delete this.events[`once_${event}`];
}
if (typeof this.events[event] !== 'undefined') {
this.events[event].forEach((handler) => {
handler(args);
});
}
}
off(event, handler) {
if (typeof this.events[event] !== 'undefined') {
if (!!handler) {
const index = this.events[event].indexOf(handler);
this.events[event].splice(index, 1);
} else {
delete this.events[event];
}
}
};
}
function test() {
const eh = new EventEmitter();
const firstHd = (str) => {console.log('first greet: ', str);};
eh.on('greet', firstHd);
eh.on('greet', (str) => {console.log('second greet: ', str);});
eh.on('bye', (name) => {console.log(name + ', goodbye!');});
eh.once('break', (str) => {console.log(`once break: ${str}`);});
eh.on('break', (str) => {console.log(`on break: ${str}`);});
console.log('====== start ======');
eh.emit('greet', 'Green');
eh.emit('bye', 'Mark');
eh.emit('break', 'Jack');
eh.emit('break', 'Tony');
console.log('====== removeListener ======');
eh.off('bye');
eh.emit('bye', 'Mark');
eh.off('greet', firstHd);
eh.emit('greet', 'Green');
}
test();