(
onChange: (active: boolean) => void,
{ delayMs = 0, minVisibleMs = 0 }: StableFlagOptions = {}
)
| 23 | * is safe to feed it the same value repeatedly (e.g. from React effect re-runs). |
| 24 | */ |
| 25 | export function createStableFlagController( |
| 26 | onChange: (active: boolean) => void, |
| 27 | { delayMs = 0, minVisibleMs = 0 }: StableFlagOptions = {} |
| 28 | ) { |
| 29 | let active = false |
| 30 | let shownAt: number | null = null |
| 31 | let showTimer: ReturnType<typeof setTimeout> | null = null |
| 32 | let hideTimer: ReturnType<typeof setTimeout> | null = null |
| 33 | |
| 34 | const clearShow = () => { |
| 35 | if (showTimer !== null) { |
| 36 | clearTimeout(showTimer) |
| 37 | showTimer = null |
| 38 | } |
| 39 | } |
| 40 | const clearHide = () => { |
| 41 | if (hideTimer !== null) { |
| 42 | clearTimeout(hideTimer) |
| 43 | hideTimer = null |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | const show = () => { |
| 48 | showTimer = null |
| 49 | shownAt = Date.now() |
| 50 | active = true |
| 51 | onChange(true) |
| 52 | } |
| 53 | const hide = () => { |
| 54 | hideTimer = null |
| 55 | shownAt = null |
| 56 | active = false |
| 57 | onChange(false) |
| 58 | } |
| 59 | |
| 60 | return { |
| 61 | setValue(value: boolean) { |
| 62 | if (value) { |
| 63 | clearHide() |
| 64 | if (active || showTimer !== null) { |
| 65 | return |
| 66 | } |
| 67 | showTimer = setTimeout(show, delayMs) |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | clearShow() |
| 72 | if (!active || hideTimer !== null) { |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | const elapsed = shownAt === null ? minVisibleMs : Date.now() - shownAt |
| 77 | const remaining = minVisibleMs - elapsed |
| 78 | if (remaining <= 0) { |
| 79 | hide() |
| 80 | return |
| 81 | } |
| 82 | hideTimer = setTimeout(hide, remaining) |
no outgoing calls