All files / layers / L2W / routes routeContextStore.ts

100% Statements 20/20
100% Branches 0/0
100% Functions 12/12
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24  38x 38x 4x 13x 13x   44x 170x 170x 134x   17x 17x 1147x     482x       32x    
import { useSyncExternalStore } from 'react'
import type { RouteContext } from '@/layers/interfaces'
 
let current: RouteContext | null = null
const listeners = new Set<() => void>()
 
export function setRouteContext(ctx: RouteContext | null) {
  current = ctx
  listeners.forEach((l) => l())
}
 
function subscribe(fn: () => void) {
  listeners.add(fn)
  return () => { listeners.delete(fn) }
}
 
function getSnapshot(): RouteContext | null {
  return current
}
 
export function useRouteContext(): RouteContext | null {
  return useSyncExternalStore(subscribe, getSnapshot)
}