DataspaceObserver, for reflection
This commit is contained in:
parent
7b8526dfcf
commit
1de1e9d382
|
@ -15,11 +15,41 @@ export type DataspaceOptions = {
|
|||
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> {
|
||||
readonly options: DataspaceOptions;
|
||||
readonly index = new Index();
|
||||
readonly handleMap = new IdentityMap<Handle, Assertion>();
|
||||
readonly observerMap = new IdentityMap<Ref, IndexObserver<Turn>>();
|
||||
readonly observerMap = new IdentityMap<Ref, DataspaceObserver>();
|
||||
readonly data = this;
|
||||
|
||||
constructor(options?: DataspaceOptions) {
|
||||
|
@ -33,26 +63,7 @@ export class Dataspace implements Partial<Entity> {
|
|||
const o = toObserve(v);
|
||||
if (o !== void 0) {
|
||||
const target = o.observer;
|
||||
const captureMap = new KeyedDictionary<Array<AnyValue>, Handle, Ref>();
|
||||
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');
|
||||
},
|
||||
};
|
||||
const observer = new DataspaceObserver(target);
|
||||
this.observerMap.set(target, observer);
|
||||
this.index.addObserver(o.pattern, observer, Turn.active);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue