mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-11-13 12:13:42 +01:00
26 lines
650 B
TypeScript
26 lines
650 B
TypeScript
/**
|
|
* An unsubscribe for a subscription that is still being set up. Called before the setup
|
|
* resolves, it unsubscribes as soon as it does; `active` says whether to still deliver events.
|
|
*/
|
|
export function unsubscribeWhenReady(subscription: Promise<() => void>): {
|
|
unsubscribe: () => void;
|
|
active: () => boolean;
|
|
} {
|
|
let unsub: (() => void) | null = null;
|
|
let cancelled = false;
|
|
subscription
|
|
.then((fn) => {
|
|
if (cancelled) fn();
|
|
else unsub = fn;
|
|
})
|
|
.catch(console.error);
|
|
return {
|
|
unsubscribe: () => {
|
|
cancelled = true;
|
|
unsub?.();
|
|
unsub = null;
|
|
},
|
|
active: () => !cancelled,
|
|
};
|
|
}
|