{"version":3,"file":"internal-DH_wIht6.cjs","names":[],"sources":["../src/withStore/internal.ts"],"sourcesContent":["import type { AtomStore } from \"../createAtomStore/implementation\";\n\n/**\n * Browser store context implementation.\n *\n * Uses a module-global synchronous stack. Safe for single-threaded\n * browser environments where only one render boundary executes at a time.\n *\n * For Node.js / Edge builds, this file is replaced at build time via\n * resolve alias with `internal.node.ts` which uses AsyncLocalStorage.\n */\nconst storeStack: AtomStore[] = [];\n\nfunction popStoreBoundary(store: AtomStore): void {\n  const popped = storeStack.pop();\n  if (popped !== store) {\n    throw new Error(\"Store boundary stack is corrupted\");\n  }\n}\n\n/**\n * Returns the currently active store in the nearest `withStore` boundary,\n * or `undefined` if called outside any boundary.\n */\nfunction getCurrentStore(): AtomStore | undefined {\n  return storeStack[storeStack.length - 1];\n}\n\n/**\n * Executes `fn` within a store boundary backed by a synchronous stack.\n *\n * Pushes `store` onto the module-scoped stack before invoking `fn`, and\n * pops it in a `finally` block so the stack stays consistent even if\n * `fn` throws.\n */\nfunction runWithStoreContext<T>(store: AtomStore, fn: () => T): T {\n  storeStack.push(store);\n\n  try {\n    const result = fn();\n    popStoreBoundary(store);\n    return result;\n  } catch (error: unknown) {\n    popStoreBoundary(store);\n    throw error instanceof Error ? error : new Error(String(error));\n  }\n}\n\nexport { getCurrentStore, runWithStoreContext };\n"],"mappings":";;;;;;;;;;;AAWA,MAAM,aAA0B,EAAE;AAElC,SAAS,iBAAiB,OAAwB;CAEhD,IADe,WAAW,KAChB,KAAK,OACb,MAAM,IAAI,MAAM,oCAAoC;;;;;;AAQxD,SAAS,kBAAyC;CAChD,OAAO,WAAW,WAAW,SAAS;;;;;;;;;AAUxC,SAAS,oBAAuB,OAAkB,IAAgB;CAChE,WAAW,KAAK,MAAM;CAEtB,IAAI;EACF,MAAM,SAAS,IAAI;EACnB,iBAAiB,MAAM;EACvB,OAAO;UACA,OAAgB;EACvB,iBAAiB,MAAM;EACvB,MAAM,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,MAAM,CAAC"}