2026-04-20 17:16:15 +08:00
|
|
|
type RuntimeInvalidationReason = "account-health";
|
|
|
|
|
|
|
|
|
|
type RuntimeInvalidationListener = (event: {
|
|
|
|
|
reason: RuntimeInvalidationReason;
|
|
|
|
|
at: number;
|
2026-04-27 21:33:55 +08:00
|
|
|
accountId?: string;
|
2026-04-20 17:16:15 +08:00
|
|
|
}) => void;
|
|
|
|
|
|
|
|
|
|
const listeners = new Set<RuntimeInvalidationListener>();
|
|
|
|
|
|
2026-04-27 21:33:55 +08:00
|
|
|
export function emitRuntimeInvalidated(
|
|
|
|
|
reason: RuntimeInvalidationReason,
|
|
|
|
|
details: { accountId?: string } = {},
|
|
|
|
|
): void {
|
2026-04-20 17:16:15 +08:00
|
|
|
const event = {
|
|
|
|
|
reason,
|
|
|
|
|
at: Date.now(),
|
2026-04-27 21:33:55 +08:00
|
|
|
...details,
|
2026-04-20 17:16:15 +08:00
|
|
|
} 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);
|
|
|
|
|
};
|
|
|
|
|
}
|