Files
SnowLuma/packages/core/src/notifications/debounce.ts
T
8e24995fc3 feat(core): add process facts to ordered account notifications (#539)
* feat(core): carry hook process status in notifications

`offline` alone cannot tell an external supervisor whether the QQ session dropped (log back in) or the process is gone (restart the client). Resolve the hook process behind a UIN at notify time and expose `status`, `pid` and `error` as template variables; a failing lookup degrades to empty values so delivery is never blocked.

The default webhook/email templates gain a process-status line; existing channels keep their stored template untouched.

* fix(core): preserve notification lifecycle ordering and simplify state

---------

Co-authored-by: zhngzhu <215231231+zhngzhu@users.noreply.github.com>
Co-authored-by: 手瓜一十雪 <nanaeonn@outlook.com>
2026-10-18 11:13:58 +08:00

56 lines
1.7 KiB
TypeScript

// Offline must survive the debounce window before it can emit a recovery pair.
// The manager owns timers and delivery; this machine only decides transitions.
import type { NotificationEvent } from './config';
export type DebounceDecision =
| { kind: 'none' }
| { kind: 'schedule'; delayMs: number }
| { kind: 'cancel' }
| { kind: 'emit'; event: NotificationEvent };
export class DebounceMachine {
private readonly states = new Map<string, 'pending' | 'offline'>();
/** Account went offline. `debounceSeconds` is read fresh from config so a
* reload takes effect on the next transition. */
onOffline(uin: string, debounceSeconds: number): DebounceDecision {
if (this.states.has(uin)) {
return { kind: 'none' };
}
if (!Number.isFinite(debounceSeconds) || debounceSeconds <= 0) {
this.states.set(uin, 'offline');
return { kind: 'emit', event: 'offline' };
}
this.states.set(uin, 'pending');
return { kind: 'schedule', delayMs: debounceSeconds * 1000 };
}
/** Account came online. */
onOnline(uin: string): DebounceDecision {
const state = this.states.get(uin);
this.states.delete(uin);
if (state === 'pending') {
return { kind: 'cancel' };
}
if (state === 'offline') {
return { kind: 'emit', event: 'online' };
}
return { kind: 'none' };
}
/** The offline debounce window elapsed (the armed timer fired). */
onTimerElapsed(uin: string): DebounceDecision {
if (this.states.get(uin) !== 'pending') {
return { kind: 'none' };
}
this.states.set(uin, 'offline');
return { kind: 'emit', event: 'offline' };
}
}