-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheventEmitter.js
More file actions
168 lines (153 loc) · 4.12 KB
/
eventEmitter.js
File metadata and controls
168 lines (153 loc) · 4.12 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
import sleep from './sleep.js';
export default function eventEmitter() {
const events = {
// eventName: [events]
};
const singletonEvents = {
// eventName: { data, async }
};
const options = {
cancelable: false,
canceled: false,
singleton: false,
async: false,
};
options.cancelable = undefined;
function reset() {
const ret = { ...options };
options.cancelable = undefined;
options.canceled = false;
options.singleton = false;
options.async = false;
return ret;
}
function emit(event, e, ...data) {
const {
canceled: canceledState,
cancelable = canceledState,
singleton,
async,
} = reset();
const delayed = e !== events[event];
// If this event has run previously, don't run it again
if (singletonEvents[event] && !delayed) {
const ret = {
ran: false,
canceled: false,
};
if (async) return Promise.resolve(ret);
return ret;
}
if (singleton) { // Need to save even if we don't run
singletonEvents[event] = {
data,
async,
};
}
let ran = false;
let canceled = canceledState;
const promises = [];
if (Array.isArray(e) && e.length) {
ran = true;
[...e].forEach((ev) => {
// Should we stop processing on cancel? Maybe.
try {
const meta = { event, cancelable, canceled, async, delayed, singleton };
const ret = ev.call(meta, ...data);
if (async && ret !== undefined) {
promises.push(Promise.resolve(ret)
.then(() => canceled = !!meta.canceled) // This is so broken, should process these one at a time
.catch((err) => {
console.error(`Error occurred while parsing (async) event: ${ev.displayName || ev.name || 'unnamed'}(${event})`, err, ...data);
}));
}
canceled = !!meta.canceled;
} catch (err) {
console.error(`Error occurred while parsing event: ${ev.displayName || ev.name || 'unnamed'}(${event})`, err, ...data);
}
});
}
function results() {
return {
ran,
canceled: cancelable && canceled,
};
}
if (async) {
return Promise.all(promises).then(results);
}
return results();
}
function off(event, fn) {
event.split(' ').forEach((e) => {
const list = events[e] || [];
while (list.includes(fn)) {
list.splice(list.indexOf(fn), 1);
}
});
}
const emitter = {
on(event, fn) {
reset();
if (typeof fn !== 'function') return this;
event.split(' ').forEach((e) => {
const singleton = singletonEvents[e];
if (singleton) {
sleep().then(() => {
reset(); // Make sure nothing is set!
options.async = singleton.async;
emit(e, [fn], ...singleton.data);
});
} else {
if (!Array.isArray(events[e])) {
events[e] = [];
}
events[e].push(fn);
}
});
return this;
},
once(event, fn) {
if (typeof fn !== 'function') return this;
function wrapper(...args) {
off(event, wrapper);
return fn.call(this, ...args);
}
return this.on(event, wrapper);
},
one(event, fn) {
return this.once(event, fn);
},
until(event, fn) {
if (typeof fn !== 'function') return this;
function wrapper(...args) {
const ret = fn.call(this, ...args);
if (ret instanceof Promise) {
return ret.then((val) => {
if (val) off(event, wrapper);
return val;
});
}
if (ret) {
off(event, wrapper);
}
return ret;
}
return this.on(event, wrapper);
},
emit: (event, ...data) => emit(event, singletonEvents[event] || events[event], ...data),
off(event, fn) {
off(event, fn);
return this;
},
};
Object.keys(options).forEach((key) => {
Object.defineProperty(emitter, key, {
get() {
options[key] = true;
return this;
},
});
});
return Object.freeze(emitter);
}