UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

32 lines (31 loc) • 950 B
const builtBy = new WeakMap(); export class HolderLatch { shut = false; holder; constructor(holder) { this.holder = holder; } close() { if (this.shut) return false; this.shut = true; return true; } get closed() { if (this.shut) return true; const by = this.holder === undefined ? undefined : builtBy.get(this.holder); return by !== undefined && by.closed; } refuse(verb) { if (this.closed) throw closedHolderRefusal(verb); } } export function closedHolderRefusal(verb) { return Object.assign(new Error(`${verb}: this store was closed (its own close, or the dispose of the backend that built it), so it lays nothing down under the data root — open the data root again to go on`), { code: "log_closed" }); } export function builtUnder(store, backend) { builtBy.set(store, backend); return store; }