Make usePrism hot-reloadable

This commit is contained in:
Aria Minaei 2023-08-02 19:41:22 +02:00
parent d26db282c6
commit 27f918f53c

View file

@ -65,16 +65,16 @@ export function usePrism<T>(
atomRef.current.set(fnAsCallback) atomRef.current.set(fnAsCallback)
} }
const prsm = useMemo( const prismRef = useRef<Prism<T> | null>(null)
() =>
prism(() => {
const fn = atomRef.current.prism.getValue()
return fn()
}),
[],
)
return usePrismInstance(prsm, debugLabel) if (!prismRef.current) {
prismRef.current = prism(() => {
const fn = atomRef.current.prism.getValue()
return fn()
})
}
return usePrismInstance(prismRef.current, debugLabel)
} }
export const useVal: typeof val = (p: $IntentionalAny, debugLabel?: string) => { export const useVal: typeof val = (p: $IntentionalAny, debugLabel?: string) => {
@ -346,6 +346,7 @@ export function usePrismInstance<T>(der: Prism<T>, debugLabel?: string): T {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
if (der !== ref.current.der) { if (der !== ref.current.der) {
debugger
console.error( console.error(
'Argument `der` in `usePrismInstance(der)` should not change between renders.', 'Argument `der` in `usePrismInstance(der)` should not change between renders.',
) )