Open History to see the last commit
1interface DelayedActivationOptions<TKey> {
2 delayMs: number
3 canActivate: (key: TKey) => boolean
4 onChange: (key: TKey, active: boolean) => void
5}
6β
7interface PendingActivation {
8 phase: 'pending'
9 deadline: number
10}
11β
12interface ActiveActivation {
13 phase: 'active'
14}
15β
16type Activation = PendingActivation | ActiveActivation
17β
18interface DelayedActivationController<TKey> {
19 dispose: () => void
20 isActive: (key: TKey) => boolean
21 observe: (key: TKey, requested: boolean) => void
22 remove: (key: TKey) => void
23 removeWhere: (predicate: (key: TKey) => boolean) => void
24}
25β
26/**
27 * Delays activation while keeping deactivation immediate.
28 *
29 * Repeated positive observations preserve the original activation deadline.
30 * Removing a pending or active key is silent and is intended for lifecycle
31 * cleanup where the consumer is already removing the corresponding state.
32 */
33export function createDelayedActivationController<TKey>({
34 delayMs,
35 canActivate,
36 onChange,
37}: DelayedActivationOptions<TKey>): DelayedActivationController<TKey> {
38 const activations = new Map<TKey, Activation>()
39 let disposed = false
40 let scheduledDeadline: number | undefined
41 let activationTimer: ReturnType<typeof setTimeout> | undefined
42β
43 const clearActivationTimer = (): void => {
44 if (activationTimer !== undefined) clearTimeout(activationTimer)
45 activationTimer = undefined
46 scheduledDeadline = undefined
47 }
48β
49 const scheduleNextActivation = (): void => {
50 if (disposed) {
51 clearActivationTimer()
52 return
53 }
54β
55 let nextDeadline: number | undefined
56 for (const activation of activations.values()) {
57 if (activation.phase !== 'pending') continue
58 if (nextDeadline === undefined || activation.deadline < nextDeadline) {
59 nextDeadline = activation.deadline
60 }
61 }
62β
63 if (nextDeadline === undefined) {
64 clearActivationTimer()
65 return
66 }
67 if (activationTimer !== undefined && scheduledDeadline === nextDeadline) {
68 return
69 }
70β
71 clearActivationTimer()
72 scheduledDeadline = nextDeadline
73 activationTimer = setTimeout(
74 () => {
75 activationTimer = undefined
76 scheduledDeadline = undefined
77 const now = Date.now()
78 const activatedKeys: Array<TKey> = []
79β
80 for (const [key, activation] of activations) {
81 if (activation.phase !== 'pending' || activation.deadline > now) {
82 continue
83 }
84β
85 if (!canActivate(key)) {
86 activations.delete(key)
87 continue
88 }
89β
90 activations.set(key, { phase: 'active' })
91 activatedKeys.push(key)
92 }
93β
94 scheduleNextActivation()
95 for (const key of activatedKeys) onChange(key, true)
96 },
97 Math.max(0, nextDeadline - Date.now()),
98 )
99 }
100β
101 const remove = (key: TKey): void => {
102 const activation = activations.get(key)
103 if (!activation) return
104β
105 activations.delete(key)
106 if (activation.phase === 'pending') scheduleNextActivation()
107 }
108β
109 const observe = (key: TKey, requested: boolean): void => {
110 if (disposed) return
111β
112 const current = activations.get(key)
113β
114 if (!requested) {
115 if (!current) return
116β
117 remove(key)
118 if (current.phase === 'active') onChange(key, false)
119 return
120 }
121β
122 if (current) return
123β
124 activations.set(key, {
125 phase: 'pending',
126 deadline: Date.now() + delayMs,
127 })
128 scheduleNextActivation()
129 }
130β
131 return {
132 dispose: () => {
133 if (disposed) return
134 disposed = true
135 clearActivationTimer()
136 activations.clear()
137 },
138 isActive: (key) => activations.get(key)?.phase === 'active',
139 observe,
140 remove,
141 removeWhere: (predicate) => {
142 let removedPending = false
143 for (const [key] of activations) {
144 if (!predicate(key)) continue
145 if (activations.get(key)?.phase === 'pending') removedPending = true
146 activations.delete(key)
147 }
148 if (removedPending) scheduleNextActivation()
149 },
150 }
151}