From f540b41d730fdc2752a127b46c63c0b4040d8297 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sat, 11 Dec 2021 15:43:32 +0100 Subject: [PATCH] Optional tracing/dumping for Dataspace --- packages/core/src/runtime/dataspace.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/core/src/runtime/dataspace.ts b/packages/core/src/runtime/dataspace.ts index 9a7ed18..431b082 100644 --- a/packages/core/src/runtime/dataspace.ts +++ b/packages/core/src/runtime/dataspace.ts @@ -1,24 +1,34 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones -import { IdentityMap, stringify } from '@preserves/core'; +import { IdentityMap } from '@preserves/core'; import { Index } from './skeleton.js'; import { Actor, 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 type DataspaceOptions { + tracer?: (event: '+' | '-' | '!', assertion: Assertion, dataspace: Dataspace) => void, + dumpIndex?: boolean, +}; + export class Dataspace implements Partial { + readonly options: DataspaceOptions; readonly index = new Index(); readonly handleMap = new IdentityMap(); + constructor(options?: DataspaceOptions) { + this.options = options ?? {}; + } + assert(v: Assertion, handle: Handle): void { - // console.log('+', stringify(v)); + this.options.tracer?.('+', v, this); this.index.addAssertion(v); const o = toObserve(v); if (o !== void 0) { this.index.addObserver(o.pattern, o.observer); } - // this.index.dump(); + if (this.options.dumpIndex ?? false) this.index.dump(); this.handleMap.set(handle, [v, o]); } @@ -26,23 +36,23 @@ export class Dataspace implements Partial { const entry = this.handleMap.get(handle); if (entry === void 0) return; const [v, o] = entry; - // console.log('-', stringify(v)); + this.options.tracer?.('-', v, this); if (o !== void 0) { this.index.removeObserver(o.pattern, o.observer); } this.index.removeAssertion(v); - // this.index.dump(); + if (this.options.dumpIndex ?? false) this.index.dump(); } message(v: Assertion): void { - // console.log('!', stringify(v)); + this.options.tracer?.('!', v, this); this.index.deliverMessage(v); } - static boot(bootProc: (ds: Ref) => void): Actor { + static boot(bootProc: (ds: Ref) => void, options?: DataspaceOptions): Actor { return Actor.boot(() => { Turn.activeFacet.preventInertCheck(); - const ds = Turn.active.ref(new Dataspace()); + const ds = Turn.active.ref(new Dataspace(options)); bootProc(ds); }); }