'use client' import { useCallback, useSyncExternalStore } from 'react' import { hk } from 'rntwsc/fetch/config' import type { HydrationData } from 'rntwsc/hydration/config' import { drainHydration, subscribeHydration } from 'rntwsc/hydration/store' import { isBrowser } from 'rntwsc/platform' import type { StrMap } from 'rntwsc/libs/utility-types' const store: StrMap = {} const listeners = new Set<() => void>() const notify = () => listeners.forEach(cb => cb()) const subscribe = (cb: () => void) => { listeners.add(cb) return () => listeners.delete(cb) } export const useFetchData = (k: string) => { const fn = useCallback(() => store[k], [k]) return useSyncExternalStore(subscribe, fn, fn) as HydrationData | undefined } // to use when we dont need hook // eg: to merge in action function.. export const getFetchData = (k: string) => store[k] as HydrationData | undefined export const setFetchData = ( k: string, v: HydrationData, { silent = false } = {}, ) => { if (store[k] === v) { return } store[k] = v if (!silent) { notify() } } export const clearFetchData = (k: string, { silent = false } = {}) => { if (!(k in store)) { return } delete store[k] if (!silent) { notify() } } export const clearAllFetchData = ({ silent = false } = {}) => { const keys = Object.keys(store) if (!keys.length) { return } keys.forEach(k => { delete store[k] }) if (!silent) { notify() } } const syncFromHydration = ({ silent = false } = {}) => { let found = false drainHydration(hk, (k, d, v) => { if (store[k] !== v) { store[k] = v found = true } }) if (found && !silent) { notify() } } if (isBrowser) { syncFromHydration({ silent: true, }) subscribeHydration(syncFromHydration) }