104 lines
3.4 KiB
TypeScript
104 lines
3.4 KiB
TypeScript
/// SPDX-License-Identifier: GPL-3.0-or-later
|
|
/// SPDX-FileCopyrightText: Copyright © 2016-2022 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
|
|
|
import { IdentityMap } from '@preserves/core';
|
|
import { Index } from './skeleton.js';
|
|
import { Actor, Assertion, Entity, Facet, Handle, LocalAction, Ref, Turn } from './actor.js';
|
|
import { Observe, toObserve } from '../gen/dataspace.js';
|
|
import * as P from '../gen/dataspacePatterns.js';
|
|
|
|
export type DataspaceOptions = {
|
|
tracer?: (event: '+' | '-' | '!',
|
|
assertion: Assertion,
|
|
dataspace: Dataspace,
|
|
is_significant: boolean) => void,
|
|
dumpIndex?: boolean,
|
|
};
|
|
|
|
export class Dataspace implements Partial<Entity> {
|
|
readonly options: DataspaceOptions;
|
|
readonly index = new Index();
|
|
readonly handleMap = new IdentityMap<Handle, Assertion>();
|
|
|
|
constructor(options?: DataspaceOptions) {
|
|
this.options = options ?? {};
|
|
}
|
|
|
|
assert(v: Assertion, handle: Handle): void {
|
|
const is_new = this.index.addAssertion(v);
|
|
this.options.tracer?.('+', v, this, is_new);
|
|
if (is_new) {
|
|
const o = toObserve(v);
|
|
if (o !== void 0) {
|
|
this.index.addObserver(o.pattern, o.observer);
|
|
}
|
|
if (this.options.dumpIndex ?? false) this.index.dump();
|
|
}
|
|
this.handleMap.set(handle, v);
|
|
}
|
|
|
|
retract(handle: Handle): void {
|
|
const v = this.handleMap.get(handle);
|
|
if (v === void 0) return;
|
|
const is_last = this.index.removeAssertion(v);
|
|
this.options.tracer?.('-', v, this, is_last);
|
|
if (is_last) {
|
|
const o = toObserve(v);
|
|
if (o !== void 0) {
|
|
this.index.removeObserver(o.pattern, o.observer);
|
|
}
|
|
if (this.options.dumpIndex ?? false) this.index.dump();
|
|
}
|
|
}
|
|
|
|
message(v: Assertion): void {
|
|
this.options.tracer?.('!', v, this, true);
|
|
this.index.deliverMessage(v);
|
|
}
|
|
|
|
static boot(bootProc: (ds: Ref) => void, options?: DataspaceOptions): Actor {
|
|
return Actor.boot(() => {
|
|
Turn.activeFacet.preventInertCheck();
|
|
const ds = Turn.active.ref(new Dataspace(options));
|
|
bootProc(ds);
|
|
});
|
|
}
|
|
}
|
|
|
|
export function assertionObserver(f: (a: Assertion) => LocalAction | undefined): Partial<Entity> {
|
|
const assertionMap = new IdentityMap<Handle, LocalAction>();
|
|
return {
|
|
assert(a: Assertion, h: Handle): void {
|
|
const g = f(a) ?? null;
|
|
if (g !== null) {
|
|
assertionMap.set(h, g);
|
|
}
|
|
},
|
|
retract(h: Handle): void {
|
|
assertionMap.get(h)?.();
|
|
assertionMap.delete(h);
|
|
},
|
|
};
|
|
}
|
|
|
|
export function assertionFacetObserver(f: (a: Assertion) => void, inertOk: boolean = true): Partial<Entity> {
|
|
const facetMap = new IdentityMap<Handle, Facet>();
|
|
return {
|
|
assert(a: Assertion, h: Handle): void {
|
|
facetMap.set(h, Turn.active.facet(() => {
|
|
if (inertOk) Turn.activeFacet.preventInertCheck();
|
|
f(a);
|
|
}));
|
|
},
|
|
retract(h: Handle): void {
|
|
const facet = facetMap.get(h);
|
|
if (facet) Turn.active.stop(facet);
|
|
facetMap.delete(h);
|
|
},
|
|
};
|
|
}
|
|
|
|
export function assertObserve(ds: Ref, pattern: P.Pattern, e: Partial<Entity>): Handle {
|
|
return Turn.active.assert(ds, Observe({ pattern, observer: Turn.ref(e) }));
|
|
}
|