@reatom/react
Version:
React bindings for @reatom/core
1 lines • 4.3 kB
Source Map (JSON)
{"version":3,"file":"index.modern.mjs","sources":["../src/index.ts"],"sourcesContent":["import {\n Action,\n Atom,\n AtomState,\n createAtom,\n defaultStore,\n Fn,\n getState,\n isActionCreator,\n Rec,\n Store,\n} from '@reatom/core/'\nimport React from 'react'\n\nexport const reatomContext = React.createContext(defaultStore)\n\nlet batchedUpdates = (f: () => any) => f()\nexport const setBatchedUpdates = (f: (callback: () => any) => void) => {\n batchedUpdates = f\n}\n\nfunction bindActionCreator<Args extends any[]>(\n store: Store,\n actionCreator: (...args: Args) => Action | Action[] | void,\n) {\n return (...args: Args) => {\n const action = actionCreator(...args)\n\n if (action) {\n batchedUpdates(() => {\n store.dispatch(action)\n })\n }\n }\n}\n\nexport function useAction<Args extends any[] = []>(\n actionCreator: (...args: Args) => Action | Action[] | void,\n deps: any[] = [],\n): (...args: Args) => void {\n const store = React.useContext(reatomContext)\n\n return React.useCallback(\n bindActionCreator(store, actionCreator),\n deps.concat(store),\n )\n}\n\ntype ActionCreators<T extends Rec = {}> = {\n [K in keyof T]: T[K] extends (...a: infer Args) => Action\n ? (...args: Args) => void\n : never\n}\n\nexport function useAtom<T extends Atom>(\n atom: T,\n deps?: any[],\n): [state: AtomState<T>, bindedActionCreators: ActionCreators<T>]\nexport function useAtom<T extends Atom, Res>(\n atom: T,\n map: Fn<[AtomState<T>], Res>,\n deps?: any[],\n): [state: Res, bindedActionCreators: ActionCreators<T>]\nexport function useAtom(atom: Atom, mapOrDeps?: Fn | any[], deps?: any[]) {\n const origin = atom\n if (typeof mapOrDeps === 'function') {\n deps ??= []\n // FIXME: rewrite to useState\n atom = React.useMemo(\n () => createAtom({ origin }, ({ get }) => mapOrDeps(get(`origin`))),\n deps.concat(origin),\n )\n } else {\n deps = mapOrDeps ?? []\n }\n\n const store = React.useContext(reatomContext)\n\n deps = deps.concat([atom, store])\n\n let [state, setState] = React.useState(() => getState(atom, store))\n const lastRef = React.useRef(state)\n lastRef.current = state = getState(atom, store)\n\n const bindedActionCreators = React.useMemo(\n () =>\n Object.entries(origin).reduce((acc, [k, ac]) => {\n // @ts-expect-error\n if (isActionCreator(ac)) acc[k] = bindActionCreator(store, ac)\n return acc\n }, {} as ActionCreators),\n deps,\n )\n\n React.useEffect(() => {\n return store.subscribe(\n atom,\n (newState) =>\n Object.is(newState, lastRef.current) ||\n setState((lastRef.current = newState)),\n )\n }, deps)\n\n React.useDebugValue(state)\n\n return [state, bindedActionCreators]\n}\n"],"names":["reatomContext","React","createContext","defaultStore","batchedUpdates","f","setBatchedUpdates","bindActionCreator","store","actionCreator","args","action","dispatch","useAction","deps","useContext","useCallback","concat","useAtom","atom","mapOrDeps","origin","useMemo","createAtom","get","state","setState","useState","getState","lastRef","useRef","current","bindedActionCreators","Object","entries","reduce","acc","k","ac","isActionCreator","useEffect","subscribe","newState","is","useDebugValue"],"mappings":"qHAcaA,MAAAA,eAAgBC,EAAMC,cAAcC,GAEjD,IAAIC,EAAkBC,GAAiBA,IAC1BC,MAAAA,EAAqBD,IAChCD,EAAiBC,GAGnB,SAASE,EACPC,EACAC,GAEA,MAAO,IAAIC,KACT,MAAMC,EAASF,KAAiBC,GAE5BC,GACFP,EAAe,KACbI,EAAMI,SAASD,eAMPE,EACdJ,EACAK,EAAc,IAEd,MAAMN,EAAQP,EAAMc,WAAWf,GAE/B,OAAOC,EAAMe,YACXT,EAAkBC,EAAOC,GACzBK,EAAKG,OAAOT,aAmBAU,EAAQC,EAAYC,EAAwBN,GAC1D,MAAMO,EAASF,EACU,mBAAdC,SACTN,IAAAA,EAAS,IAETK,EAAOlB,EAAMqB,QACX,IAAMC,EAAW,CAAEF,OAAAA,GAAU,EAAGG,IAAAA,KAAUJ,EAAUI,cACpDV,EAAKG,OAAOI,KAGdP,QAAOM,EAAAA,EAAa,GAGtB,MAAMZ,EAAQP,EAAMc,WAAWf,GAE/Bc,EAAOA,EAAKG,OAAO,CAACE,EAAMX,IAE1B,IAAKiB,EAAOC,GAAYzB,EAAM0B,SAAS,IAAMC,EAAST,EAAMX,IAC5D,MAAMqB,EAAU5B,EAAM6B,OAAOL,GAC7BI,EAAQE,QAAUN,EAAQG,EAAST,EAAMX,GAEzC,MAAMwB,EAAuB/B,EAAMqB,QACjC,IACEW,OAAOC,QAAQb,GAAQc,OAAO,CAACC,GAAMC,EAAGC,MAElCC,EAAgBD,KAAKF,EAAIC,GAAK9B,EAAkBC,EAAO8B,IACpDF,GACN,IACLtB,GAcF,OAXAb,EAAMuC,UAAU,IACPhC,EAAMiC,UACXtB,EACCuB,GACCT,OAAOU,GAAGD,EAAUb,EAAQE,UAC5BL,EAAUG,EAAQE,QAAUW,IAE/B5B,GAEHb,EAAM2C,cAAcnB,GAEb,CAACA,EAAOO"}