Skip to content

Commit 030b6c0

Browse files
committed
fix(realtime): reset stable-flag React state on options change; de-vacuous blip test
Address Greptile review: - useStableFlag: reset React state to the fresh controller's baseline when the controller is recreated on an options change, so a dynamic consumer changing delayMs/minVisibleMs while active with value already false can no longer strand the flag at true. - test: read the live probe.active getter in the blip test instead of a destructured snapshot, which was bound to false at destructure time and made the assertion vacuous.
1 parent 978bac1 commit 030b6c0

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

apps/sim/hooks/use-stable-flag.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ describe('createStableFlagController', () => {
3838
})
3939

4040
it('suppresses a blip that heals before the delay (flash-on)', () => {
41-
const { controller, states, active } = setup()
41+
const probe = setup()
4242

43-
controller.setValue(true)
43+
probe.controller.setValue(true)
4444
vi.advanceTimersByTime(DELAY_MS - 1)
45-
controller.setValue(false)
45+
probe.controller.setValue(false)
4646
vi.advanceTimersByTime(10_000)
4747

48-
expect(states).toEqual([])
49-
expect(active).toBe(false)
48+
expect(probe.states).toEqual([])
49+
expect(probe.active).toBe(false)
5050
})
5151

5252
it('does not turn on one tick before the delay boundary', () => {

apps/sim/hooks/use-stable-flag.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ export function useStableFlag(value: boolean, options: StableFlagOptions = {}):
106106
const controllerRef = useRef<ReturnType<typeof createStableFlagController> | null>(null)
107107

108108
useEffect(() => {
109+
// Reset to the fresh controller's baseline. Without this, recreating the
110+
// controller on an options change while `active` is true and `value` is
111+
// already false would strand the React state at true — the new controller
112+
// starts internally false, so its `setValue(false)` early-returns and never
113+
// emits `onChange(false)`.
114+
setActive(false)
109115
const controller = createStableFlagController(setActive, { delayMs, minVisibleMs })
110116
controllerRef.current = controller
111117
controller.setValue(valueRef.current)

0 commit comments

Comments
 (0)