All files / layers / L2W / routes routeContextStore.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 6/6
100% Lines 9/9

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  66x 66x   67x 67x     390x 390x 326x       3004x     1272x            
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)
}