瀏覽代碼

Removes `disposables` from event wrappers

- Avoid confusion on usage
 - Clarifies usage on `weakEvent` which is completely different than `disposables`
main
Eric Amodio 1 年之前
父節點
當前提交
2cb4262ba6
共有 2 個檔案被更改,包括 30 行新增44 行删除
  1. +1
    -7
      src/eventBus.ts
  2. +29
    -37
      src/system/event.ts

+ 1
- 7
src/eventBus.ts 查看文件

@ -79,12 +79,7 @@ export class EventBus implements Disposable {
return _cachedEventArgs.get(name) as CacheableEventsMapping[T] | undefined;
}
on<T extends keyof EventsMapping>(
name: T,
handler: (e: EventBusEvent<T>) => void,
thisArgs?: unknown,
disposables?: Disposable[],
) {
on<T extends keyof EventsMapping>(name: T, handler: (e: EventBusEvent<T>) => 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<T>);
},
thisArgs,
disposables,
);
}
}

+ 29
- 37
src/system/event.ts 查看文件

@ -3,15 +3,11 @@ import { Disposable } from 'vscode';
import type { Deferred } from './promise';
export function once<T>(event: Event<T>): Event<T> {
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<T>(event: Event<T>, predicate: (e: T) => boolean): Event<T> {
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<T>,
listener: (e: T) => any,
thisArg: U,
disposables?: Disposable[],
alsoDisposeOnReleaseOrDispose?: Disposable[],
): Disposable {
const ref = new WeakRef<U>(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;
}

Loading…
取消
儲存