novy-syndicate/main.ts

99 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-02-24 20:48:55 +00:00
import {
Actor,
Assertion,
Handle,
Ref,
Turn,
attenuate,
rfilter,
PCompound,
CRec,
Lit,
2021-02-24 20:48:55 +00:00
} from './actor.js';
import { Dictionary, Record } from 'preserves';
import { Dataspace, Observe } from './dataspace.js';
2021-02-23 15:16:15 +00:00
const BoxState = Record.makeConstructor<{value: number}, Ref>()(
Symbol.for('BoxState'), ['value']);
const SetBox = Record.makeConstructor<{newValue: number}, Ref>()(
Symbol.for('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-22 18:37:47 +00:00
2021-02-23 15:16:15 +00:00
function spawnBox(t: Turn, ds: Ref) {
2021-02-22 18:37:47 +00:00
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);
}
})));
});
2021-02-23 15:16:15 +00:00
}
2021-02-22 18:37:47 +00:00
2021-02-23 15:16:15 +00:00
function spawnClient(t: Turn, ds: Ref) {
2021-02-22 18:37:47 +00:00
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
});
2021-02-23 15:16:15 +00:00
}
Turn.for(new Actor(), async (t: Turn) => {
const ds = t.ref(new Dataspace());
2021-02-24 20:48:55 +00:00
spawnBox(t, attenuate(
ds,
rfilter(PCompound(CRec(BoxState.constructorInfo.label,
BoxState.constructorInfo.arity),
new Dictionary()),
PCompound(CRec(Observe.constructorInfo.label,
Observe.constructorInfo.arity),
new Dictionary([[0, Lit(SetBox.constructorInfo.label)]])))));
2021-02-24 20:48:55 +00:00
spawnClient(t, attenuate(
ds,
rfilter(PCompound(CRec(SetBox.constructorInfo.label,
SetBox.constructorInfo.arity),
new Dictionary()),
PCompound(CRec(Observe.constructorInfo.label,
Observe.constructorInfo.arity),
new Dictionary([[0, Lit(BoxState.constructorInfo.label)]])))));
2021-02-22 09:12:10 +00:00
});