diff --git a/src/system/event.ts b/src/system/event.ts index 84f1f94..72ecf8c 100644 --- a/src/system/event.ts +++ b/src/system/event.ts @@ -65,8 +65,10 @@ export function promisifyDeferred( let cancel: ((reason?: any) => void) | undefined; let disposable: Disposable; + let pending = true; const promise = new Promise((resolve, reject) => { cancel = () => { + pending = false; cancel = undefined; reject(); }; @@ -74,7 +76,9 @@ export function promisifyDeferred( disposable = event(async (value: T) => { try { await executor(value, resolve, reject); + pending = false; } catch (ex) { + pending = false; reject(ex); } }); @@ -90,6 +94,9 @@ export function promisifyDeferred( ); return { + get pending() { + return pending; + }, promise: promise, cancel: () => cancel?.(), }; diff --git a/src/system/promise.ts b/src/system/promise.ts index 120ce61..fa208c0 100644 --- a/src/system/promise.ts +++ b/src/system/promise.ts @@ -126,16 +126,28 @@ export function cancellable( } export interface Deferred { - promise: Promise; + readonly pending: boolean; + readonly promise: Promise; fulfill: (value: T) => void; cancel(): void; } export function defer(): Deferred { - const deferred: Deferred = { promise: undefined!, fulfill: undefined!, cancel: undefined! }; + const deferred: Mutable> = { + pending: true, + promise: undefined!, + fulfill: undefined!, + cancel: undefined!, + }; deferred.promise = new Promise((resolve, reject) => { - deferred.fulfill = resolve; - deferred.cancel = reject; + deferred.fulfill = function (value) { + deferred.pending = false; + return resolve(value); + }; + deferred.cancel = function () { + deferred.pending = false; + reject(); + }; }); return deferred; }