Debian Codegraph @

node/@xterm/xterm/src/vs/base/common/event.ts

raw
1 /*---------------------------------------------------------------------------------------------
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT License. See License.txt in the project root for license information.
4 *--------------------------------------------------------------------------------------------*/
5
6 import { CancellationToken } from 'vs/base/common/cancellation';
7 import { onUnexpectedError } from 'vs/base/common/errors';
8 import { createSingleCallFunction } from 'vs/base/common/functional';
9 import { combinedDisposable, Disposable, DisposableMap, DisposableStore, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
10 import { LinkedList } from 'vs/base/common/linkedList';
11 import { IObservable, IObserver } from 'vs/base/common/observable';
12 import { StopWatch } from 'vs/base/common/stopwatch';
13 import { MicrotaskDelay } from 'vs/base/common/symbols';
14
15
16 // -----------------------------------------------------------------------------------------------------------------------
17 // Uncomment the next line to print warnings whenever a listener is GC'ed without having been disposed. This is a LEAK.
18 // -----------------------------------------------------------------------------------------------------------------------
19 const _enableListenerGCedWarning = false
20 // || Boolean("TRUE") // causes a linter warning so that it cannot be pushed
21 ;
22
23 // -----------------------------------------------------------------------------------------------------------------------
24 // Uncomment the next line to print warnings whenever an emitter with listeners is disposed. That is a sign of code smell.
25 // -----------------------------------------------------------------------------------------------------------------------
26 const _enableDisposeWithListenerWarning = false
27 // || Boolean("TRUE") // causes a linter warning so that it cannot be pushed
28 ;
29
30
31 // -----------------------------------------------------------------------------------------------------------------------
32 // Uncomment the next line to print warnings whenever a snapshotted event is used repeatedly without cleanup.
33 // See https://github.com/microsoft/vscode/issues/142851
34 // -----------------------------------------------------------------------------------------------------------------------
35 const _enableSnapshotPotentialLeakWarning = false
36 // || Boolean("TRUE") // causes a linter warning so that it cannot be pushed
37 ;
38
39 /**
40 * An event with zero or one parameters that can be subscribed to. The event is a function itself.
41 */
42 export interface Event<T> {
43 (listener: (e: T) => any, thisArgs?: any, disposables?: IDisposable[] | DisposableStore): IDisposable;
44 }
45
46 export namespace Event {
47 export const None: Event<any> = () => Disposable.None;
48
49 function _addLeakageTraceLogic(options: EmitterOptions) {
50 if (_enableSnapshotPotentialLeakWarning) {
51 const { onDidAddListener: origListenerDidAdd } = options;
52 const stack = Stacktrace.create();
53 let count = 0;
54 options.onDidAddListener = () => {
55 if (++count === 2) {
56 console.warn('snapshotted emitter LIKELY used public and SHOULD HAVE BEEN created with DisposableStore. snapshotted here');
57 stack.print();
58 }
59 origListenerDidAdd?.();
60 };
61 }
62 }
63
64 /**
65 * Given an event, returns another event which debounces calls and defers the listeners to a later task via a shared
66 * `setTimeout`. The event is converted into a signal (`Event<void>`) to avoid additional object creation as a
67 * result of merging events and to try prevent race conditions that could arise when using related deferred and
68 * non-deferred events.
69 *
70 * This is useful for deferring non-critical work (eg. general UI updates) to ensure it does not block critical work
71 * (eg. latency of keypress to text rendered).
72 *
73 * *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
74 * event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
75 * returned event causes this utility to leak a listener on the original event.
76 *
77 * @param event The event source for the new event.
78 * @param disposable A disposable store to add the new EventEmitter to.
79 */
80 export function defer(event: Event<unknown>, disposable?: DisposableStore): Event<void> {
81 return debounce<unknown, void>(event, () => void 0, 0, undefined, true, undefined, disposable);
82 }
83
84 /**
85 * Given an event, returns another event which only fires once.
86 *
87 * @param event The event source for the new event.
88 */
89 export function once<T>(event: Event<T>): Event<T> {
90 return (listener, thisArgs = null, disposables?) => {
91 // we need this, in case the event fires during the listener call
92 let didFire = false;
93 let result: IDisposable | undefined = undefined;
94 result = event(e => {
95 if (didFire) {
96 return;
97 } else if (result) {
98 result.dispose();
99 } else {
100 didFire = true;
101 }
102
103 return listener.call(thisArgs, e);
104 }, null, disposables);
105
106 if (didFire) {
107 result.dispose();
108 }
109
110 return result;
111 };
112 }
113
114 /**
115 * Maps an event of one type into an event of another type using a mapping function, similar to how
116 * `Array.prototype.map` works.
117 *
118 * *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
119 * event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
120 * returned event causes this utility to leak a listener on the original event.
121 *
122 * @param event The event source for the new event.
123 * @param map The mapping function.
124 * @param disposable A disposable store to add the new EventEmitter to.
125 */
126 export function map<I, O>(event: Event<I>, map: (i: I) => O, disposable?: DisposableStore): Event<O> {
127 return snapshot((listener, thisArgs = null, disposables?) => event(i => listener.call(thisArgs, map(i)), null, disposables), disposable);
128 }
129
130 /**
131 * Wraps an event in another event that performs some function on the event object before firing.
132 *
133 * *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
134 * event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
135 * returned event causes this utility to leak a listener on the original event.
136 *
137 * @param event The event source for the new event.
138 * @param each The function to perform on the event object.
139 * @param disposable A disposable store to add the new EventEmitter to.
140 */
141 export function forEach<I>(event: Event<I>, each: (i: I) => void, disposable?: DisposableStore): Event<I> {
142 return snapshot((listener, thisArgs = null, disposables?) => event(i => { each(i); listener.call(thisArgs, i); }, null, disposables), disposable);
143 }
144
145 /**
146 * Wraps an event in another event that fires only when some condition is met.
147 *
148 * *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
149 * event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
150 * returned event causes this utility to leak a listener on the original event.
151 *
152 * @param event The event source for the new event.
153 * @param filter The filter function that defines the condition. The event will fire for the object if this function
154 * returns true.
155 * @param disposable A disposable store to add the new EventEmitter to.
156 */
157 export function filter<T, U>(event: Event<T | U>, filter: (e: T | U) => e is T, disposable?: DisposableStore): Event<T>;
158 export function filter<T>(event: Event<T>, filter: (e: T) => boolean, disposable?: DisposableStore): Event<T>;
159 export function filter<T, R>(event: Event<T | R>, filter: (e: T | R) => e is R, disposable?: DisposableStore): Event<R>;
160 export function filter<T>(event: Event<T>, filter: (e: T) => boolean, disposable?: DisposableStore): Event<T> {
161 return snapshot((listener, thisArgs = null, disposables?) => event(e => filter(e) && listener.call(thisArgs, e), null, disposables), disposable);
162 }
163
164 /**
165 * Given an event, returns the same event but typed as `Event<void>`.
166 */
167 export function signal<T>(event: Event<T>): Event<void> {
168 return event as Event<any> as Event<void>;
169 }
170
171 /**
172 * Given a collection of events, returns a single event which emits whenever any of the provided events emit.
173 */
174 export function any<T>(...events: Event<T>[]): Event<T>;
175 export function any(...events: Event<any>[]): Event<void>;
176 export function any<T>(...events: Event<T>[]): Event<T> {
177 return (listener, thisArgs = null, disposables?) => {
178 const disposable = combinedDisposable(...events.map(event => event(e => listener.call(thisArgs, e))));
179 return addAndReturnDisposable(disposable, disposables);
180 };
181 }
182
183 /**
184 * *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
185 * event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
186 * returned event causes this utility to leak a listener on the original event.
187 */
188 export function reduce<I, O>(event: Event<I>, merge: (last: O | undefined, event: I) => O, initial?: O, disposable?: DisposableStore): Event<O> {
189 let output: O | undefined = initial;
190
191 return map<I, O>(event, e => {
192 output = merge(output, e);
193 return output;
194 }, disposable);
195 }
196
197 function snapshot<T>(event: Event<T>, disposable: DisposableStore | undefined): Event<T> {
198 let listener: IDisposable | undefined;
199
200 const options: EmitterOptions | undefined = {
201 onWillAddFirstListener() {
202 listener = event(emitter.fire, emitter);
203 },
204 onDidRemoveLastListener() {
205 listener?.dispose();
206 }
207 };
208
209 if (!disposable) {
210 _addLeakageTraceLogic(options);
211 }
212
213 const emitter = new Emitter<T>(options);
214
215 disposable?.add(emitter);
216
217 return emitter.event;
218 }
219
220 /**
221 * Adds the IDisposable to the store if it's set, and returns it. Useful to
222 * Event function implementation.
223 */
224 function addAndReturnDisposable<T extends IDisposable>(d: T, store: DisposableStore | IDisposable[] | undefined): T {
225 if (store instanceof Array) {
226 store.push(d);
227 } else if (store) {
228 store.add(d);
229 }
230 return d;
231 }
232
233 /**
234 * Given an event, creates a new emitter that event that will debounce events based on {@link delay} and give an
235 * array event object of all events that fired.
236 *
237 * *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
238 * event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
239 * returned event causes this utility to leak a listener on the original event.
240 *
241 * @param event The original event to debounce.
242 * @param merge A function that reduces all events into a single event.
243 * @param delay The number of milliseconds to debounce.
244 * @param leading Whether to fire a leading event without debouncing.
245 * @param flushOnListenerRemove Whether to fire all debounced events when a listener is removed. If this is not
246 * specified, some events could go missing. Use this if it's important that all events are processed, even if the
247 * listener gets disposed before the debounced event fires.
248 * @param leakWarningThreshold See {@link EmitterOptions.leakWarningThreshold}.
249 * @param disposable A disposable store to register the debounce emitter to.
250 */
251 export function debounce<T>(event: Event<T>, merge: (last: T | undefined, event: T) => T, delay?: number | typeof MicrotaskDelay, leading?: boolean, flushOnListenerRemove?: boolean, leakWarningThreshold?: number, disposable?: DisposableStore): Event<T>;
252 export function debounce<I, O>(event: Event<I>, merge: (last: O | undefined, event: I) => O, delay?: number | typeof MicrotaskDelay, leading?: boolean, flushOnListenerRemove?: boolean, leakWarningThreshold?: number, disposable?: DisposableStore): Event<O>;
253 export function debounce<I, O>(event: Event<I>, merge: (last: O | undefined, event: I) => O, delay: number | typeof MicrotaskDelay = 100, leading = false, flushOnListenerRemove = false, leakWarningThreshold?: number, disposable?: DisposableStore): Event<O> {
254 let subscription: IDisposable;
255 let output: O | undefined = undefined;
256 let handle: any = undefined;
257 let numDebouncedCalls = 0;
258 let doFire: (() => void) | undefined;
259
260 const options: EmitterOptions | undefined = {
261 leakWarningThreshold,
262 onWillAddFirstListener() {
263 subscription = event(cur => {
264 numDebouncedCalls++;
265 output = merge(output, cur);
266
267 if (leading && !handle) {
268 emitter.fire(output);
269 output = undefined;
270 }
271
272 doFire = () => {
273 const _output = output;
274 output = undefined;
275 handle = undefined;
276 if (!leading || numDebouncedCalls > 1) {
277 emitter.fire(_output!);
278 }
279 numDebouncedCalls = 0;
280 };
281
282 if (typeof delay === 'number') {
283 clearTimeout(handle);
284 handle = setTimeout(doFire, delay);
285 } else {
286 if (handle === undefined) {
287 handle = 0;
288 queueMicrotask(doFire);
289 }
290 }
291 });
292 },
293 onWillRemoveListener() {
294 if (flushOnListenerRemove && numDebouncedCalls > 0) {
295 doFire?.();
296 }
297 },
298 onDidRemoveLastListener() {
299 doFire = undefined;
300 subscription.dispose();
301 }
302 };
303
304 if (!disposable) {
305 _addLeakageTraceLogic(options);
306 }
307
308 const emitter = new Emitter<O>(options);
309
310 disposable?.add(emitter);
311
312 return emitter.event;
313 }
314
315 /**
316 * Debounces an event, firing after some delay (default=0) with an array of all event original objects.
317 *
318 * *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
319 * event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
320 * returned event causes this utility to leak a listener on the original event.
321 */
322 export function accumulate<T>(event: Event<T>, delay: number = 0, disposable?: DisposableStore): Event<T[]> {
323 return Event.debounce<T, T[]>(event, (last, e) => {
324 if (!last) {
325 return [e];
326 }
327 last.push(e);
328 return last;
329 }, delay, undefined, true, undefined, disposable);
330 }
331
332 /**
333 * Filters an event such that some condition is _not_ met more than once in a row, effectively ensuring duplicate
334 * event objects from different sources do not fire the same event object.
335 *
336 * *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
337 * event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
338 * returned event causes this utility to leak a listener on the original event.
339 *
340 * @param event The event source for the new event.
341 * @param equals The equality condition.
342 * @param disposable A disposable store to add the new EventEmitter to.
343 *
344 * @example
345 * ```
346 * // Fire only one time when a single window is opened or focused
347 * Event.latch(Event.any(onDidOpenWindow, onDidFocusWindow))
348 * ```
349 */
350 export function latch<T>(event: Event<T>, equals: (a: T, b: T) => boolean = (a, b) => a === b, disposable?: DisposableStore): Event<T> {
351 let firstCall = true;
352 let cache: T;
353
354 return filter(event, value => {
355 const shouldEmit = firstCall || !equals(value, cache);
356 firstCall = false;
357 cache = value;
358 return shouldEmit;
359 }, disposable);
360 }
361
362 /**
363 * Splits an event whose parameter is a union type into 2 separate events for each type in the union.
364 *
365 * *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
366 * event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
367 * returned event causes this utility to leak a listener on the original event.
368 *
369 * @example
370 * ```
371 * const event = new EventEmitter<number | undefined>().event;
372 * const [numberEvent, undefinedEvent] = Event.split(event, isUndefined);
373 * ```
374 *
375 * @param event The event source for the new event.
376 * @param isT A function that determines what event is of the first type.
377 * @param disposable A disposable store to add the new EventEmitter to.
378 */
379 export function split<T, U>(event: Event<T | U>, isT: (e: T | U) => e is T, disposable?: DisposableStore): [Event<T>, Event<U>] {
380 return [
381 Event.filter(event, isT, disposable),
382 Event.filter(event, e => !isT(e), disposable) as Event<U>,
383 ];
384 }
385
386 /**
387 * Buffers an event until it has a listener attached.
388 *
389 * *NOTE* that this function returns an `Event` and it MUST be called with a `DisposableStore` whenever the returned
390 * event is accessible to "third parties", e.g the event is a public property. Otherwise a leaked listener on the
391 * returned event causes this utility to leak a listener on the original event.
392 *
393 * @param event The event source for the new event.
394 * @param flushAfterTimeout Determines whether to flush the buffer after a timeout immediately or after a
395 * `setTimeout` when the first event listener is added.
396 * @param _buffer Internal: A source event array used for tests.
397 *
398 * @example
399 * ```
400 * // Start accumulating events, when the first listener is attached, flush
401 * // the event after a timeout such that multiple listeners attached before
402 * // the timeout would receive the event
403 * this.onInstallExtension = Event.buffer(service.onInstallExtension, true);
404 * ```
405 */
406 export function buffer<T>(event: Event<T>, flushAfterTimeout = false, _buffer: T[] = [], disposable?: DisposableStore): Event<T> {
407 let buffer: T[] | null = _buffer.slice();
408
409 let listener: IDisposable | null = event(e => {
410 if (buffer) {
411 buffer.push(e);
412 } else {
413 emitter.fire(e);
414 }
415 });
416
417 if (disposable) {
418 disposable.add(listener);
419 }
420
421 const flush = () => {
422 buffer?.forEach(e => emitter.fire(e));
423 buffer = null;
424 };
425
426 const emitter = new Emitter<T>({
427 onWillAddFirstListener() {
428 if (!listener) {
429 listener = event(e => emitter.fire(e));
430 if (disposable) {
431 disposable.add(listener);
432 }
433 }
434 },
435
436 onDidAddFirstListener() {
437 if (buffer) {
438 if (flushAfterTimeout) {
439 setTimeout(flush);
440 } else {
441 flush();
442 }
443 }
444 },
445
446 onDidRemoveLastListener() {
447 if (listener) {
448 listener.dispose();
449 }
450 listener = null;
451 }
452 });
453
454 if (disposable) {
455 disposable.add(emitter);
456 }
457
458 return emitter.event;
459 }
460 /**
461 * Wraps the event in an {@link IChainableEvent}, allowing a more functional programming style.
462 *
463 * @example
464 * ```
465 * // Normal
466 * const onEnterPressNormal = Event.filter(
467 * Event.map(onKeyPress.event, e => new StandardKeyboardEvent(e)),
468 * e.keyCode === KeyCode.Enter
469 * ).event;
470 *
471 * // Using chain
472 * const onEnterPressChain = Event.chain(onKeyPress.event, $ => $
473 * .map(e => new StandardKeyboardEvent(e))
474 * .filter(e => e.keyCode === KeyCode.Enter)
475 * );
476 * ```
477 */
478 export function chain<T, R>(event: Event<T>, sythensize: ($: IChainableSythensis<T>) => IChainableSythensis<R>): Event<R> {
479 const fn: Event<R> = (listener, thisArgs, disposables) => {
480 const cs = sythensize(new ChainableSynthesis()) as ChainableSynthesis;
481 return event(function (value) {
482 const result = cs.evaluate(value);
483 if (result !== HaltChainable) {
484 listener.call(thisArgs, result);
485 }
486 }, undefined, disposables);
487 };
488
489 return fn;
490 }
491
492 const HaltChainable = Symbol('HaltChainable');
493
494 class ChainableSynthesis implements IChainableSythensis<any> {
495 private readonly steps: ((input: any) => any)[] = [];
496
497 map<O>(fn: (i: any) => O): this {
498 this.steps.push(fn);
499 return this;
500 }
501
502 forEach(fn: (i: any) => void): this {
503 this.steps.push(v => {
504 fn(v);
505 return v;
506 });
507 return this;
508 }
509
510 filter(fn: (e: any) => boolean): this {
511 this.steps.push(v => fn(v) ? v : HaltChainable);
512 return this;
513 }
514
515 reduce<R>(merge: (last: R | undefined, event: any) => R, initial?: R | undefined): this {
516 let last = initial;
517 this.steps.push(v => {
518 last = merge(last, v);
519 return last;
520 });
521 return this;
522 }
523
524 latch(equals: (a: any, b: any) => boolean = (a, b) => a === b): ChainableSynthesis {
525 let firstCall = true;
526 let cache: any;
527 this.steps.push(value => {
528 const shouldEmit = firstCall || !equals(value, cache);
529 firstCall = false;
530 cache = value;
531 return shouldEmit ? value : HaltChainable;
532 });
533
534 return this;
535 }
536
537 public evaluate(value: any) {
538 for (const step of this.steps) {
539 value = step(value);
540 if (value === HaltChainable) {
541 break;
542 }
543 }
544
545 return value;
546 }
547 }
548
549 export interface IChainableSythensis<T> {
550 map<O>(fn: (i: T) => O): IChainableSythensis<O>;
551 forEach(fn: (i: T) => void): IChainableSythensis<T>;
552 filter<R extends T>(fn: (e: T) => e is R): IChainableSythensis<R>;
553 filter(fn: (e: T) => boolean): IChainableSythensis<T>;
554 reduce<R>(merge: (last: R, event: T) => R, initial: R): IChainableSythensis<R>;
555 reduce<R>(merge: (last: R | undefined, event: T) => R): IChainableSythensis<R>;
556 latch(equals?: (a: T, b: T) => boolean): IChainableSythensis<T>;
557 }
558
559 export interface NodeEventEmitter {
560 on(event: string | symbol, listener: Function): unknown;
561 removeListener(event: string | symbol, listener: Function): unknown;
562 }
563
564 /**
565 * Creates an {@link Event} from a node event emitter.
566 */
567 export function fromNodeEventEmitter<T>(emitter: NodeEventEmitter, eventName: string, map: (...args: any[]) => T = id => id): Event<T> {
568 const fn = (...args: any[]) => result.fire(map(...args));
569 const onFirstListenerAdd = () => emitter.on(eventName, fn);
570 const onLastListenerRemove = () => emitter.removeListener(eventName, fn);
571 const result = new Emitter<T>({ onWillAddFirstListener: onFirstListenerAdd, onDidRemoveLastListener: onLastListenerRemove });
572
573 return result.event;
574 }
575
576 export interface DOMEventEmitter {
577 addEventListener(event: string | symbol, listener: Function): void;
578 removeEventListener(event: string | symbol, listener: Function): void;
579 }
580
581 /**
582 * Creates an {@link Event} from a DOM event emitter.
583 */
584 export function fromDOMEventEmitter<T>(emitter: DOMEventEmitter, eventName: string, map: (...args: any[]) => T = id => id): Event<T> {
585 const fn = (...args: any[]) => result.fire(map(...args));
586 const onFirstListenerAdd = () => emitter.addEventListener(eventName, fn);
587 const onLastListenerRemove = () => emitter.removeEventListener(eventName, fn);
588 const result = new Emitter<T>({ onWillAddFirstListener: onFirstListenerAdd, onDidRemoveLastListener: onLastListenerRemove });
589
590 return result.event;
591 }
592
593 /**
594 * Creates a promise out of an event, using the {@link Event.once} helper.
595 */
596 export function toPromise<T>(event: Event<T>): Promise<T> {
597 return new Promise(resolve => once(event)(resolve));
598 }
599
600 /**
601 * Creates an event out of a promise that fires once when the promise is
602 * resolved with the result of the promise or `undefined`.
603 */
604 export function fromPromise<T>(promise: Promise<T>): Event<T | undefined> {
605 const result = new Emitter<T | undefined>();
606
607 promise.then(res => {
608 result.fire(res);
609 }, () => {
610 result.fire(undefined);
611 }).finally(() => {
612 result.dispose();
613 });
614
615 return result.event;
616 }
617
618 /**
619 * A convenience function for forwarding an event to another emitter which
620 * improves readability.allows Event.forward(event, emitter) instead of `event(e => emitter.fire(e))`.
621 * @param from The event to forward.
622 * @param to The emitter to forward the event to.
623 * @example
624 * Event.forward(event, emitter);
625 * // equivalent to
626 * event(e => emitter.fire(e));
627 * // equivalent to
628 * event(emitter.fire, emitter);
629 */
630 export function forward<T>(from: Event<T>, to: Emitter<T>): IDisposable {
631 return from(e => to.fire(e));
632 }
633
634 /**
635 * Adds a listener to an event and calls the listener immediately with undefined as the event object.
636 *
637 * @example
638 * ```
639 * // Initialize the UI and update it when dataChangeEvent fires
640 * runAndSubscribe(dataChangeEvent, () => this._updateUI());
641 * ```
642 */
643 export function runAndSubscribe<T>(event: Event<T>, handler: (e: T) => any, initial: T): IDisposable;
644 export function runAndSubscribe<T>(event: Event<T>, handler: (e: T | undefined) => any): IDisposable;
645 export function runAndSubscribe<T>(event: Event<T>, handler: (e: T | undefined) => any, initial?: T): IDisposable {
646 handler(initial);
647 return event(e => handler(e));
648 }
649
650 class EmitterObserver<T> implements IObserver {
651
652 readonly emitter: Emitter<T>;
653
654 private _counter = 0;
655 private _hasChanged = false;
656
657 constructor(readonly _observable: IObservable<T, any>, store: DisposableStore | undefined) {
658 const options: EmitterOptions = {
659 onWillAddFirstListener: () => {
660 _observable.addObserver(this);
661 },
662 onDidRemoveLastListener: () => {
663 _observable.removeObserver(this);
664 }
665 };
666 if (!store) {
667 _addLeakageTraceLogic(options);
668 }
669 this.emitter = new Emitter<T>(options);
670 if (store) {
671 store.add(this.emitter);
672 }
673 }
674
675 beginUpdate<T>(_observable: IObservable<T, void>): void {
676 // assert(_observable === this.obs);
677 this._counter++;
678 }
679
680 handlePossibleChange<T>(_observable: IObservable<T, unknown>): void {
681 // assert(_observable === this.obs);
682 }
683
684 handleChange<T, TChange>(_observable: IObservable<T, TChange>, _change: TChange): void {
685 // assert(_observable === this.obs);
686 this._hasChanged = true;
687 }
688
689 endUpdate<T>(_observable: IObservable<T, void>): void {
690 // assert(_observable === this.obs);
691 this._counter--;
692 if (this._counter === 0) {
693 this._observable.reportChanges();
694 if (this._hasChanged) {
695 this._hasChanged = false;
696 this.emitter.fire(this._observable.get());
697 }
698 }
699 }
700 }
701
702 /**
703 * Creates an event emitter that is fired when the observable changes.
704 * Each listeners subscribes to the emitter.
705 */
706 export function fromObservable<T>(obs: IObservable<T, any>, store?: DisposableStore): Event<T> {
707 const observer = new EmitterObserver(obs, store);
708 return observer.emitter.event;
709 }
710
711 /**
712 * Each listener is attached to the observable directly.
713 */
714 export function fromObservableLight(observable: IObservable<any>): Event<void> {
715 return (listener, thisArgs, disposables) => {
716 let count = 0;
717 let didChange = false;
718 const observer: IObserver = {
719 beginUpdate() {
720 count++;
721 },
722 endUpdate() {
723 count--;
724 if (count === 0) {
725 observable.reportChanges();
726 if (didChange) {
727 didChange = false;
728 listener.call(thisArgs);
729 }
730 }
731 },
732 handlePossibleChange() {
733 // noop
734 },
735 handleChange() {
736 didChange = true;
737 }
738 };
739 observable.addObserver(observer);
740 observable.reportChanges();
741 const disposable = {
742 dispose() {
743 observable.removeObserver(observer);
744 }
745 };
746
747 if (disposables instanceof DisposableStore) {
748 disposables.add(disposable);
749 } else if (Array.isArray(disposables)) {
750 disposables.push(disposable);
751 }
752
753 return disposable;
754 };
755 }
756 }
757
758 export interface EmitterOptions {
759 /**
760 * Optional function that's called *before* the very first listener is added
761 */
762 onWillAddFirstListener?: Function;
763 /**
764 * Optional function that's called *after* the very first listener is added
765 */
766 onDidAddFirstListener?: Function;
767 /**
768 * Optional function that's called after a listener is added
769 */
770 onDidAddListener?: Function;
771 /**
772 * Optional function that's called *after* remove the very last listener
773 */
774 onDidRemoveLastListener?: Function;
775 /**
776 * Optional function that's called *before* a listener is removed
777 */
778 onWillRemoveListener?: Function;
779 /**
780 * Optional function that's called when a listener throws an error. Defaults to
781 * {@link onUnexpectedError}
782 */
783 onListenerError?: (e: any) => void;
784 /**
785 * Number of listeners that are allowed before assuming a leak. Default to
786 * a globally configured value
787 *
788 * @see setGlobalLeakWarningThreshold
789 */
790 leakWarningThreshold?: number;
791 /**
792 * Pass in a delivery queue, which is useful for ensuring
793 * in order event delivery across multiple emitters.
794 */
795 deliveryQueue?: EventDeliveryQueue;
796
797 /** ONLY enable this during development */
798 _profName?: string;
799 }
800
801
802 export class EventProfiling {
803
804 static readonly all = new Set<EventProfiling>();
805
806 private static _idPool = 0;
807
808 readonly name: string;
809 public listenerCount: number = 0;
810 public invocationCount = 0;
811 public elapsedOverall = 0;
812 public durations: number[] = [];
813
814 private _stopWatch?: StopWatch;
815
816 constructor(name: string) {
817 this.name = `${name}_${EventProfiling._idPool++}`;
818 EventProfiling.all.add(this);
819 }
820
821 start(listenerCount: number): void {
822 this._stopWatch = new StopWatch();
823 this.listenerCount = listenerCount;
824 }
825
826 stop(): void {
827 if (this._stopWatch) {
828 const elapsed = this._stopWatch.elapsed();
829 this.durations.push(elapsed);
830 this.elapsedOverall += elapsed;
831 this.invocationCount += 1;
832 this._stopWatch = undefined;
833 }
834 }
835 }
836
837 let _globalLeakWarningThreshold = -1;
838 export function setGlobalLeakWarningThreshold(n: number): IDisposable {
839 const oldValue = _globalLeakWarningThreshold;
840 _globalLeakWarningThreshold = n;
841 return {
842 dispose() {
843 _globalLeakWarningThreshold = oldValue;
844 }
845 };
846 }
847
848 class LeakageMonitor {
849
850 private static _idPool = 1;
851
852 private _stacks: Map<string, number> | undefined;
853 private _warnCountdown: number = 0;
854
855 constructor(
856 private readonly _errorHandler: (err: Error) => void,
857 readonly threshold: number,
858 readonly name: string = (LeakageMonitor._idPool++).toString(16).padStart(3, '0')
859 ) { }
860
861 dispose(): void {
862 this._stacks?.clear();
863 }
864
865 check(stack: Stacktrace, listenerCount: number): undefined | (() => void) {
866
867 const threshold = this.threshold;
868 if (threshold <= 0 || listenerCount < threshold) {
869 return undefined;
870 }
871
872 if (!this._stacks) {
873 this._stacks = new Map();
874 }
875 const count = (this._stacks.get(stack.value) || 0);
876 this._stacks.set(stack.value, count + 1);
877 this._warnCountdown -= 1;
878
879 if (this._warnCountdown <= 0) {
880 // only warn on first exceed and then every time the limit
881 // is exceeded by 50% again
882 this._warnCountdown = threshold * 0.5;
883
884 const [topStack, topCount] = this.getMostFrequentStack()!;
885 const message = `[${this.name}] potential listener LEAK detected, having ${listenerCount} listeners already. MOST frequent listener (${topCount}):`;
886 console.warn(message);
887 console.warn(topStack!);
888
889 const error = new ListenerLeakError(message, topStack);
890 this._errorHandler(error);
891 }
892
893 return () => {
894 const count = (this._stacks!.get(stack.value) || 0);
895 this._stacks!.set(stack.value, count - 1);
896 };
897 }
898
899 getMostFrequentStack(): [string, number] | undefined {
900 if (!this._stacks) {
901 return undefined;
902 }
903 let topStack: [string, number] | undefined;
904 let topCount: number = 0;
905 for (const [stack, count] of this._stacks) {
906 if (!topStack || topCount < count) {
907 topStack = [stack, count];
908 topCount = count;
909 }
910 }
911 return topStack;
912 }
913 }
914
915 class Stacktrace {
916
917 static create() {
918 const err = new Error();
919 return new Stacktrace(err.stack ?? '');
920 }
921
922 private constructor(readonly value: string) { }
923
924 print() {
925 console.warn(this.value.split('\n').slice(2).join('\n'));
926 }
927 }
928
929 // error that is logged when going over the configured listener threshold
930 export class ListenerLeakError extends Error {
931 constructor(message: string, stack: string) {
932 super(message);
933 this.name = 'ListenerLeakError';
934 this.stack = stack;
935 }
936 }
937
938 // SEVERE error that is logged when having gone way over the configured listener
939 // threshold so that the emitter refuses to accept more listeners
940 export class ListenerRefusalError extends Error {
941 constructor(message: string, stack: string) {
942 super(message);
943 this.name = 'ListenerRefusalError';
944 this.stack = stack;
945 }
946 }
947
948 let id = 0;
949 class UniqueContainer<T> {
950 stack?: Stacktrace;
951 public id = id++;
952 constructor(public readonly value: T) { }
953 }
954 const compactionThreshold = 2;
955
956 type ListenerContainer<T> = UniqueContainer<(data: T) => void>;
957 type ListenerOrListeners<T> = (ListenerContainer<T> | undefined)[] | ListenerContainer<T>;
958
959 const forEachListener = <T>(listeners: ListenerOrListeners<T>, fn: (c: ListenerContainer<T>) => void) => {
960 if (listeners instanceof UniqueContainer) {
961 fn(listeners);
962 } else {
963 for (let i = 0; i < listeners.length; i++) {
964 const l = listeners[i];
965 if (l) {
966 fn(l);
967 }
968 }
969 }
970 };
971
972
973 let _listenerFinalizers: FinalizationRegistry<string> | undefined;
974
975 if (_enableListenerGCedWarning) {
976 const leaks: string[] = [];
977
978 setInterval(() => {
979 if (leaks.length === 0) {
980 return;
981 }
982 console.warn('[LEAKING LISTENERS] GC\'ed these listeners that were NOT yet disposed:');
983 console.warn(leaks.join('\n'));
984 leaks.length = 0;
985 }, 3000);
986
987 _listenerFinalizers = new FinalizationRegistry(heldValue => {
988 if (typeof heldValue === 'string') {
989 leaks.push(heldValue);
990 }
991 });
992 }
993
994 /**
995 * The Emitter can be used to expose an Event to the public
996 * to fire it from the insides.
997 * Sample:
998 class Document {
999
1000 private readonly _onDidChange = new Emitter<(value:string)=>any>();
1001
1002 public onDidChange = this._onDidChange.event;
1003
1004 // getter-style
1005 // get onDidChange(): Event<(value:string)=>any> {
1006 // return this._onDidChange.event;
1007 // }
1008
1009 private _doIt() {
1010 //...
1011 this._onDidChange.fire(value);
1012 }
1013 }
1014 */
1015 export class Emitter<T> {
1016
1017 private readonly _options?: EmitterOptions;
1018 private readonly _leakageMon?: LeakageMonitor;
1019 private readonly _perfMon?: EventProfiling;
1020 private _disposed?: true;
1021 private _event?: Event<T>;
1022
1023 /**
1024 * A listener, or list of listeners. A single listener is the most common
1025 * for event emitters (#185789), so we optimize that special case to avoid
1026 * wrapping it in an array (just like Node.js itself.)
1027 *
1028 * A list of listeners never 'downgrades' back to a plain function if
1029 * listeners are removed, for two reasons:
1030 *
1031 * 1. That's complicated (especially with the deliveryQueue)
1032 * 2. A listener with >1 listener is likely to have >1 listener again at
1033 * some point, and swapping between arrays and functions may[citation needed]
1034 * introduce unnecessary work and garbage.
1035 *
1036 * The array listeners can be 'sparse', to avoid reallocating the array
1037 * whenever any listener is added or removed. If more than `1 / compactionThreshold`
1038 * of the array is empty, only then is it resized.
1039 */
1040 protected _listeners?: ListenerOrListeners<T>;
1041
1042 /**
1043 * Always to be defined if _listeners is an array. It's no longer a true
1044 * queue, but holds the dispatching 'state'. If `fire()` is called on an
1045 * emitter, any work left in the _deliveryQueue is finished first.
1046 */
1047 private _deliveryQueue?: EventDeliveryQueuePrivate;
1048 protected _size = 0;
1049
1050 constructor(options?: EmitterOptions) {
1051 this._options = options;
1052 this._leakageMon = (_globalLeakWarningThreshold > 0 || this._options?.leakWarningThreshold)
1053 ? new LeakageMonitor(options?.onListenerError ?? onUnexpectedError, this._options?.leakWarningThreshold ?? _globalLeakWarningThreshold) :
1054 undefined;
1055 this._perfMon = this._options?._profName ? new EventProfiling(this._options._profName) : undefined;
1056 this._deliveryQueue = this._options?.deliveryQueue as EventDeliveryQueuePrivate | undefined;
1057 }
1058
1059 dispose() {
1060 if (!this._disposed) {
1061 this._disposed = true;
1062
1063 // It is bad to have listeners at the time of disposing an emitter, it is worst to have listeners keep the emitter
1064 // alive via the reference that's embedded in their disposables. Therefore we loop over all remaining listeners and
1065 // unset their subscriptions/disposables. Looping and blaming remaining listeners is done on next tick because the
1066 // the following programming pattern is very popular:
1067 //
1068 // const someModel = this._disposables.add(new ModelObject()); // (1) create and register model
1069 // this._disposables.add(someModel.onDidChange(() => { ... }); // (2) subscribe and register model-event listener
1070 // ...later...
1071 // this._disposables.dispose(); disposes (1) then (2): don't warn after (1) but after the "overall dispose" is done
1072
1073 if (this._deliveryQueue?.current === this) {
1074 this._deliveryQueue.reset();
1075 }
1076 if (this._listeners) {
1077 if (_enableDisposeWithListenerWarning) {
1078 const listeners = this._listeners;
1079 queueMicrotask(() => {
1080 forEachListener(listeners, l => l.stack?.print());
1081 });
1082 }
1083
1084 this._listeners = undefined;
1085 this._size = 0;
1086 }
1087 this._options?.onDidRemoveLastListener?.();
1088 this._leakageMon?.dispose();
1089 }
1090 }
1091
1092 /**
1093 * For the public to allow to subscribe
1094 * to events from this Emitter
1095 */
1096 get event(): Event<T> {
1097 this._event ??= (callback: (e: T) => any, thisArgs?: any, disposables?: IDisposable[] | DisposableStore) => {
1098 if (this._leakageMon && this._size > this._leakageMon.threshold ** 2) {
1099 const message = `[${this._leakageMon.name}] REFUSES to accept new listeners because it exceeded its threshold by far (${this._size} vs ${this._leakageMon.threshold})`;
1100 console.warn(message);
1101
1102 const tuple = this._leakageMon.getMostFrequentStack() ?? ['UNKNOWN stack', -1];
1103 const error = new ListenerRefusalError(`${message}. HINT: Stack shows most frequent listener (${tuple[1]}-times)`, tuple[0]);
1104 const errorHandler = this._options?.onListenerError || onUnexpectedError;
1105 errorHandler(error);
1106
1107 return Disposable.None;
1108 }
1109
1110 if (this._disposed) {
1111 // todo: should we warn if a listener is added to a disposed emitter? This happens often
1112 return Disposable.None;
1113 }
1114
1115 if (thisArgs) {
1116 callback = callback.bind(thisArgs);
1117 }
1118
1119 const contained = new UniqueContainer(callback);
1120
1121 let removeMonitor: Function | undefined;
1122 let stack: Stacktrace | undefined;
1123 if (this._leakageMon && this._size >= Math.ceil(this._leakageMon.threshold * 0.2)) {
1124 // check and record this emitter for potential leakage
1125 contained.stack = Stacktrace.create();
1126 removeMonitor = this._leakageMon.check(contained.stack, this._size + 1);
1127 }
1128
1129 if (_enableDisposeWithListenerWarning) {
1130 contained.stack = stack ?? Stacktrace.create();
1131 }
1132
1133 if (!this._listeners) {
1134 this._options?.onWillAddFirstListener?.(this);
1135 this._listeners = contained;
1136 this._options?.onDidAddFirstListener?.(this);
1137 } else if (this._listeners instanceof UniqueContainer) {
1138 this._deliveryQueue ??= new EventDeliveryQueuePrivate();
1139 this._listeners = [this._listeners, contained];
1140 } else {
1141 this._listeners.push(contained);
1142 }
1143
1144 this._size++;
1145
1146
1147 const result = toDisposable(() => {
1148 _listenerFinalizers?.unregister(result);
1149 removeMonitor?.();
1150 this._removeListener(contained);
1151 });
1152 if (disposables instanceof DisposableStore) {
1153 disposables.add(result);
1154 } else if (Array.isArray(disposables)) {
1155 disposables.push(result);
1156 }
1157
1158 if (_listenerFinalizers) {
1159 const stack = new Error().stack!.split('\n').slice(2, 3).join('\n').trim();
1160 const match = /(file:|vscode-file:\/\/vscode-app)?(\/[^:]*:\d+:\d+)/.exec(stack);
1161 _listenerFinalizers.register(result, match?.[2] ?? stack, result);
1162 }
1163
1164 return result;
1165 };
1166
1167 return this._event;
1168 }
1169
1170 private _removeListener(listener: ListenerContainer<T>) {
1171 this._options?.onWillRemoveListener?.(this);
1172
1173 if (!this._listeners) {
1174 return; // expected if a listener gets disposed
1175 }
1176
1177 if (this._size === 1) {
1178 this._listeners = undefined;
1179 this._options?.onDidRemoveLastListener?.(this);
1180 this._size = 0;
1181 return;
1182 }
1183
1184 // size > 1 which requires that listeners be a list:
1185 const listeners = this._listeners as (ListenerContainer<T> | undefined)[];
1186
1187 const index = listeners.indexOf(listener);
1188 if (index === -1) {
1189 console.log('disposed?', this._disposed);
1190 console.log('size?', this._size);
1191 console.log('arr?', JSON.stringify(this._listeners));
1192 throw new Error('Attempted to dispose unknown listener');
1193 }
1194
1195 this._size--;
1196 listeners[index] = undefined;
1197
1198 const adjustDeliveryQueue = this._deliveryQueue!.current === this;
1199 if (this._size * compactionThreshold <= listeners.length) {
1200 let n = 0;
1201 for (let i = 0; i < listeners.length; i++) {
1202 if (listeners[i]) {
1203 listeners[n++] = listeners[i];
1204 } else if (adjustDeliveryQueue) {
1205 this._deliveryQueue!.end--;
1206 if (n < this._deliveryQueue!.i) {
1207 this._deliveryQueue!.i--;
1208 }
1209 }
1210 }
1211 listeners.length = n;
1212 }
1213 }
1214
1215 private _deliver(listener: undefined | UniqueContainer<(value: T) => void>, value: T) {
1216 if (!listener) {
1217 return;
1218 }
1219
1220 const errorHandler = this._options?.onListenerError || onUnexpectedError;
1221 if (!errorHandler) {
1222 listener.value(value);
1223 return;
1224 }
1225
1226 try {
1227 listener.value(value);
1228 } catch (e) {
1229 errorHandler(e);
1230 }
1231 }
1232
1233 /** Delivers items in the queue. Assumes the queue is ready to go. */
1234 private _deliverQueue(dq: EventDeliveryQueuePrivate) {
1235 const listeners = dq.current!._listeners! as (ListenerContainer<T> | undefined)[];
1236 while (dq.i < dq.end) {
1237 // important: dq.i is incremented before calling deliver() because it might reenter deliverQueue()
1238 this._deliver(listeners[dq.i++], dq.value as T);
1239 }
1240 dq.reset();
1241 }
1242
1243 /**
1244 * To be kept private to fire an event to
1245 * subscribers
1246 */
1247 fire(event: T): void {
1248 if (this._deliveryQueue?.current) {
1249 this._deliverQueue(this._deliveryQueue);
1250 this._perfMon?.stop(); // last fire() will have starting perfmon, stop it before starting the next dispatch
1251 }
1252
1253 this._perfMon?.start(this._size);
1254
1255 if (!this._listeners) {
1256 // no-op
1257 } else if (this._listeners instanceof UniqueContainer) {
1258 this._deliver(this._listeners, event);
1259 } else {
1260 const dq = this._deliveryQueue!;
1261 dq.enqueue(this, event, this._listeners.length);
1262 this._deliverQueue(dq);
1263 }
1264
1265 this._perfMon?.stop();
1266 }
1267
1268 hasListeners(): boolean {
1269 return this._size > 0;
1270 }
1271 }
1272
1273 export interface EventDeliveryQueue {
1274 _isEventDeliveryQueue: true;
1275 }
1276
1277 export const createEventDeliveryQueue = (): EventDeliveryQueue => new EventDeliveryQueuePrivate();
1278
1279 class EventDeliveryQueuePrivate implements EventDeliveryQueue {
1280 declare _isEventDeliveryQueue: true;
1281
1282 /**
1283 * Index in current's listener list.
1284 */
1285 public i = -1;
1286
1287 /**
1288 * The last index in the listener's list to deliver.
1289 */
1290 public end = 0;
1291
1292 /**
1293 * Emitter currently being dispatched on. Emitter._listeners is always an array.
1294 */
1295 public current?: Emitter<any>;
1296 /**
1297 * Currently emitting value. Defined whenever `current` is.
1298 */
1299 public value?: unknown;
1300
1301 public enqueue<T>(emitter: Emitter<T>, value: T, end: number) {
1302 this.i = 0;
1303 this.end = end;
1304 this.current = emitter;
1305 this.value = value;
1306 }
1307
1308 public reset() {
1309 this.i = this.end; // force any current emission loop to stop, mainly for during dispose
1310 this.current = undefined;
1311 this.value = undefined;
1312 }
1313 }
1314
1315 export interface IWaitUntil {
1316 token: CancellationToken;
1317 waitUntil(thenable: Promise<unknown>): void;
1318 }
1319
1320 export type IWaitUntilData<T> = Omit<Omit<T, 'waitUntil'>, 'token'>;
1321
1322 export class AsyncEmitter<T extends IWaitUntil> extends Emitter<T> {
1323
1324 private _asyncDeliveryQueue?: LinkedList<[(ev: T) => void, IWaitUntilData<T>]>;
1325
1326 async fireAsync(data: IWaitUntilData<T>, token: CancellationToken, promiseJoin?: (p: Promise<unknown>, listener: Function) => Promise<unknown>): Promise<void> {
1327 if (!this._listeners) {
1328 return;
1329 }
1330
1331 if (!this._asyncDeliveryQueue) {
1332 this._asyncDeliveryQueue = new LinkedList();
1333 }
1334
1335 forEachListener(this._listeners, listener => this._asyncDeliveryQueue!.push([listener.value, data]));
1336
1337 while (this._asyncDeliveryQueue.size > 0 && !token.isCancellationRequested) {
1338
1339 const [listener, data] = this._asyncDeliveryQueue.shift()!;
1340 const thenables: Promise<unknown>[] = [];
1341
1342 const event = <T>{
1343 ...data,
1344 token,
1345 waitUntil: (p: Promise<unknown>): void => {
1346 if (Object.isFrozen(thenables)) {
1347 throw new Error('waitUntil can NOT be called asynchronous');
1348 }
1349 if (promiseJoin) {
1350 p = promiseJoin(p, listener);
1351 }
1352 thenables.push(p);
1353 }
1354 };
1355
1356 try {
1357 listener(event);
1358 } catch (e) {
1359 onUnexpectedError(e);
1360 continue;
1361 }
1362
1363 // freeze thenables-collection to enforce sync-calls to
1364 // wait until and then wait for all thenables to resolve
1365 Object.freeze(thenables);
1366
1367 await Promise.allSettled(thenables).then(values => {
1368 for (const value of values) {
1369 if (value.status === 'rejected') {
1370 onUnexpectedError(value.reason);
1371 }
1372 }
1373 });
1374 }
1375 }
1376 }
1377
1378
1379 export class PauseableEmitter<T> extends Emitter<T> {
1380
1381 private _isPaused = 0;
1382 protected _eventQueue = new LinkedList<T>();
1383 private _mergeFn?: (input: T[]) => T;
1384
1385 public get isPaused(): boolean {
1386 return this._isPaused !== 0;
1387 }
1388
1389 constructor(options?: EmitterOptions & { merge?: (input: T[]) => T }) {
1390 super(options);
1391 this._mergeFn = options?.merge;
1392 }
1393
1394 pause(): void {
1395 this._isPaused++;
1396 }
1397
1398 resume(): void {
1399 if (this._isPaused !== 0 && --this._isPaused === 0) {
1400 if (this._mergeFn) {
1401 // use the merge function to create a single composite
1402 // event. make a copy in case firing pauses this emitter
1403 if (this._eventQueue.size > 0) {
1404 const events = Array.from(this._eventQueue);
1405 this._eventQueue.clear();
1406 super.fire(this._mergeFn(events));
1407 }
1408
1409 } else {
1410 // no merging, fire each event individually and test
1411 // that this emitter isn't paused halfway through
1412 while (!this._isPaused && this._eventQueue.size !== 0) {
1413 super.fire(this._eventQueue.shift()!);
1414 }
1415 }
1416 }
1417 }
1418
1419 override fire(event: T): void {
1420 if (this._size) {
1421 if (this._isPaused !== 0) {
1422 this._eventQueue.push(event);
1423 } else {
1424 super.fire(event);
1425 }
1426 }
1427 }
1428 }
1429
1430 export class DebounceEmitter<T> extends PauseableEmitter<T> {
1431
1432 private readonly _delay: number;
1433 private _handle: any | undefined;
1434
1435 constructor(options: EmitterOptions & { merge: (input: T[]) => T; delay?: number }) {
1436 super(options);
1437 this._delay = options.delay ?? 100;
1438 }
1439
1440 override fire(event: T): void {
1441 if (!this._handle) {
1442 this.pause();
1443 this._handle = setTimeout(() => {
1444 this._handle = undefined;
1445 this.resume();
1446 }, this._delay);
1447 }
1448 super.fire(event);
1449 }
1450 }
1451
1452 /**
1453 * An emitter which queue all events and then process them at the
1454 * end of the event loop.
1455 */
1456 export class MicrotaskEmitter<T> extends Emitter<T> {
1457 private _queuedEvents: T[] = [];
1458 private _mergeFn?: (input: T[]) => T;
1459
1460 constructor(options?: EmitterOptions & { merge?: (input: T[]) => T }) {
1461 super(options);
1462 this._mergeFn = options?.merge;
1463 }
1464 override fire(event: T): void {
1465
1466 if (!this.hasListeners()) {
1467 return;
1468 }
1469
1470 this._queuedEvents.push(event);
1471 if (this._queuedEvents.length === 1) {
1472 queueMicrotask(() => {
1473 if (this._mergeFn) {
1474 super.fire(this._mergeFn(this._queuedEvents));
1475 } else {
1476 this._queuedEvents.forEach(e => super.fire(e));
1477 }
1478 this._queuedEvents = [];
1479 });
1480 }
1481 }
1482 }
1483
1484 /**
1485 * An event emitter that multiplexes many events into a single event.
1486 *
1487 * @example Listen to the `onData` event of all `Thing`s, dynamically adding and removing `Thing`s
1488 * to the multiplexer as needed.
1489 *
1490 * ```typescript
1491 * const anythingDataMultiplexer = new EventMultiplexer<{ data: string }>();
1492 *
1493 * const thingListeners = DisposableMap<Thing, IDisposable>();
1494 *
1495 * thingService.onDidAddThing(thing => {
1496 * thingListeners.set(thing, anythingDataMultiplexer.add(thing.onData);
1497 * });
1498 * thingService.onDidRemoveThing(thing => {
1499 * thingListeners.deleteAndDispose(thing);
1500 * });
1501 *
1502 * anythingDataMultiplexer.event(e => {
1503 * console.log('Something fired data ' + e.data)
1504 * });
1505 * ```
1506 */
1507 export class EventMultiplexer<T> implements IDisposable {
1508
1509 private readonly emitter: Emitter<T>;
1510 private hasListeners = false;
1511 private events: { event: Event<T>; listener: IDisposable | null }[] = [];
1512
1513 constructor() {
1514 this.emitter = new Emitter<T>({
1515 onWillAddFirstListener: () => this.onFirstListenerAdd(),
1516 onDidRemoveLastListener: () => this.onLastListenerRemove()
1517 });
1518 }
1519
1520 get event(): Event<T> {
1521 return this.emitter.event;
1522 }
1523
1524 add(event: Event<T>): IDisposable {
1525 const e = { event: event, listener: null };
1526 this.events.push(e);
1527
1528 if (this.hasListeners) {
1529 this.hook(e);
1530 }
1531
1532 const dispose = () => {
1533 if (this.hasListeners) {
1534 this.unhook(e);
1535 }
1536
1537 const idx = this.events.indexOf(e);
1538 this.events.splice(idx, 1);
1539 };
1540
1541 return toDisposable(createSingleCallFunction(dispose));
1542 }
1543
1544 private onFirstListenerAdd(): void {
1545 this.hasListeners = true;
1546 this.events.forEach(e => this.hook(e));
1547 }
1548
1549 private onLastListenerRemove(): void {
1550 this.hasListeners = false;
1551 this.events.forEach(e => this.unhook(e));
1552 }
1553
1554 private hook(e: { event: Event<T>; listener: IDisposable | null }): void {
1555 e.listener = e.event(r => this.emitter.fire(r));
1556 }
1557
1558 private unhook(e: { event: Event<T>; listener: IDisposable | null }): void {
1559 e.listener?.dispose();
1560 e.listener = null;
1561 }
1562
1563 dispose(): void {
1564 this.emitter.dispose();
1565
1566 for (const e of this.events) {
1567 e.listener?.dispose();
1568 }
1569 this.events = [];
1570 }
1571 }
1572
1573 export interface IDynamicListEventMultiplexer<TEventType> extends IDisposable {
1574 readonly event: Event<TEventType>;
1575 }
1576 export class DynamicListEventMultiplexer<TItem, TEventType> implements IDynamicListEventMultiplexer<TEventType> {
1577 private readonly _store = new DisposableStore();
1578
1579 readonly event: Event<TEventType>;
1580
1581 constructor(
1582 items: TItem[],
1583 onAddItem: Event<TItem>,
1584 onRemoveItem: Event<TItem>,
1585 getEvent: (item: TItem) => Event<TEventType>
1586 ) {
1587 const multiplexer = this._store.add(new EventMultiplexer<TEventType>());
1588 const itemListeners = this._store.add(new DisposableMap<TItem, IDisposable>());
1589
1590 function addItem(instance: TItem) {
1591 itemListeners.set(instance, multiplexer.add(getEvent(instance)));
1592 }
1593
1594 // Existing items
1595 for (const instance of items) {
1596 addItem(instance);
1597 }
1598
1599 // Added items
1600 this._store.add(onAddItem(instance => {
1601 addItem(instance);
1602 }));
1603
1604 // Removed items
1605 this._store.add(onRemoveItem(instance => {
1606 itemListeners.deleteAndDispose(instance);
1607 }));
1608
1609 this.event = multiplexer.event;
1610 }
1611
1612 dispose() {
1613 this._store.dispose();
1614 }
1615 }
1616
1617 /**
1618 * The EventBufferer is useful in situations in which you want
1619 * to delay firing your events during some code.
1620 * You can wrap that code and be sure that the event will not
1621 * be fired during that wrap.
1622 *
1623 * ```
1624 * const emitter: Emitter;
1625 * const delayer = new EventDelayer();
1626 * const delayedEvent = delayer.wrapEvent(emitter.event);
1627 *
1628 * delayedEvent(console.log);
1629 *
1630 * delayer.bufferEvents(() => {
1631 * emitter.fire(); // event will not be fired yet
1632 * });
1633 *
1634 * // event will only be fired at this point
1635 * ```
1636 */
1637 export class EventBufferer {
1638
1639 private data: { buffers: Function[] }[] = [];
1640
1641 wrapEvent<T>(event: Event<T>): Event<T>;
1642 wrapEvent<T>(event: Event<T>, reduce: (last: T | undefined, event: T) => T): Event<T>;
1643 wrapEvent<T, O>(event: Event<T>, reduce: (last: O | undefined, event: T) => O, initial: O): Event<O>;
1644 wrapEvent<T, O>(event: Event<T>, reduce?: (last: T | O | undefined, event: T) => T | O, initial?: O): Event<O | T> {
1645 return (listener, thisArgs?, disposables?) => {
1646 return event(i => {
1647 const data = this.data[this.data.length - 1];
1648
1649 // Non-reduce scenario
1650 if (!reduce) {
1651 // Buffering case
1652 if (data) {
1653 data.buffers.push(() => listener.call(thisArgs, i));
1654 } else {
1655 // Not buffering case
1656 listener.call(thisArgs, i);
1657 }
1658 return;
1659 }
1660
1661 // Reduce scenario
1662 const reduceData = data as typeof data & {
1663 /**
1664 * The accumulated items that will be reduced.
1665 */
1666 items?: T[];
1667 /**
1668 * The reduced result cached to be shared with other listeners.
1669 */
1670 reducedResult?: T | O;
1671 };
1672
1673 // Not buffering case
1674 if (!reduceData) {
1675 // TODO: Is there a way to cache this reduce call for all listeners?
1676 listener.call(thisArgs, reduce(initial, i));
1677 return;
1678 }
1679
1680 // Buffering case
1681 reduceData.items ??= [];
1682 reduceData.items.push(i);
1683 if (reduceData.buffers.length === 0) {
1684 // Include a single buffered function that will reduce all events when we're done buffering events
1685 data.buffers.push(() => {
1686 // cache the reduced result so that the value can be shared across all listeners
1687 reduceData.reducedResult ??= initial
1688 ? reduceData.items!.reduce(reduce as (last: O | undefined, event: T) => O, initial)
1689 : reduceData.items!.reduce(reduce as (last: T | undefined, event: T) => T);
1690 listener.call(thisArgs, reduceData.reducedResult);
1691 });
1692 }
1693 }, undefined, disposables);
1694 };
1695 }
1696
1697 bufferEvents<R = void>(fn: () => R): R {
1698 const data = { buffers: new Array<Function>() };
1699 this.data.push(data);
1700 const r = fn();
1701 this.data.pop();
1702 data.buffers.forEach(flush => flush());
1703 return r;
1704 }
1705 }
1706
1707 /**
1708 * A Relay is an event forwarder which functions as a replugabble event pipe.
1709 * Once created, you can connect an input event to it and it will simply forward
1710 * events from that input event through its own `event` property. The `input`
1711 * can be changed at any point in time.
1712 */
1713 export class Relay<T> implements IDisposable {
1714
1715 private listening = false;
1716 private inputEvent: Event<T> = Event.None;
1717 private inputEventListener: IDisposable = Disposable.None;
1718
1719 private readonly emitter = new Emitter<T>({
1720 onDidAddFirstListener: () => {
1721 this.listening = true;
1722 this.inputEventListener = this.inputEvent(this.emitter.fire, this.emitter);
1723 },
1724 onDidRemoveLastListener: () => {
1725 this.listening = false;
1726 this.inputEventListener.dispose();
1727 }
1728 });
1729
1730 readonly event: Event<T> = this.emitter.event;
1731
1732 set input(event: Event<T>) {
1733 this.inputEvent = event;
1734
1735 if (this.listening) {
1736 this.inputEventListener.dispose();
1737 this.inputEventListener = event(this.emitter.fire, this.emitter);
1738 }
1739 }
1740
1741 dispose() {
1742 this.inputEventListener.dispose();
1743 this.emitter.dispose();
1744 }
1745 }
1746
1747 export interface IValueWithChangeEvent<T> {
1748 readonly onDidChange: Event<void>;
1749 get value(): T;
1750 }
1751
1752 export class ValueWithChangeEvent<T> implements IValueWithChangeEvent<T> {
1753 public static const<T>(value: T): IValueWithChangeEvent<T> {
1754 return new ConstValueWithChangeEvent(value);
1755 }
1756
1757 private readonly _onDidChange = new Emitter<void>();
1758 readonly onDidChange: Event<void> = this._onDidChange.event;
1759
1760 constructor(private _value: T) { }
1761
1762 get value(): T {
1763 return this._value;
1764 }
1765
1766 set value(value: T) {
1767 if (value !== this._value) {
1768 this._value = value;
1769 this._onDidChange.fire(undefined);
1770 }
1771 }
1772 }
1773
1774 class ConstValueWithChangeEvent<T> implements IValueWithChangeEvent<T> {
1775 public readonly onDidChange: Event<void> = Event.None;
1776
1777 constructor(readonly value: T) { }
1778 }