-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathuse-stable-flag.ts
More file actions
123 lines (112 loc) · 3.5 KB
/
Copy pathuse-stable-flag.ts
File metadata and controls
123 lines (112 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { useEffect, useRef, useState } from 'react'
export interface StableFlagOptions {
/**
* Time `value` must stay continuously true before the flag turns on. Suppresses
* brief flashes for blips that heal within the window. Defaults to `0` (no delay).
*/
delayMs?: number
/**
* Minimum time the flag stays on once shown, even if `value` clears immediately
* after. Prevents a flash-and-vanish when `value` is true just past `delayMs`.
* Defaults to `0` (clears as soon as `value` does).
*/
minVisibleMs?: number
}
/**
* Framework-agnostic state machine behind {@link useStableFlag}. Extracted so the
* anti-flicker timing can be unit-tested with fake timers without a DOM. Relies on
* the ambient `setTimeout`/`clearTimeout`/`Date.now`, which fake timers replace.
*
* `onChange` fires whenever the smoothed flag flips. `setValue` is idempotent — it
* is safe to feed it the same value repeatedly (e.g. from React effect re-runs).
*/
export function createStableFlagController(
onChange: (active: boolean) => void,
{ delayMs = 0, minVisibleMs = 0 }: StableFlagOptions = {}
) {
let active = false
let shownAt: number | null = null
let showTimer: ReturnType<typeof setTimeout> | null = null
let hideTimer: ReturnType<typeof setTimeout> | null = null
const clearShow = () => {
if (showTimer !== null) {
clearTimeout(showTimer)
showTimer = null
}
}
const clearHide = () => {
if (hideTimer !== null) {
clearTimeout(hideTimer)
hideTimer = null
}
}
const show = () => {
showTimer = null
shownAt = Date.now()
active = true
onChange(true)
}
const hide = () => {
hideTimer = null
shownAt = null
active = false
onChange(false)
}
return {
setValue(value: boolean) {
if (value) {
clearHide()
if (active || showTimer !== null) {
return
}
showTimer = setTimeout(show, delayMs)
return
}
clearShow()
if (!active || hideTimer !== null) {
return
}
const elapsed = shownAt === null ? minVisibleMs : Date.now() - shownAt
const remaining = minVisibleMs - elapsed
if (remaining <= 0) {
hide()
return
}
hideTimer = setTimeout(hide, remaining)
},
dispose() {
clearShow()
clearHide()
},
}
}
/**
* Anti-flicker boolean. Mirrors `value` but smooths both edges so transient
* toggles never produce a visible flash:
*
* - Rising edge — `value` must hold true for `delayMs` before the flag turns on.
* - Falling edge — once on, the flag stays on for at least `minVisibleMs`.
*
* With both options at `0` it returns `value` unchanged (after a tick). Useful for
* connection/loading indicators that would otherwise flicker on sub-second changes.
*/
export function useStableFlag(value: boolean, options: StableFlagOptions = {}): boolean {
const [active, setActive] = useState(false)
const { delayMs = 0, minVisibleMs = 0 } = options
const valueRef = useRef(value)
valueRef.current = value
const controllerRef = useRef<ReturnType<typeof createStableFlagController> | null>(null)
useEffect(() => {
const controller = createStableFlagController(setActive, { delayMs, minVisibleMs })
controllerRef.current = controller
controller.setValue(valueRef.current)
return () => {
controller.dispose()
controllerRef.current = null
}
}, [delayMs, minVisibleMs])
useEffect(() => {
controllerRef.current?.setValue(value)
}, [value])
return active
}