35 lines
817 B
TypeScript
35 lines
817 B
TypeScript
|
|
type RuntimeInvalidationReason = "account-health";
|
||
|
|
|
||
|
|
type RuntimeInvalidationListener = (event: {
|
||
|
|
reason: RuntimeInvalidationReason;
|
||
|
|
at: number;
|
||
|
|
}) => void;
|
||
|
|
|
||
|
|
const listeners = new Set<RuntimeInvalidationListener>();
|
||
|
|
|
||
|
|
export function emitRuntimeInvalidated(reason: RuntimeInvalidationReason): void {
|
||
|
|
const event = {
|
||
|
|
reason,
|
||
|
|
at: Date.now(),
|
||
|
|
} as const;
|
||
|
|
|
||
|
|
for (const listener of listeners) {
|
||
|
|
try {
|
||
|
|
listener(event);
|
||
|
|
} catch (error) {
|
||
|
|
console.warn("[desktop-runtime] invalidation listener failed", {
|
||
|
|
reason,
|
||
|
|
message: error instanceof Error ? error.message : String(error),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function onRuntimeInvalidated(listener: RuntimeInvalidationListener): () => void {
|
||
|
|
listeners.add(listener);
|
||
|
|
return () => {
|
||
|
|
listeners.delete(listener);
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|