{"version":3,"file":"react-atom.mjs","names":["atomCollection","baseAtomCollection"],"sources":["../../../../@mongez/react-atom/src/react-atom.tsx"],"sourcesContent":["\"use client\";\n/* eslint-disable react-hooks/rules-of-hooks */\nimport {\n  type Atom,\n  type AtomActions,\n  type AtomCollectionActions,\n  type AtomOptions,\n  type AtomValue,\n  atomCollection as baseAtomCollection,\n  type CollectionOptions,\n  createAtom,\n} from \"@mongez/atom\";\nimport {\n  useCallback,\n  useEffect,\n  useSyncExternalStore,\n} from \"react\";\nimport { useAtom } from \"./store\";\nimport type { ReactActions, ReactAtom } from \"./types\";\n\n/**\n * Build the React-aware action bag injected into every atom created via\n * the `atom()` factory in this package.\n *\n * Every hook in here goes through `useAtom(this)` first so that\n * components rendered inside an `<AtomStoreProvider>` operate on the\n * store-scoped clone, not the module-level template.\n *\n * Subscriptions are wired through `useSyncExternalStore` to keep React 18+\n * concurrent rendering tear-free.\n */\nfunction reactActions<Value>(data: any): ReactActions<Value> {\n  return {\n    ...data.actions,\n\n    Provider(props) {\n      const atom = useAtom(this as unknown as Atom<Value>);\n      useEffect(() => {\n        atom.update(props.value as Value);\n      }, [props.value, atom]);\n      return props.children;\n    },\n\n    useWatch(key, callback) {\n      const atom = useAtom(this as unknown as Atom<Value>);\n      useEffect(() => {\n        const sub = atom.watch(key, callback);\n        return () => sub.unsubscribe();\n      }, [atom, key, callback]);\n    },\n\n    useState() {\n      const atom = useAtom(this as unknown as Atom<Value>);\n\n      const subscribe = useCallback(\n        (onChange: () => void) => {\n          const sub = atom.onChange(onChange);\n          return () => sub.unsubscribe();\n        },\n        [atom]\n      );\n      const getSnapshot = useCallback(() => atom.value, [atom]);\n\n      const value = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n      const setValue = useCallback(\n        (next: Value | ((oldValue: Value) => Value)) => {\n          atom.update(next as any);\n        },\n        [atom]\n      );\n\n      return [value, setValue];\n    },\n\n    useValue() {\n      const atom = useAtom(this as unknown as Atom<Value>);\n\n      const subscribe = useCallback(\n        (onChange: () => void) => {\n          const sub = atom.onChange(onChange);\n          return () => sub.unsubscribe();\n        },\n        [atom]\n      );\n      const getSnapshot = useCallback(() => atom.value, [atom]);\n\n      return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n    },\n\n    use<K extends keyof Value>(key: K): Value[K] {\n      const atom = useAtom(this as unknown as ReactAtom<Value>);\n\n      const subscribe = useCallback(\n        (onChange: () => void) => {\n          const sub = atom.watch(key, onChange);\n          return () => sub.unsubscribe();\n        },\n        [atom, key]\n      );\n      const getSnapshot = useCallback(\n        () => atom.get(key),\n        [atom, key]\n      );\n\n      return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n    },\n  };\n}\n\n/**\n * Create a new React-aware atom.\n *\n * The returned atom carries hooks (`useState`, `useValue`, `use`, `useWatch`)\n * and a `<Provider>` component as instance methods. All hooks honor the\n * nearest `<AtomStoreProvider>` and use `useSyncExternalStore` underneath.\n */\nexport function atom<\n  Value = any,\n  Actions extends AtomActions<Value> = AtomActions<Value>,\n>(data: AtomOptions<AtomValue<Value>>): ReactAtom<Value, Actions> {\n  return createAtom<Value, any>({\n    ...data,\n    actions: reactActions<Value>(data),\n  });\n}\n\n/**\n * Create a React-aware collection atom for working with arrays.\n */\nexport function atomCollection<\n  Value = any,\n  Actions extends AtomCollectionActions<Value> = AtomCollectionActions<Value>,\n>(options: CollectionOptions<Value>) {\n  return baseAtomCollection({\n    ...options,\n    actions: {\n      ...options.actions,\n      ...(reactActions(options) as any),\n    } as AtomCollectionActions<Value[]> & Actions,\n  });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA+BA,SAAS,aAAoB,MAAgC;CAC3D,OAAO;EACL,GAAG,KAAK;EAER,SAAS,OAAO;GACd,MAAM,OAAO,QAAQ,IAA8B;GACnD,gBAAgB;IACd,KAAK,OAAO,MAAM,KAAc;GAClC,GAAG,CAAC,MAAM,OAAO,IAAI,CAAC;GACtB,OAAO,MAAM;EACf;EAEA,SAAS,KAAK,UAAU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