/// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones import { IdentityMap } from '@preserves/core'; import { Index } from './skeleton.js'; import { Assertion, Entity, Facet, Handle, LocalAction, Ref, Turn } from './actor.js'; import { fromObserve, Observe, toObserve } from '../gen/dataspace.js'; import * as P from '../gen/dataspacePatterns.js'; export class Dataspace implements Partial { readonly index = new Index(); readonly handleMap = new IdentityMap(); assert(turn: Turn, v: Assertion, handle: Handle): void { this.index.addAssertion(turn, v); const o = toObserve(v); if (o !== void 0) { this.index.addObserver(turn, o.pattern, o.observer); } this.handleMap.set(handle, [v, o]); } retract(turn: Turn, handle: Handle): void { const entry = this.handleMap.get(handle); if (entry === void 0) return; const [v, o] = entry; if (o !== void 0) { this.index.removeObserver(turn, o.pattern, o.observer); } this.index.removeAssertion(turn, v); } message(turn: Turn, v: Assertion): void { this.index.deliverMessage(turn, v); } } export function assertionObserver(f: (t: Turn, a: Assertion) => LocalAction | undefined): Partial { const assertionMap = new IdentityMap(); return { assert(t: Turn, a: Assertion, h: Handle): void { const g = f(t, a) ?? null; if (g !== null) { assertionMap.set(h, g); } }, retract(t: Turn, h: Handle): void { assertionMap.get(h)?.(t); assertionMap.delete(h); }, }; } export function assertionFacetObserver(f: (t: Turn, a: Assertion) => void, inertOk: boolean = true): Partial { const facetMap = new IdentityMap(); return { assert(t: Turn, a: Assertion, h: Handle): void { facetMap.set(h, t.facet(t => { if (inertOk) t.activeFacet.preventInertCheck(); f(t, a); })); }, retract(t: Turn, h: Handle): void { const facet = facetMap.get(h); if (facet) t.stop(facet); facetMap.delete(h); }, }; } export function assertObserve(t: Turn, ds: Ref, pattern: P.Pattern, e: Partial): Handle { return t.assert(ds, fromObserve(Observe({ pattern, observer: t.ref(e) }))); }