novy-syndicate/src/box.ts

34 lines
1.3 KiB
TypeScript

import { BoxState, SetBox } from "./gen/box-protocol.js";
import { Assertion, Handle, Ref, Turn } from "./actor.js";
import { Observe } from "./dataspace.js";
let startTime = Date.now();
let prevValue = 0;
export default function (t: Turn, arg: Assertion) {
const [ds, LIMIT, REPORT_EVERY]: [Ref, number, number] = Array.isArray(arg) && arg.length === 3
? arg as any
: [arg, 50000, 2500];
console.log('Spawning Box', LIMIT, REPORT_EVERY);
let valueHandle: Handle | undefined;
function setValue(t: Turn, value: number) {
valueHandle = t.replace(ds, valueHandle, BoxState(value));
}
setValue(t, 0);
t.assert(ds, Observe(SetBox.constructorInfo.label, t.ref({
message(t: Turn, [newValue]: [number]): void {
// console.log(`Box ${t.actor.id}: got ${newValue}`);
if (newValue % REPORT_EVERY === 0) {
const endTime = Date.now();
const delta = (endTime - startTime) / 1000.0;
const count = newValue - prevValue;
prevValue = newValue;
startTime = endTime;
console.log(`Box ${t.actor.id}: got ${newValue} (${count / delta} Hz)`);
}
if (newValue === LIMIT) t.quit();
setValue(t, newValue);
}
})));
}