Files
yaak-mountain-loop/apps/yaak-proxy/lib/unsubscribe.ts
T
2026-10-30 00:41:00 -07:00

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,
};
}