{"version":3,"file":"internal.node-DiqCZpRg.mjs","names":[],"sources":["../src/withStore/internal.node.ts"],"sourcesContent":["import { AsyncLocalStorage } from \"node:async_hooks\";\n\nimport type { AtomStore } from \"../createAtomStore/implementation\";\n\n/**\n * Node.js / Edge store context implementation.\n *\n * Uses AsyncLocalStorage to provide per-request isolation. Each\n * `runWithStoreContext` call creates a new ALS context with the store\n * appended to the inherited stack. This ensures:\n *\n * - Concurrent SSR requests never leak stores across request boundaries.\n * - Async callbacks within the same request automatically inherit the\n *   store context (microtasks, setTimeout, await, etc.).\n * - Nested `withStore` calls properly shadow the outer store.\n */\nconst als = new AsyncLocalStorage<AtomStore[]>();\n\n/**\n * Returns the currently active store in the nearest `withStore` boundary,\n * or `undefined` if called outside any boundary (or outside an ALS context).\n */\nfunction getCurrentStore(): AtomStore | undefined {\n  const stack = als.getStore();\n  if (stack === undefined || stack.length === 0) {\n    return undefined;\n  }\n  return stack[stack.length - 1];\n}\n\n/**\n * Executes `fn` within a store boundary backed by AsyncLocalStorage.\n *\n * Creates a new ALS context with `store` appended to the inherited stack.\n * Nested calls inherit the parent stack and append their own store.\n * The ALS context is automatically cleaned up when `fn` returns or throws.\n */\nfunction runWithStoreContext<T>(store: AtomStore, fn: () => T): T {\n  const parentStack = als.getStore() ?? [];\n  return als.run([...parentStack, store], fn);\n}\n\nexport { getCurrentStore, runWithStoreContext };\n"],"mappings":";;;;;;;;;;;;;;;AAgBA,MAAM,MAAM,IAAI,mBAAgC;;;;;AAMhD,SAAS,kBAAyC;CAChD,MAAM,QAAQ,IAAI,UAAU;CAC5B,IAAI,UAAU,UAAa,MAAM,WAAW,GAC1C;CAEF,OAAO,MAAM,MAAM,SAAS;;;;;;;;;AAU9B,SAAS,oBAAuB,OAAkB,IAAgB;CAChE,MAAM,cAAc,IAAI,UAAU,IAAI,EAAE;CACxC,OAAO,IAAI,IAAI,CAAC,GAAG,aAAa,MAAM,EAAE,GAAG"}