2026-05-06 21:34:13 +08:00
|
|
|
type RuntimeInvalidationReason = 'account-health' | 'publish-task-lease'
|
2026-04-20 17:16:15 +08:00
|
|
|
|
|
|
|
|
type RuntimeInvalidationListener = (event: {
|
2026-05-01 20:39:09 +08:00
|
|
|
reason: RuntimeInvalidationReason
|
|
|
|
|
at: number
|
|
|
|
|
accountId?: string
|
2026-05-06 21:34:13 +08:00
|
|
|
taskId?: string
|
2026-05-01 20:39:09 +08:00
|
|
|
}) => void
|
2026-04-20 17:16:15 +08:00
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
const listeners = new Set<RuntimeInvalidationListener>()
|
2026-04-20 17:16:15 +08:00
|
|
|
|
2026-04-27 21:33:55 +08:00
|
|
|
export function emitRuntimeInvalidated(
|
|
|
|
|
reason: RuntimeInvalidationReason,
|
2026-05-06 21:34:13 +08:00
|
|
|
details: { accountId?: string; taskId?: string } = {},
|
2026-04-27 21:33:55 +08:00
|
|
|
): 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-05-01 20:39:09 +08:00
|
|
|
} as const
|
2026-04-20 17:16:15 +08:00
|
|
|
|
|
|
|
|
for (const listener of listeners) {
|
|
|
|
|
try {
|
2026-05-01 20:39:09 +08:00
|
|
|
listener(event)
|
2026-04-20 17:16:15 +08:00
|
|
|
} catch (error) {
|
2026-05-01 20:39:09 +08:00
|
|
|
console.warn('[desktop-runtime] invalidation listener failed', {
|
2026-04-20 17:16:15 +08:00
|
|
|
reason,
|
|
|
|
|
message: error instanceof Error ? error.message : String(error),
|
2026-05-01 20:39:09 +08:00
|
|
|
})
|
2026-04-20 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function onRuntimeInvalidated(listener: RuntimeInvalidationListener): () => void {
|
2026-05-01 20:39:09 +08:00
|
|
|
listeners.add(listener)
|
2026-04-20 17:16:15 +08:00
|
|
|
return () => {
|
2026-05-01 20:39:09 +08:00
|
|
|
listeners.delete(listener)
|
|
|
|
|
}
|
2026-04-20 17:16:15 +08:00
|
|
|
}
|