Optional tracing/dumping for Dataspace
This commit is contained in:
parent
97cfb19852
commit
f540b41d73
|
@ -1,24 +1,34 @@
|
|||
/// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
/// 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 { 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<Entity> {
|
||||
readonly options: DataspaceOptions;
|
||||
readonly index = new Index();
|
||||
readonly handleMap = new IdentityMap<Handle, [Assertion, Observe | undefined]>();
|
||||
|
||||
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<Entity> {
|
|||
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);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue