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AK,QAAA,IAAY,IAAA,EAAM,IAAA,CAAK,QAAA,EAAU,EAAE,GAAG,KAAK,CAAA;AAAA,EACrD;AAAA;AAAA,EAGA,QAAA,CAAA,EAAoB;AACnB,IAAA,OAAO,IAAA,CAAK,QAAA,IAAY,IAAA;AAAA,EACzB;AAAA;AAAA,EAGA,YAAA,CAAA,EAAuB;AACtB,IAAA,OAAO,IAAA,CAAK,QAAA,CAAS,IAAA;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,KAAA,CAAA,EAAuB;AAC5B,IAAA,MAAA,CAAO,IAAA,CAAK,QAAA,CAAS,KAAA,EAAO,CAAA,EAAG;AAC9B,MAAA,MAAM,OAAA,CAAQ,UAAA,CAAW,CAAC,GAAG,IAAA,CAAK,QAAQ,CAAC,CAAA;AAAA,IAC5C;AAAA,EACD;AACD,UAAA;ADjBA;AACA;AACE;AACF,8BAAC","file":"/home/runner/work/kora/kora/packages/store/dist/chunk-X6VTACUR.cjs","sourcesContent":[null,"import { BuildSupersededError, type BuildSupersededInfo } from '../errors'\n\n/** What a refused write was: its collection (when it has one) and its operation. */\nexport interface WriteScope {\n\tcollection?: string\n\toperation: string\n}\n\n/**\n * Admits writes until the tab is superseded by a newer build (version gate, section 5,\n * steps 2 and 3), and lets retirement wait for every write it admitted.\n *\n * A write is admitted or refused synchronously, when it is called: a refused write never\n * started, so nothing of it is applied. An admitted write is never cancelled: it commits,\n * or fails and rejects to its caller as it would have anyway, and {@link drain} waits for\n * it to settle.\n */\nexport class WriteGate {\n\tprivate refusal: BuildSupersededInfo | null = null\n\tprivate readonly inflight = new Set<Promise<unknown>>()\n\n\t/**\n\t * Run `fn` as one write, or reject with {@link BuildSupersededError} without calling it\n\t * when the gate is closed. The check happens before `fn` runs, in the caller's task.\n\t */\n\trun<T>(scope: WriteScope, fn: () => Promise<T>): Promise<T> {\n\t\tif (this.refusal !== null) {\n\t\t\treturn Promise.reject(new BuildSupersededError(scope, this.refusal))\n\t\t}\n\t\tlet pending: Promise<T>\n\t\ttry {\n\t\t\tpending = fn()\n\t\t} catch (error) {\n\t\t\treturn Promise.reject(error)\n\t\t}\n\t\tthis.inflight.add(pending)\n\t\tconst settle = (): void => {\n\t\t\tthis.inflight.delete(pending)\n\t\t}\n\t\tpending.then(settle, settle)\n\t\treturn pending\n\t}\n\n\t/** Throws {@link BuildSupersededError} when the gate is closed (synchronous guard). */\n\tassertOpen(scope: WriteScope): void {\n\t\tif (this.refusal !== null) throw new BuildSupersededError(scope, this.refusal)\n\t}\n\n\t/** Refuse every write from now on. Idempotent; the first builds named are kept. */\n\tclose(info: BuildSupersededInfo): void {\n\t\tif (this.refusal === null) this.refusal = { ...info }\n\t}\n\n\t/** Whether writes are refused. */\n\tisClosed(): boolean {\n\t\treturn this.refusal !== null\n\t}\n\n\t/** Number of admitted writes that have not settled yet. */\n\tpendingCount(): number {\n\t\treturn this.inflight.size\n\t}\n\n\t/**\n\t * Resolve once every admitted write settled (committed or failed). Never rejects:\n\t * a write's failure belongs to its caller. Writes admitted while draining (the gate\n\t * still open) are waited for too.\n\t */\n\tasync drain(): Promise<void> {\n\t\twhile (this.inflight.size > 0) {\n\t\t\tawait Promise.allSettled([...this.inflight])\n\t\t}\n\t}\n}\n"]}