{"version":3,"sources":["../../src/react/use-reactive.ts"],"sourcesContent":["import { useRef } from 'react'\nimport { deepEqual } from '../utils/index.js'\nimport { createWithHooks } from './create-with-hooks.js'\n\nimport type { DependencyList } from 'react'\nimport type { SnapshotOptions } from './use-snapshot.js'\n\nexport interface UseReactiveOptions<State> extends SnapshotOptions<State> {}\n\n/**\n * A React Hook that helps to use [Reactive](https://sheinsight.github.io/reactive) in React with ease.\n *\n * @since 0.2.0\n */\nexport function useReactive<State extends object>(\n  initialState: State,\n  options: UseReactiveOptions<State> = {},\n): readonly [State, State] {\n  const store = useCreation(() => createWithHooks(initialState), [initialState])\n  return [store.useSnapshot({ ...options }), store.mutate] as const\n}\n\ninterface CreationRefState<T> {\n  value?: T\n  created?: boolean\n  preDeps?: DependencyList\n}\n\nfunction useCreation<T>(create: () => T, currDeps?: DependencyList): T {\n  const { current: creation } = useRef<CreationRefState<T>>({})\n\n  if (!creation.created || !deepEqual(creation.preDeps, currDeps)) {\n    creation.value = create()\n    creation.created = true\n    creation.preDeps = currDeps\n  }\n\n  return creation.value as T\n}\n"],"names":["useReactive","initialState","options","store","useCreation","createWithHooks","useSnapshot","mutate","create","currDeps","useRef","current","creation","created","deepEqual","preDeps","value"],"mappings":";;;;+BAcgBA;;;eAAAA;;;qBAdO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAchB,SAASA,YACdC,YAAmB;QACnBC,UAAAA,iEAAqC,CAAC;IAEtC,IAAMC,QAAQC,YAAY;eAAMC,IAAAA,mCAAe,EAACJ;OAAe;QAACA;KAAa;IAC7E,OAAO;QAACE,MAAMG,WAAW,CAAC,mBAAKJ;QAAYC,MAAMI,MAAM;KAAC;AAC1D;AAQA,SAASH,YAAeI,MAAe,EAAEC,QAAyB;IAChE,IAA8BC,UAAAA,IAAAA,aAAM,EAAsB,CAAC,IAAnDC,AAASC,WAAaF,QAAtBC;IAER,IAAI,CAACC,SAASC,OAAO,IAAI,CAACC,IAAAA,mBAAS,EAACF,SAASG,OAAO,EAAEN,WAAW;QAC/DG,SAASI,KAAK,GAAGR;QACjBI,SAASC,OAAO,GAAG;QACnBD,SAASG,OAAO,GAAGN;IACrB;IAEA,OAAOG,SAASI,KAAK;AACvB"}