EventKit.ts
6.01 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
export class Disposable {
private _disposed = false;
private _disposalAction: () => void;
constructor(disposalAction: () => void) {
this._disposalAction = disposalAction;
}
get disposed() {
return this._disposed;
}
dispose() {
if (!this.disposed) {
this._disposed = true;
if (this._disposalAction) {
this._disposalAction();
this._disposalAction = null;
}
}
}
}
export class CompositeDisposable {
private _disposed = false;
private _disposables: Set<Disposable> = new Set();
constructor(...disposables: Disposable[]) {
for (let disposable of disposables) {
this.add(disposable);
}
}
add(disposable: Disposable) {
if (!this._disposed) {
if (disposable) {
this._disposables.add(disposable);
}
}
}
remove(disposable: Disposable) {
if (!this._disposed) {
this._disposables.delete(disposable);
}
}
clear() {
if (!this._disposed) {
this._disposables.clear();
}
}
dispose() {
if (!this._disposed) {
this._disposed = true;
this._disposables.forEach((disposable) => {
disposable.dispose();
});
this._disposables = null;
}
}
}
export class Event<T extends (...args: any[]) => any> {
private emitter: Emitter;
private name: string | symbol;
constructor(emitter: Emitter, name: string | symbol) {
this.emitter = emitter;
this.name = name;
}
getName() {
return this.name;
}
toString() {
return `[Event ${this.name.toString()}]`;
}
on(handler: T, unshift: boolean = false) {
return this.emitter.on(this.name, handler, unshift);
}
once(handler: T, unshift: boolean = false) {
return this.emitter.once(this.name, handler, unshift);
}
readonly emit = ((...argsparam: any[]): Promise<ReturnType<T>>[] => {
argsparam.unshift(this.name);
return this.emitter.emit.apply(this.emitter, argsparam);
}) as T;
}
export class Emitter {
private _disposed = false;
private _handlersByEventName: Map<string | symbol, Array<(...args: any[]) => any>> = new Map();
get disposed() {
return this._disposed;
}
clear() {
this._handlersByEventName.clear();
}
dispose() {
this._handlersByEventName = null;
this._disposed = true;
}
createEvent<T extends (...args: any[]) => void>(eventName?: string | symbol) {
if (!eventName) {
return new Event<T>(this, Symbol("Event"));
}
return new Event<T>(this, eventName);
}
on(eventName: string | symbol, handler: (...args: any[]) => any, unshift: boolean = false) {
if (this._disposed) {
throw new Error("Emitter has been diposed");
}
let currentHandler = this._handlersByEventName.get(eventName);
if (currentHandler) {
if (unshift) {
currentHandler.unshift(handler);
} else {
currentHandler.push(handler);
}
} else {
this._handlersByEventName.set(eventName, [handler]);
}
return new Disposable(this.off.bind(this, eventName, handler));
}
once(eventName: string | symbol, handler: (...args: any[]) => any, unshift: boolean = false) {
let wrapped = (...args: any[]) => {
disposable.dispose();
return handler(...args);
};
let disposable = this.on(eventName, wrapped, unshift);
return disposable;
}
private off = (eventName: string | symbol, handlerToRemove: (...args: any[]) => any) => {
if (this._disposed) return;
let oldHandlers = this._handlersByEventName.get(eventName);
if (oldHandlers) {
let newHandlers = [];
for (let handler of oldHandlers) {
if (handler != handlerToRemove) {
newHandlers.push(handler);
}
}
if (newHandlers.length) {
this._handlersByEventName.set(eventName, newHandlers);
} else {
this._handlersByEventName.delete(eventName);
}
}
};
emit(eventName: string | symbol, ...args: any[]) {
let handlers = this._handlersByEventName.get(eventName);
let result: Promise<any>[] = [];
if (handlers) {
for (let handler of handlers) {
try {
let ret = handler(...args);
if (ret === undefined) {
result.push(Promise.resolve());
} else if (ret instanceof Promise) {
result.push(ret);
} else {
result.push(Promise.resolve(ret));
}
} catch (e) {
console.error(e);
}
}
}
return result;
}
getEventNames() {
let ret: string[] = [];
let keys = Object.keys(this._handlersByEventName);
for (let k of keys) {
if (typeof (k) === 'string') {
ret.push(k);
}
}
return ret;
}
getEventSymbols() {
let ret: symbol[] = [];
let keys = Object.keys(this._handlersByEventName);
for (let k of keys) {
if (typeof (k) === 'symbol') {
ret.push(k);
}
}
return ret;
}
listenerCountForEventName(eventName: string | symbol) {
let handlers = this._handlersByEventName.get(eventName);
return handlers ? handlers.length : 0;
}
getTotalListenerCount() {
let result = 0;
let keys = Object.keys(this._handlersByEventName);
for (let eventName of keys) {
result += this._handlersByEventName.get(eventName).length;
}
return result;
}
}