{"version":3,"sources":["../src/index.ts","../src/use-computed-value.ts","../src/use-sync-external-store.ts","../src/Subscribe.tsx","../src/use-update.ts","../src/use-value.ts"],"sourcesContent":["export * from \"containerized-state\";\nexport * from \"./Subscribe.tsx\";\nexport * from \"./use-computed-value.ts\";\nexport * from \"./use-update.ts\";\nexport * from \"./use-value.ts\";\n","import type {\n  ComputeValue,\n  Container,\n  EqualityCheckFunction,\n} from \"containerized-state\";\nimport { useMemo } from \"react\";\nimport { useSyncExternalStoreWithSelector } from \"./use-sync-external-store.ts\";\n\n/**\n * Custom hook that subscribes to a container and computes a derived value.\n *\n * @param container The container to subscribe to and retrieve the value from.\n * @param compute The function to compute the derived value from the container's value.\n * @param [isEqual] Optional function to compare the previous and next computed values.\n *\n * @example\n * // Example usage:\n * const container = new Container({ count: 0, step: 1 });\n * const computeDouble = (state) => state.count * 2;\n * const doubleCount = useComputedValue(container, computeDouble);\n */\nexport const useComputedValue = <T, P>(\n  container: Container<T>,\n  compute: ComputeValue<T, P>,\n  isEqual?: EqualityCheckFunction<P>,\n): P => {\n  const { subscribe, getServerSnapshot, getSnapshot } = useMemo(\n    () => ({\n      subscribe: container.subscribe.bind(container),\n      getSnapshot: container.getValue.bind(container),\n      getServerSnapshot: container.getValue.bind(container),\n    }),\n    [container],\n  );\n\n  const snapshot = useSyncExternalStoreWithSelector(\n    subscribe,\n    getSnapshot,\n    getServerSnapshot,\n    compute,\n    isEqual,\n  );\n\n  return snapshot;\n};\n","export { useSyncExternalStore } from \"use-sync-external-store/shim\";\nexport { useSyncExternalStoreWithSelector } from \"use-sync-external-store/shim/with-selector\";\n","import type {\n  ComputeValue,\n  Container,\n  EqualityCheckFunction,\n} from \"containerized-state\";\nimport { useComputedValue } from \"./use-computed-value.ts\";\n\nexport type SubscribeProps<T, P> = {\n  /**\n   * The state container to subscribe to.\n   */\n  container: Container<T>;\n  /**\n   * A render function that receives the computed value as an argument.\n   */\n  children: (renderProps: P) => React.ReactNode;\n  /**\n   * A function that computes a derived value from the container's state.\n   */\n  compute?: ComputeValue<T, P>;\n  /**\n   * An optional function to compare previous and next computed values to\n   * prevent unnecessary re-renders.\n   */\n  isEqual?: EqualityCheckFunction<P>;\n};\n\n/**\n * A render props component that subscribes to a state container and provides\n * a computed value to its children.\n *\n * It uses the `useComputedValue` hook internally to handle the subscription\n * and re-rendering logic. This component is useful for encapsulating state\n * access and providing a clean, declarative API for consuming state in a\n * React component tree.\n *\n * @template T The type of the value in the state container.\n * @template P The type of the computed value.\n *\n * @example\n * // In a component:\n * const myContainer = new Container({ name: \"Alice\", age: 30 });\n *\n * <Subscribe\n * container={myContainer}\n * compute={(state) => `Hello, ${state.name}! You are ${state.age} years old.`}\n * >\n * {(greeting) => <div>{greeting}</div>}\n * </Subscribe>\n */\nexport const Subscribe = <T, P = T>(\n  props: SubscribeProps<T, P>,\n): React.JSX.Element => {\n  const {\n    children: render,\n    compute = (value: T) => value as unknown as P,\n    container,\n    isEqual,\n  } = props;\n\n  // The component will re-render whenever the computed value changes.\n  const computed = useComputedValue(container, compute, isEqual);\n  // Call the render prop function with the computed value to get the children to render.\n  const children = render(computed);\n\n  return <>{children}</>;\n};\n","import type { Container } from \"containerized-state\";\nimport { useCallback } from \"react\";\nimport type { CallableFunction } from \"./types.ts\";\n\nexport type Updater<T> = (\n  state: React.SetStateAction<T>,\n) => void | Promise<void>;\n\n/**\n * Custom hook that provides an updater function to set the value of a container.\n *\n * @param container - The container to update.\n *\n * @example\n * // Example usage:\n * const container = new Container(0);\n * const updateValue = useUpdate(container);\n * updateValue(42); // Sets the container value to 42\n * updateValue(prev => prev + 1); // Updates the container value using a function\n */\nexport const useUpdate = <T>(container: Container<T>): Updater<T> => {\n  return useCallback(\n    state => {\n      const newStateValue =\n        typeof state === \"function\"\n          ? (state as CallableFunction<[T], T>)(container.getValue())\n          : state;\n\n      return container.setValue(newStateValue);\n    },\n    [container],\n  );\n};\n","import type { Container } from \"containerized-state\";\nimport { useMemo } from \"react\";\nimport { useSyncExternalStore } from \"./use-sync-external-store.ts\";\n\n/**\n * Custom hook that subscribes to a container and retrieves its current value.\n *\n * @param container - The container to subscribe to and retrieve the value from.\n *\n * @example\n * // Example usage:\n * const container = new Container(0);\n * const currentValue = useValue(container);\n */\nexport const useValue = <T>(container: Container<T>): T => {\n  const { subscribe, getServerSnapshot, getSnapshot } = useMemo(\n    () => ({\n      subscribe: container.subscribe.bind(container),\n      getSnapshot: container.getValue.bind(container),\n      getServerSnapshot: container.getValue.bind(container),\n    }),\n    [container],\n  );\n\n  const snapshot = useSyncExternalStore(\n    subscribe,\n    getSnapshot,\n    getServerSnapshot,\n  );\n\n  return snapshot;\n};\n"],"mappings":"2dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,eAAAE,EAAA,qBAAAC,EAAA,cAAAC,EAAA,aAAAC,IAAA,eAAAC,EAAAN,GAAAO,EAAAP,EAAc,+BAAd,gBCKA,IAAAQ,EAAwB,iBCLxB,IAAAC,EAAqC,wCACrCC,EAAiD,sDDoB1C,IAAMC,EAAmB,CAC9BC,EACAC,EACAC,IACM,CACN,GAAM,CAAE,UAAAC,EAAW,kBAAAC,EAAmB,YAAAC,CAAY,KAAI,WACpD,KAAO,CACL,UAAWL,EAAU,UAAU,KAAKA,CAAS,EAC7C,YAAaA,EAAU,SAAS,KAAKA,CAAS,EAC9C,kBAAmBA,EAAU,SAAS,KAAKA,CAAS,CACtD,GACA,CAACA,CAAS,CACZ,EAUA,SARiB,oCACfG,EACAE,EACAD,EACAH,EACAC,CACF,CAGF,EEqBS,IAAAI,EAAA,6BAfIC,EACXC,GACsB,CACtB,GAAM,CACJ,SAAUC,EACV,QAAAC,EAAWC,GAAaA,EACxB,UAAAC,EACA,QAAAC,CACF,EAAIL,EAGEM,EAAWC,EAAiBH,EAAWF,EAASG,CAAO,EAEvDG,EAAWP,EAAOK,CAAQ,EAEhC,SAAO,mBAAG,SAAAE,EAAS,CACrB,ECjEA,IAAAC,EAA4B,iBAmBfC,EAAgBC,MACpB,eACLC,GAAS,CACP,IAAMC,EACJ,OAAOD,GAAU,WACZA,EAAmCD,EAAU,SAAS,CAAC,EACxDC,EAEN,OAAOD,EAAU,SAASE,CAAa,CACzC,EACA,CAACF,CAAS,CACZ,EC9BF,IAAAG,EAAwB,iBAajB,IAAMC,EAAeC,GAA+B,CACzD,GAAM,CAAE,UAAAC,EAAW,kBAAAC,EAAmB,YAAAC,CAAY,KAAI,WACpD,KAAO,CACL,UAAWH,EAAU,UAAU,KAAKA,CAAS,EAC7C,YAAaA,EAAU,SAAS,KAAKA,CAAS,EAC9C,kBAAmBA,EAAU,SAAS,KAAKA,CAAS,CACtD,GACA,CAACA,CAAS,CACZ,EAQA,SANiB,wBACfC,EACAE,EACAD,CACF,CAGF","names":["index_exports","__export","Subscribe","useComputedValue","useUpdate","useValue","__toCommonJS","__reExport","import_react","import_shim","import_with_selector","useComputedValue","container","compute","isEqual","subscribe","getServerSnapshot","getSnapshot","import_jsx_runtime","Subscribe","props","render","compute","value","container","isEqual","computed","useComputedValue","children","import_react","useUpdate","container","state","newStateValue","import_react","useValue","container","subscribe","getServerSnapshot","getSnapshot"]}