Skip to content

abso-core

Public
Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/src/engine/experimental/projection-read-scope.ts?format=json
59 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 143, 1 version changed it
1import type { Graph } from "../graph.ts";
2
3/** Private host-owned lifetime. The host closes it in commit/rollback finally blocks. */
4export class ProjectionReadScope {
5 readonly #graph: Graph;
6 readonly #source: () => unknown;
7 readonly #identity: unknown;
8 readonly #parent: () => void;
9 readonly #onClose: () => void;
10 #active = true;
11 #epoch = 0;
12 #child = false;
13 constructor(graph: Graph, source: () => unknown, parent = () => {}, onClose = () => {}) {
14 this.#graph = graph;
15 this.#source = source;
16 this.#identity = source();
17 this.#parent = parent;
18 this.#onClose = onClose;
19 }
20 #check(): void {
21 this.#parent();
22 if (!this.#active || this.#source() !== this.#identity) throw Error("expired projection read scope");
23 }
24 /** Capture this exact graph revision and scope generation, not a mutable validity flag. */
25 guard(graph: Graph): () => void {
26 this.#check();
27 if (graph !== this.#graph || this.#child) throw Error("projection read scope mismatch");
28 const epoch = this.#epoch,
29 size = graph.size;
30 return () => {
31 this.#check();
32 if (this.#child || this.#epoch !== epoch || graph.size !== size) throw Error("stale projection reader");
33 };
34 }
35 nested(): ProjectionReadScope {
36 this.#check();
37 if (this.#child) throw Error("projection child scope already active");
38 this.#child = true;
39 const epoch = ++this.#epoch;
40 return new ProjectionReadScope(
41 this.#graph,
42 this.#source,
43 () => {
44 this.#check();
45 if (this.#epoch !== epoch) throw Error("expired projection parent scope");
46 },
47 () => {
48 this.#child = false;
49 this.#epoch++;
50 },
51 );
52 }
53 close(): void {
54 if (!this.#active) return;
55 this.#active = false;
56 this.#epoch++;
57 this.#onClose();
58 }