2021-03-02 08:50:23 +00:00
|
|
|
import { BoxState, SetBox } from "./box-protocol.js";
|
2021-03-04 10:29:04 +00:00
|
|
|
import { Assertion, Handle, Ref, Turn } from "./actor.js";
|
2021-03-02 08:50:23 +00:00
|
|
|
import { Observe } from "./dataspace.js";
|
|
|
|
|
|
|
|
let startTime = Date.now();
|
|
|
|
let prevValue = 0;
|
|
|
|
|
2021-03-04 10:29:04 +00:00
|
|
|
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);
|
2021-03-03 15:22:19 +00:00
|
|
|
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)`);
|
2021-03-02 08:50:23 +00:00
|
|
|
}
|
2021-03-03 15:22:19 +00:00
|
|
|
if (newValue === LIMIT) t.quit();
|
|
|
|
setValue(t, newValue);
|
|
|
|
}
|
|
|
|
})));
|
2021-03-02 08:50:23 +00:00
|
|
|
}
|