From 2cb4262ba6210929a753af1c9f67bb1ec6ff91a6 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Thu, 2 Nov 2023 11:23:27 -0400 Subject: [PATCH] Removes `disposables` from event wrappers - Avoid confusion on usage - Clarifies usage on `weakEvent` which is completely different than `disposables` --- src/eventBus.ts | 8 +------ src/system/event.ts | 66 +++++++++++++++++++++++------------------------------ 2 files changed, 30 insertions(+), 44 deletions(-) diff --git a/src/eventBus.ts b/src/eventBus.ts index 515e0a8..4819f77 100644 --- a/src/eventBus.ts +++ b/src/eventBus.ts @@ -79,12 +79,7 @@ export class EventBus implements Disposable { return _cachedEventArgs.get(name) as CacheableEventsMapping[T] | undefined; } - on( - name: T, - handler: (e: EventBusEvent) => void, - thisArgs?: unknown, - disposables?: Disposable[], - ) { + on(name: T, handler: (e: EventBusEvent) => void, thisArgs?: unknown) { return this._emitter.event( // eslint-disable-next-line prefer-arrow-callback function (e) { @@ -92,7 +87,6 @@ export class EventBus implements Disposable { handler.call(thisArgs, e as EventBusEvent); }, thisArgs, - disposables, ); } } diff --git a/src/system/event.ts b/src/system/event.ts index e16165d..314228e 100644 --- a/src/system/event.ts +++ b/src/system/event.ts @@ -3,15 +3,11 @@ import { Disposable } from 'vscode'; import type { Deferred } from './promise'; export function once(event: Event): Event { - return (listener: (e: T) => unknown, thisArgs?: unknown, disposables?: Disposable[]) => { - const result = event( - e => { - result.dispose(); - return listener.call(thisArgs, e); - }, - null, - disposables, - ); + return (listener: (e: T) => unknown, thisArgs?: unknown) => { + const result = event(e => { + result.dispose(); + return listener.call(thisArgs, e); + }); return result; }; @@ -22,17 +18,13 @@ export function promisify(event: Event): Promise { } export function until(event: Event, predicate: (e: T) => boolean): Event { - return (listener: (e: T) => unknown, thisArgs?: unknown, disposables?: Disposable[]) => { - const result = event( - e => { - if (predicate(e)) { - result.dispose(); - } - return listener.call(thisArgs, e); - }, - null, - disposables, - ); + return (listener: (e: T) => unknown, thisArgs?: unknown) => { + const result = event(e => { + if (predicate(e)) { + result.dispose(); + } + return listener.call(thisArgs, e); + }); return result; }; @@ -109,25 +101,25 @@ export function weakEvent( event: Event, listener: (e: T) => any, thisArg: U, - disposables?: Disposable[], + alsoDisposeOnReleaseOrDispose?: Disposable[], ): Disposable { const ref = new WeakRef(thisArg); - const disposable = event( - (e: T) => { - const obj = ref.deref(); - if (obj != null) { - listener.call(obj, e); - } else { - disposable.dispose(); - disposables?.forEach(d => void d.dispose()); - } - }, - null, - disposables, - ); - - if (disposables == null) return disposable; + let disposable: Disposable; - return Disposable.from(disposable, ...disposables); + const d = event((e: T) => { + const obj = ref.deref(); + if (obj != null) { + listener.call(obj, e); + } else { + disposable.dispose(); + } + }); + + if (alsoDisposeOnReleaseOrDispose == null) { + disposable = d; + } else { + disposable = Disposable.from(d, ...alsoDisposeOnReleaseOrDispose); + } + return disposable; }