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

36 lines
1.2 KiB
TypeScript

/// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
import { IdentityMap } from '@preserves/core';
import { Index } from './skeleton.js';
import { Assertion, Entity, Handle, Turn } from './actor.js';
import { Observe, toObserve } from '../gen/dataspace.js';
export class Dataspace implements Partial<Entity> {
readonly index = new Index();
readonly handleMap = new IdentityMap<Handle, [Assertion, Observe | undefined]>();
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);
}
}