novy-syndicate/main.ts

115 lines
4.6 KiB
TypeScript
Raw Normal View History

2021-02-23 14:53:42 +00:00
import { Actor, Assertion, Entity, Handle, Ref, Turn } from './actor.js';
2021-02-22 18:37:47 +00:00
import { Dictionary, IdentityMap, is, Record } from 'preserves';
import { Bag, ChangeDescription } from './bag';
2021-02-22 09:12:10 +00:00
const Observe = Record.makeConstructor<Ref>('Observe', ['label', 'observer']);
2021-02-22 18:37:47 +00:00
2021-02-23 07:52:54 +00:00
class Dataspace implements Entity {
readonly handleMap: IdentityMap<Handle, Record<Ref>> = new IdentityMap();
readonly assertions = new Bag<Ref>();
readonly subscriptions: Dictionary<Map<Ref, Dictionary<Handle>>> = new Dictionary();
2021-02-22 18:37:47 +00:00
2021-02-23 14:53:42 +00:00
assert(turn: Turn, rec: Assertion, handle: Handle): void {
if (!Record.isRecord<Ref>(rec)) return;
2021-02-23 07:52:54 +00:00
this.handleMap.set(handle, rec);
if (this.assertions.change(rec, +1) !== ChangeDescription.ABSENT_TO_PRESENT) return;
if (Observe.isClassOf(rec)) {
const label = Observe._.label(rec)!;
const observer = Observe._.observer(rec) as Ref;
2021-02-23 07:52:54 +00:00
const seen = new Dictionary<Handle>();
if (!this.subscriptions.has(label)) this.subscriptions.set(label, new Map());
this.subscriptions.get(label)!.set(observer, seen);
this.assertions.forEach((_count, prev) =>
is((prev as Record<Ref>).label, label)
2021-02-23 07:52:54 +00:00
&& seen.set(prev, turn.assert(observer, prev)));
}
this.subscriptions.get(rec.label)?.forEach((seen, peer) =>
seen.has(rec) || seen.set(rec, turn.assert(peer, rec)));
}
2021-02-22 18:37:47 +00:00
2021-02-23 14:53:42 +00:00
retract(turn: Turn, upstreamHandle: Handle): void {
2021-02-23 07:52:54 +00:00
const rec = this.handleMap.get(upstreamHandle);
if (rec === void 0) return;
this.handleMap.delete(upstreamHandle);
if (this.assertions.change(rec, -1) !== ChangeDescription.PRESENT_TO_ABSENT) return;
this.subscriptions.get(rec.label)?.forEach((seen, _peer) => {
const downstreamHandle = seen.get(rec);
if (downstreamHandle !== void 0) {
turn.retract(downstreamHandle);
seen.delete(rec);
2021-02-22 18:37:47 +00:00
}
2021-02-23 07:52:54 +00:00
});
if (Observe.isClassOf(rec)) {
let peerMap = this.subscriptions.get(Observe._.label(rec)!)!;
peerMap.delete(Observe._.observer(rec) as Ref);
2021-02-23 07:52:54 +00:00
if (peerMap.size === 0) this.subscriptions.delete(Observe._.label(rec)!);
2021-02-22 18:37:47 +00:00
}
2021-02-23 07:52:54 +00:00
}
2021-02-23 14:53:42 +00:00
message(turn: Turn, rec: Assertion): void {
if (!Record.isRecord<Ref>(rec)) return;
2021-02-23 07:52:54 +00:00
this.subscriptions.get(rec.label)?.forEach((_seen, peer) => turn.message(peer, rec));
}
2021-02-22 18:37:47 +00:00
}
const BoxState = Record.makeConstructor<Ref>('BoxState', ['value']);
const SetBox = Record.makeConstructor<Ref>('SetBox', ['newValue']);
2021-02-22 18:37:47 +00:00
let startTime = Date.now();
let prevValue = 0;
2021-02-23 08:34:36 +00:00
const LIMIT = 500000;
2021-02-23 09:56:12 +00:00
Turn.for(new Actor(), async (t: Turn) => {
2021-02-23 14:38:57 +00:00
const ds = t.ref(new Dataspace());
2021-02-22 18:37:47 +00:00
// Box
t.spawn(t => {
console.log('Spawning Box');
let valueHandle: Handle | undefined;
2021-02-22 21:30:36 +00:00
function setValue(t: Turn, value: number) {
2021-02-22 18:37:47 +00:00
valueHandle = t.replace(ds, valueHandle, BoxState(value));
}
setValue(t, 0);
t.assert(ds, Observe(SetBox.constructorInfo.label, t.ref({
2021-02-23 14:53:42 +00:00
message(t: Turn, [newValue]: [number]): void {
2021-02-23 14:38:57 +00:00
// console.log(`Box: got ${newValue}`);
2021-02-22 18:37:47 +00:00
if (newValue % 25000 === 0) {
const endTime = Date.now();
const delta = (endTime - startTime) / 1000.0;
const count = newValue - prevValue;
prevValue = newValue;
startTime = endTime;
console.log(`Box: got ${newValue} (${count / delta} Hz)`);
}
2021-02-23 08:34:36 +00:00
if (newValue === LIMIT - 20000) t.quit();
2021-02-22 18:37:47 +00:00
setValue(t, newValue);
}
})));
});
// Client
t.spawn(t => {
console.log('Spawning Client');
let count = 0;
t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({
2021-02-23 14:53:42 +00:00
assert(t: Turn, [currentValue]: [number]): void {
2021-02-22 18:37:47 +00:00
// console.log(`Client: got ${currentValue}`);
2021-02-23 08:34:36 +00:00
if (currentValue === LIMIT) {
2021-02-22 18:37:47 +00:00
console.log(`Client: quitting at limit`);
t.quit();
} else {
t.message(ds, SetBox(currentValue + 1));
}
2021-02-22 19:08:11 +00:00
}
2021-02-22 18:37:47 +00:00
})));
t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({
2021-02-23 14:53:42 +00:00
assert(_t: Turn, _assertion: Assertion): void { count++; },
retract(t: Turn, _handle: Handle) {
2021-02-22 18:37:47 +00:00
if (--count === 0) {
console.log('Client: detected box termination');
t.quit();
}
},
})));
2021-02-22 09:12:10 +00:00
});
});