syndicate-js/packages/core/src/runtime/dataspace.ts

104 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-12-01 16:24:29 +00:00
/// SPDX-License-Identifier: GPL-3.0-or-later
2023-01-17 10:43:15 +00:00
/// SPDX-FileCopyrightText: Copyright © 2016-2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
2021-12-11 14:43:32 +00:00
import { IdentityMap } from '@preserves/core';
2021-12-02 13:40:24 +00:00
import { Index } from './skeleton.js';
2021-12-09 21:12:41 +00:00
import { Actor, Assertion, Entity, Facet, Handle, LocalAction, Ref, Turn } from './actor.js';
import { Observe, toObserve } from '../gen/dataspace.js';
2021-12-02 23:55:42 +00:00
import * as P from '../gen/dataspacePatterns.js';
2021-12-11 15:49:24 +00:00
export type DataspaceOptions = {
tracer?: (event: '+' | '-' | '!',
assertion: Assertion,
dataspace: Dataspace,
is_significant: boolean) => void,
2021-12-11 14:43:32 +00:00
dumpIndex?: boolean,
};
2021-12-01 16:13:00 +00:00
export class Dataspace implements Partial<Entity> {
2021-12-11 14:43:32 +00:00
readonly options: DataspaceOptions;
2021-12-02 13:40:24 +00:00
readonly index = new Index();
readonly handleMap = new IdentityMap<Handle, Assertion>();
2021-12-02 13:40:24 +00:00
2021-12-11 14:43:32 +00:00
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();
2021-12-02 13:40:24 +00:00
}
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();
2021-12-02 13:40:24 +00:00
}
}
message(v: Assertion): void {
this.options.tracer?.('!', v, this, true);
this.index.deliverMessage(v);
}
2021-12-09 21:12:41 +00:00
2021-12-11 14:43:32 +00:00
static boot(bootProc: (ds: Ref) => void, options?: DataspaceOptions): Actor {
2021-12-09 21:12:41 +00:00
return Actor.boot(() => {
Turn.activeFacet.preventInertCheck();
2021-12-11 14:43:32 +00:00
const ds = Turn.active.ref(new Dataspace(options));
2021-12-09 21:12:41 +00:00
bootProc(ds);
});
}
}
2021-12-02 23:55:42 +00:00
export function assertionObserver(f: (a: Assertion) => LocalAction | undefined): Partial<Entity> {
2021-12-02 23:55:42 +00:00
const assertionMap = new IdentityMap<Handle, LocalAction>();
return {
assert(a: Assertion, h: Handle): void {
const g = f(a) ?? null;
2021-12-02 23:55:42 +00:00
if (g !== null) {
assertionMap.set(h, g);
}
},
retract(h: Handle): void {
assertionMap.get(h)?.();
2021-12-02 23:55:42 +00:00
assertionMap.delete(h);
},
};
}
export function assertionFacetObserver(f: (a: Assertion) => void, inertOk: boolean = true): Partial<Entity> {
2021-12-02 23:55:42 +00:00
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);
2021-12-02 23:55:42 +00:00
}));
},
retract(h: Handle): void {
2021-12-02 23:55:42 +00:00
const facet = facetMap.get(h);
if (facet) Turn.active.stop(facet);
2021-12-02 23:55:42 +00:00
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) }));
2021-12-02 23:55:42 +00:00
}