Optional tracing/dumping for Dataspace

This commit is contained in:
Tony Garnock-Jones 2021-12-11 15:43:32 +01:00
parent 97cfb19852
commit f540b41d73
1 changed files with 18 additions and 8 deletions

View File

@ -1,24 +1,34 @@
/// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com> /// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
import { IdentityMap, stringify } from '@preserves/core'; import { IdentityMap } from '@preserves/core';
import { Index } from './skeleton.js'; import { Index } from './skeleton.js';
import { Actor, Assertion, Entity, Facet, Handle, LocalAction, Ref, Turn } from './actor.js'; import { Actor, Assertion, Entity, Facet, Handle, LocalAction, Ref, Turn } from './actor.js';
import { fromObserve, Observe, toObserve } from '../gen/dataspace.js'; import { fromObserve, Observe, toObserve } from '../gen/dataspace.js';
import * as P from '../gen/dataspacePatterns.js'; import * as P from '../gen/dataspacePatterns.js';
export type DataspaceOptions {
tracer?: (event: '+' | '-' | '!', assertion: Assertion, dataspace: Dataspace) => void,
dumpIndex?: boolean,
};
export class Dataspace implements Partial<Entity> { export class Dataspace implements Partial<Entity> {
readonly options: DataspaceOptions;
readonly index = new Index(); readonly index = new Index();
readonly handleMap = new IdentityMap<Handle, [Assertion, Observe | undefined]>(); readonly handleMap = new IdentityMap<Handle, [Assertion, Observe | undefined]>();
constructor(options?: DataspaceOptions) {
this.options = options ?? {};
}
assert(v: Assertion, handle: Handle): void { assert(v: Assertion, handle: Handle): void {
// console.log('+', stringify(v)); this.options.tracer?.('+', v, this);
this.index.addAssertion(v); this.index.addAssertion(v);
const o = toObserve(v); const o = toObserve(v);
if (o !== void 0) { if (o !== void 0) {
this.index.addObserver(o.pattern, o.observer); this.index.addObserver(o.pattern, o.observer);
} }
// this.index.dump(); if (this.options.dumpIndex ?? false) this.index.dump();
this.handleMap.set(handle, [v, o]); this.handleMap.set(handle, [v, o]);
} }
@ -26,23 +36,23 @@ export class Dataspace implements Partial<Entity> {
const entry = this.handleMap.get(handle); const entry = this.handleMap.get(handle);
if (entry === void 0) return; if (entry === void 0) return;
const [v, o] = entry; const [v, o] = entry;
// console.log('-', stringify(v)); this.options.tracer?.('-', v, this);
if (o !== void 0) { if (o !== void 0) {
this.index.removeObserver(o.pattern, o.observer); this.index.removeObserver(o.pattern, o.observer);
} }
this.index.removeAssertion(v); this.index.removeAssertion(v);
// this.index.dump(); if (this.options.dumpIndex ?? false) this.index.dump();
} }
message(v: Assertion): void { message(v: Assertion): void {
// console.log('!', stringify(v)); this.options.tracer?.('!', v, this);
this.index.deliverMessage(v); this.index.deliverMessage(v);
} }
static boot(bootProc: (ds: Ref) => void): Actor { static boot(bootProc: (ds: Ref) => void, options?: DataspaceOptions): Actor {
return Actor.boot(() => { return Actor.boot(() => {
Turn.activeFacet.preventInertCheck(); Turn.activeFacet.preventInertCheck();
const ds = Turn.active.ref(new Dataspace()); const ds = Turn.active.ref(new Dataspace(options));
bootProc(ds); bootProc(ds);
}); });
} }