{"version":3,"file":"atom-store.mjs","names":[],"sources":["../../../../@mongez/atom/src/atom-store.ts"],"sourcesContent":["import type { Atom } from \"./types\";\n\n/**\n * A store is an isolated registry of atom instances.\n *\n * Each store creates and holds its own clones of atom templates so that\n * concurrent consumers (e.g. server-rendered requests) do not share state.\n *\n * Stores are looked up via React context by `<AtomStoreProvider>` in\n * `@mongez/react-atom`, but the class itself is framework-agnostic and can\n * be used directly outside React.\n */\nexport class AtomStore {\n  /**\n   * Scoped atom clones, keyed by the ORIGINAL atom's key (not the clone key).\n   */\n  private store = new Map<string, Atom<any>>();\n\n  /**\n   * Values applied to atoms the moment they enter the store. Useful for\n   * SSR hydration when atoms register lazily.\n   */\n  private pendingValues = new Map<string, unknown>();\n\n  /**\n   * Get or lazily create a store-scoped clone of the given atom template.\n   * The clone shares the template's options (actions, beforeUpdate, get,\n   * onUpdate) but owns its own state and event topic.\n   */\n  use<V, A extends Record<string, any> = {}>(template: Atom<V, A>): Atom<V, A> {\n    const existing = this.store.get(template.key);\n    if (existing) return existing as Atom<V, A>;\n\n    const scoped = template.clone({ register: false }) as Atom<V, A>;\n\n    if (this.pendingValues.has(template.key)) {\n      scoped.silentUpdate(this.pendingValues.get(template.key) as V);\n      this.pendingValues.delete(template.key);\n    }\n\n    this.store.set(template.key, scoped);\n    return scoped;\n  }\n\n  /**\n   * Look up a scoped atom by its original key. Returns undefined when the\n   * atom has not been used in this store yet.\n   */\n  get<V = any>(key: string): Atom<V> | undefined {\n    return this.store.get(key) as Atom<V> | undefined;\n  }\n\n  /**\n   * True when the given key has a scoped atom in this store.\n   */\n  has(key: string): boolean {\n    return this.store.has(key);\n  }\n\n  /**\n   * All scoped atoms currently in this store.\n   */\n  list(): Atom<any>[] {\n    return Array.from(this.store.values());\n  }\n\n  /**\n   * Apply initial values to atoms in the store. Atoms not yet registered\n   * have their values queued until first `use(template)` call.\n   */\n  hydrate(snapshot: Record<string, unknown>): void {\n    for (const key in snapshot) {\n      const value = snapshot[key];\n      const atom = this.store.get(key);\n      if (atom) {\n        atom.silentUpdate(value);\n      } else {\n        this.pendingValues.set(key, value);\n      }\n    }\n  }\n\n  /**\n   * Serialize the current values of every scoped atom as a plain object.\n   * Intended for SSR payloads that the client will pass back via\n   * `<AtomStoreProvider initialValues={...}>`.\n   */\n  snapshot(): Record<string, unknown> {\n    const result: Record<string, unknown> = {};\n    for (const [key, atom] of this.store.entries()) {\n      result[key] = atom.value;\n    }\n    return result;\n  }\n\n  /**\n   * Destroy every scoped atom and clear the store. Call this at the end of\n   * a request lifecycle to release event-bus subscriptions and let the\n   * scoped atoms be garbage collected.\n   */\n  destroy(): void {\n    for (const atom of this.store.values()) {\n      atom.destroy();\n    }\n    this.store.clear();\n    this.pendingValues.clear();\n  }\n}\n\n/**\n * Convenience factory; equivalent to `new AtomStore()`.\n */\nexport function createAtomStore(): AtomStore {\n  return new AtomStore();\n}\n"],"mappings":";;;;;;;;;;;AAYA,IAAa,YAAb,MAAuB;;;;CAIrB,AAAQ,wBAAQ,IAAI,IAAuB;;;;;CAM3C,AAAQ,gCAAgB,IAAI,IAAqB;;;;;;CAOjD,IAA2C,UAAkC;EAC3E,MAAM,WAAW,KAAK,MAAM,IAAI,SAAS,GAAG;EAC5C,IAAI,UAAU,OAAO;EAErB,MAAM,SAAS,SAAS,MAAM,EAAE,UAAU,MAAM,CAAC;EAEjD,IAAI,KAAK,cAAc,IAAI,SAAS,GAAG,GAAG;GACxC,OAAO,aAAa,KAAK,cAAc,IAAI,SAAS,GAAG,CAAM;GAC7D,KAAK,cAAc,OAAO,SAAS,GAAG;EACxC;EAEA,KAAK,MAAM,IAAI,SAAS,KAAK,MAAM;EACnC,OAAO;CACT;;;;;CAMA,IAAa,KAAkC;EAC7C,OAAO,KAAK,MAAM,IAAI,GAAG;CAC3B;;;;CAKA,IAAI,KAAsB;EACxB,OAAO,KAAK,MAAM,IAAI,GAAG;CAC3B;;;;CAKA,OAAoB;EAClB,OAAO,MAAM,KAAK,KAAK,MAAM,OAAO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