2021-04-21 19:22:30 +00:00
|
|
|
import { BoxState, $SetBox, fromBoxState } from "../gen/box-protocol.js";
|
|
|
|
import { fromObserve, Observe } from "../gen/dataspace.js";
|
|
|
|
import { Assertion, Handle, Ref, Turn } from "../runtime/actor.js";
|
2021-03-02 08:50:23 +00:00
|
|
|
|
|
|
|
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) {
|
2021-03-23 11:18:57 +00:00
|
|
|
valueHandle = t.replace(ds, valueHandle, fromBoxState(BoxState(value)));
|
2021-03-03 15:22:19 +00:00
|
|
|
}
|
|
|
|
setValue(t, 0);
|
2021-03-23 18:18:26 +00:00
|
|
|
t.assert(ds, fromObserve(Observe({
|
|
|
|
label: $SetBox,
|
|
|
|
observer: 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;
|
2021-04-16 18:29:16 +00:00
|
|
|
console.log(`Box ${t.activeFacet.id}: got ${newValue} (${count / delta} Hz)`);
|
2021-03-23 18:18:26 +00:00
|
|
|
}
|
2021-04-16 18:29:16 +00:00
|
|
|
if (newValue === LIMIT) t.stopActor();
|
2021-03-23 18:18:26 +00:00
|
|
|
setValue(t, newValue);
|
2021-03-02 08:50:23 +00:00
|
|
|
}
|
2021-03-23 18:18:26 +00:00
|
|
|
})
|
|
|
|
})));
|
2021-03-02 08:50:23 +00:00
|
|
|
}
|