1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.AsyncEmitter = exports.WaitUntilEvent = exports.Emitter = exports.Event = void 0;
|
19 |
|
20 | const disposable_1 = require("./disposable");
|
21 | var Event;
|
22 | (function (Event) {
|
23 | const _disposable = { dispose() { } };
|
24 | function getMaxListeners(event) {
|
25 | const { maxListeners } = event;
|
26 | return typeof maxListeners === 'number' ? maxListeners : 0;
|
27 | }
|
28 | Event.getMaxListeners = getMaxListeners;
|
29 | function setMaxListeners(event, maxListeners) {
|
30 | if (typeof event.maxListeners === 'number') {
|
31 | return event.maxListeners = maxListeners;
|
32 | }
|
33 | return maxListeners;
|
34 | }
|
35 | Event.setMaxListeners = setMaxListeners;
|
36 | function addMaxListeners(event, add) {
|
37 | if (typeof event.maxListeners === 'number') {
|
38 | return event.maxListeners += add;
|
39 | }
|
40 | return add;
|
41 | }
|
42 | Event.addMaxListeners = addMaxListeners;
|
43 | Event.None = Object.assign(function () { return _disposable; }, {
|
44 | get maxListeners() { return 0; },
|
45 | set maxListeners(maxListeners) { }
|
46 | });
|
47 | |
48 |
|
49 |
|
50 |
|
51 | function map(event, mapFunc) {
|
52 | return Object.assign((listener, thisArgs, disposables) => event(i => listener.call(thisArgs, mapFunc(i)), undefined, disposables), {
|
53 | get maxListeners() { return 0; },
|
54 | set maxListeners(maxListeners) { }
|
55 | });
|
56 | }
|
57 | Event.map = map;
|
58 | })(Event = exports.Event || (exports.Event = {}));
|
59 | class CallbackList {
|
60 | get length() {
|
61 | return this._callbacks && this._callbacks.length || 0;
|
62 | }
|
63 | add(callback, context = undefined, bucket) {
|
64 | if (!this._callbacks) {
|
65 | this._callbacks = [];
|
66 | this._contexts = [];
|
67 | }
|
68 | this._callbacks.push(callback);
|
69 | this._contexts.push(context);
|
70 | if (Array.isArray(bucket)) {
|
71 | bucket.push({ dispose: () => this.remove(callback, context) });
|
72 | }
|
73 | }
|
74 | remove(callback, context = undefined) {
|
75 | if (!this._callbacks) {
|
76 | return;
|
77 | }
|
78 | let foundCallbackWithDifferentContext = false;
|
79 | for (let i = 0; i < this._callbacks.length; i++) {
|
80 | if (this._callbacks[i] === callback) {
|
81 | if (this._contexts[i] === context) {
|
82 |
|
83 | this._callbacks.splice(i, 1);
|
84 | this._contexts.splice(i, 1);
|
85 | return;
|
86 | }
|
87 | else {
|
88 | foundCallbackWithDifferentContext = true;
|
89 | }
|
90 | }
|
91 | }
|
92 | if (foundCallbackWithDifferentContext) {
|
93 | throw new Error('When adding a listener with a context, you should remove it with the same context');
|
94 | }
|
95 | }
|
96 |
|
97 | [Symbol.iterator]() {
|
98 | if (!this._callbacks) {
|
99 | return [][Symbol.iterator]();
|
100 | }
|
101 | const callbacks = this._callbacks.slice(0);
|
102 | const contexts = this._contexts.slice(0);
|
103 | return callbacks.map((callback, i) => (...args) => callback.apply(contexts[i], args))[Symbol.iterator]();
|
104 | }
|
105 | invoke(...args) {
|
106 | const ret = [];
|
107 | for (const callback of this) {
|
108 | try {
|
109 | ret.push(callback(...args));
|
110 | }
|
111 | catch (e) {
|
112 | console.error(e);
|
113 | }
|
114 | }
|
115 | return ret;
|
116 | }
|
117 | isEmpty() {
|
118 | return !this._callbacks || this._callbacks.length === 0;
|
119 | }
|
120 | dispose() {
|
121 | this._callbacks = undefined;
|
122 | this._contexts = undefined;
|
123 | }
|
124 | }
|
125 | class Emitter {
|
126 | constructor(_options) {
|
127 | this._options = _options;
|
128 | this._disposed = false;
|
129 | this._leakWarnCountdown = 0;
|
130 | }
|
131 | |
132 |
|
133 |
|
134 |
|
135 | get event() {
|
136 | if (!this._event) {
|
137 | this._event = Object.assign((listener, thisArgs, disposables) => {
|
138 | if (!this._callbacks) {
|
139 | this._callbacks = new CallbackList();
|
140 | }
|
141 | if (this._options && this._options.onFirstListenerAdd && this._callbacks.isEmpty()) {
|
142 | this._options.onFirstListenerAdd(this);
|
143 | }
|
144 | this._callbacks.add(listener, thisArgs);
|
145 | const removeMaxListenersCheck = this.checkMaxListeners(Event.getMaxListeners(this._event));
|
146 | const result = {
|
147 | dispose: () => {
|
148 | if (removeMaxListenersCheck) {
|
149 | removeMaxListenersCheck();
|
150 | }
|
151 | result.dispose = Emitter._noop;
|
152 | if (!this._disposed) {
|
153 | this._callbacks.remove(listener, thisArgs);
|
154 | result.dispose = Emitter._noop;
|
155 | if (this._options && this._options.onLastListenerRemove && this._callbacks.isEmpty()) {
|
156 | this._options.onLastListenerRemove(this);
|
157 | }
|
158 | }
|
159 | }
|
160 | };
|
161 | if (disposable_1.DisposableGroup.canPush(disposables)) {
|
162 | disposables.push(result);
|
163 | }
|
164 | else if (disposable_1.DisposableGroup.canAdd(disposables)) {
|
165 | disposables.add(result);
|
166 | }
|
167 | return result;
|
168 | }, {
|
169 | maxListeners: Emitter.LEAK_WARNING_THRESHHOLD
|
170 | });
|
171 | }
|
172 | return this._event;
|
173 | }
|
174 | checkMaxListeners(maxListeners) {
|
175 | if (maxListeners === 0 || !this._callbacks) {
|
176 | return undefined;
|
177 | }
|
178 | const listenerCount = this._callbacks.length;
|
179 | if (listenerCount <= maxListeners) {
|
180 | return undefined;
|
181 | }
|
182 | const popStack = this.pushLeakingStack();
|
183 | this._leakWarnCountdown -= 1;
|
184 | if (this._leakWarnCountdown <= 0) {
|
185 |
|
186 |
|
187 | this._leakWarnCountdown = maxListeners * 0.5;
|
188 | let topStack;
|
189 | let topCount = 0;
|
190 | this._leakingStacks.forEach((stackCount, stack) => {
|
191 | if (!topStack || topCount < stackCount) {
|
192 | topStack = stack;
|
193 | topCount = stackCount;
|
194 | }
|
195 | });
|
196 |
|
197 | console.warn(`Possible Emitter memory leak detected. ${listenerCount} listeners added. Use event.maxListeners to increase the limit (${maxListeners}). MOST frequent listener (${topCount}):`);
|
198 | console.warn(topStack);
|
199 | }
|
200 | return popStack;
|
201 | }
|
202 | pushLeakingStack() {
|
203 | if (!this._leakingStacks) {
|
204 | this._leakingStacks = new Map();
|
205 | }
|
206 | const stack = new Error().stack.split('\n').slice(3).join('\n');
|
207 | const count = (this._leakingStacks.get(stack) || 0);
|
208 | this._leakingStacks.set(stack, count + 1);
|
209 | return () => this.popLeakingStack(stack);
|
210 | }
|
211 | popLeakingStack(stack) {
|
212 | if (!this._leakingStacks) {
|
213 | return;
|
214 | }
|
215 | const count = (this._leakingStacks.get(stack) || 0);
|
216 | this._leakingStacks.set(stack, count - 1);
|
217 | }
|
218 | |
219 |
|
220 |
|
221 |
|
222 | fire(event) {
|
223 | if (this._callbacks) {
|
224 | this._callbacks.invoke(event);
|
225 | }
|
226 | }
|
227 | |
228 |
|
229 |
|
230 |
|
231 | async sequence(processor) {
|
232 | if (this._callbacks) {
|
233 | for (const listener of this._callbacks) {
|
234 | if (!await processor(listener)) {
|
235 | break;
|
236 | }
|
237 | }
|
238 | }
|
239 | }
|
240 | dispose() {
|
241 | if (this._leakingStacks) {
|
242 | this._leakingStacks.clear();
|
243 | this._leakingStacks = undefined;
|
244 | }
|
245 | if (this._callbacks) {
|
246 | this._callbacks.dispose();
|
247 | this._callbacks = undefined;
|
248 | }
|
249 | this._disposed = true;
|
250 | }
|
251 | }
|
252 | exports.Emitter = Emitter;
|
253 | Emitter.LEAK_WARNING_THRESHHOLD = 175;
|
254 | Emitter._noop = function () { };
|
255 | var WaitUntilEvent;
|
256 | (function (WaitUntilEvent) {
|
257 | |
258 |
|
259 |
|
260 |
|
261 |
|
262 | async function fire(emitter, event, timeout, token = cancellation_1.CancellationToken.None) {
|
263 | const waitables = [];
|
264 | const asyncEvent = Object.assign(event, {
|
265 | token,
|
266 | waitUntil: (thenable) => {
|
267 | if (Object.isFrozen(waitables)) {
|
268 | throw new Error('waitUntil cannot be called asynchronously.');
|
269 | }
|
270 | waitables.push(thenable);
|
271 | }
|
272 | });
|
273 | try {
|
274 | emitter.fire(asyncEvent);
|
275 |
|
276 | Object.freeze(waitables);
|
277 | }
|
278 | finally {
|
279 | delete asyncEvent['waitUntil'];
|
280 | }
|
281 | if (!waitables.length) {
|
282 | return;
|
283 | }
|
284 | if (timeout !== undefined) {
|
285 | await Promise.race([Promise.all(waitables), new Promise(resolve => setTimeout(resolve, timeout))]);
|
286 | }
|
287 | else {
|
288 | await Promise.all(waitables);
|
289 | }
|
290 | }
|
291 | WaitUntilEvent.fire = fire;
|
292 | })(WaitUntilEvent = exports.WaitUntilEvent || (exports.WaitUntilEvent = {}));
|
293 | const cancellation_1 = require("./cancellation");
|
294 | class AsyncEmitter extends Emitter {
|
295 | |
296 |
|
297 |
|
298 | fire(event, token = cancellation_1.CancellationToken.None, promiseJoin) {
|
299 | const callbacks = this._callbacks;
|
300 | if (!callbacks) {
|
301 | return Promise.resolve();
|
302 | }
|
303 | const listeners = [...callbacks];
|
304 | if (this.deliveryQueue) {
|
305 | return this.deliveryQueue = this.deliveryQueue.then(() => this.deliver(listeners, event, token, promiseJoin));
|
306 | }
|
307 | return this.deliveryQueue = this.deliver(listeners, event, token, promiseJoin);
|
308 | }
|
309 | async deliver(listeners, event, token, promiseJoin) {
|
310 | for (const listener of listeners) {
|
311 | if (token.isCancellationRequested) {
|
312 | return;
|
313 | }
|
314 | const waitables = [];
|
315 | const asyncEvent = Object.assign(event, {
|
316 | token,
|
317 | waitUntil: (thenable) => {
|
318 | if (Object.isFrozen(waitables)) {
|
319 | throw new Error('waitUntil cannot be called asynchronously.');
|
320 | }
|
321 | if (promiseJoin) {
|
322 | thenable = promiseJoin(thenable, listener);
|
323 | }
|
324 | waitables.push(thenable);
|
325 | }
|
326 | });
|
327 | try {
|
328 | listener(event);
|
329 |
|
330 | Object.freeze(waitables);
|
331 | }
|
332 | catch (e) {
|
333 | console.error(e);
|
334 | }
|
335 | finally {
|
336 | delete asyncEvent['waitUntil'];
|
337 | }
|
338 | if (!waitables.length) {
|
339 | return;
|
340 | }
|
341 | try {
|
342 | await Promise.all(waitables);
|
343 | }
|
344 | catch (e) {
|
345 | console.error(e);
|
346 | }
|
347 | }
|
348 | }
|
349 | }
|
350 | exports.AsyncEmitter = AsyncEmitter;
|
351 |
|
\ | No newline at end of file |