DataspaceObserver, for reflection
This commit is contained in:
parent
7b8526dfcf
commit
1de1e9d382
|
@ -15,11 +15,41 @@ export type DataspaceOptions = {
|
||||||
dumpIndex?: boolean,
|
dumpIndex?: boolean,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export class DataspaceObserver implements IndexObserver<Turn> {
|
||||||
|
readonly captureMap = new KeyedDictionary<Array<AnyValue>, Handle, Ref>();
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public readonly target: Ref,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
onAssert(captures: Assertion[], t: Turn) {
|
||||||
|
this.captureMap.set(captures, t.assert(this.target, captures));
|
||||||
|
}
|
||||||
|
|
||||||
|
onRetract(vs: Assertion[], t: Turn) {
|
||||||
|
t.retract(this.captureMap.get(vs));
|
||||||
|
this.captureMap.delete(vs);
|
||||||
|
}
|
||||||
|
|
||||||
|
onMessage(vs: Assertion[], t: Turn) {
|
||||||
|
t.message(this.target, vs);
|
||||||
|
}
|
||||||
|
|
||||||
|
onRemoval(t: Turn) {
|
||||||
|
this.captureMap.forEach((handle, _captures) => t.retract(handle));
|
||||||
|
}
|
||||||
|
|
||||||
|
dump(): string {
|
||||||
|
return Array.from(this.captureMap.entries()).map((handle, values) =>
|
||||||
|
`captured ${stringify(values)} handle ${handle}`).join('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class Dataspace implements Partial<Entity> {
|
export class Dataspace implements Partial<Entity> {
|
||||||
readonly options: DataspaceOptions;
|
readonly options: DataspaceOptions;
|
||||||
readonly index = new Index();
|
readonly index = new Index();
|
||||||
readonly handleMap = new IdentityMap<Handle, Assertion>();
|
readonly handleMap = new IdentityMap<Handle, Assertion>();
|
||||||
readonly observerMap = new IdentityMap<Ref, IndexObserver<Turn>>();
|
readonly observerMap = new IdentityMap<Ref, DataspaceObserver>();
|
||||||
readonly data = this;
|
readonly data = this;
|
||||||
|
|
||||||
constructor(options?: DataspaceOptions) {
|
constructor(options?: DataspaceOptions) {
|
||||||
|
@ -33,26 +63,7 @@ export class Dataspace implements Partial<Entity> {
|
||||||
const o = toObserve(v);
|
const o = toObserve(v);
|
||||||
if (o !== void 0) {
|
if (o !== void 0) {
|
||||||
const target = o.observer;
|
const target = o.observer;
|
||||||
const captureMap = new KeyedDictionary<Array<AnyValue>, Handle, Ref>();
|
const observer = new DataspaceObserver(target);
|
||||||
const observer: IndexObserver<Turn> = {
|
|
||||||
onAssert(captures, t: Turn) {
|
|
||||||
captureMap.set(captures, t.assert(target, captures));
|
|
||||||
},
|
|
||||||
onRetract(vs, t: Turn) {
|
|
||||||
t.retract(captureMap.get(vs));
|
|
||||||
captureMap.delete(vs);
|
|
||||||
},
|
|
||||||
onMessage(vs, t: Turn) {
|
|
||||||
t.message(target, vs);
|
|
||||||
},
|
|
||||||
onRemoval(t: Turn) {
|
|
||||||
captureMap.forEach((handle, _captures) => t.retract(handle));
|
|
||||||
},
|
|
||||||
dump(): string {
|
|
||||||
return Array.from(captureMap.entries()).map((handle, values) =>
|
|
||||||
`captured ${stringify(values)} handle ${handle}`).join('\n');
|
|
||||||
},
|
|
||||||
};
|
|
||||||
this.observerMap.set(target, observer);
|
this.observerMap.set(target, observer);
|
||||||
this.index.addObserver(o.pattern, observer, Turn.active);
|
this.index.addObserver(o.pattern, observer, Turn.active);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue