Run box/client in the actor they are spawned in, for Worker termination purposes
This commit is contained in:
parent
b2add0e463
commit
26f8cd81d2
42
src/box.ts
42
src/box.ts
|
@ -6,27 +6,25 @@ let startTime = Date.now();
|
|||
let prevValue = 0;
|
||||
|
||||
export default function (t: Turn, [ds, LIMIT, REPORT_EVERY]: [Ref, number, number]) {
|
||||
t.spawn(t => {
|
||||
console.log('Spawning Box');
|
||||
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);
|
||||
console.log('Spawning Box');
|
||||
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);
|
||||
}
|
||||
})));
|
||||
}
|
||||
|
|
|
@ -3,27 +3,25 @@ import { Observe } from "./dataspace.js";
|
|||
import { Assertion, Ref, Turn } from "./actor.js";
|
||||
|
||||
export default function (t: Turn, ds: Ref) {
|
||||
t.spawn(t => {
|
||||
console.log('Spawning Client');
|
||||
let count = 0;
|
||||
t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({
|
||||
assert(t: Turn, [currentValue]: [number]): void {
|
||||
// console.log(`Client ${t.actor.id}: got ${currentValue}`);
|
||||
t.message(ds, SetBox(currentValue + 1));
|
||||
console.log('Spawning Client');
|
||||
let count = 0;
|
||||
t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({
|
||||
assert(t: Turn, [currentValue]: [number]): void {
|
||||
// console.log(`Client ${t.actor.id}: got ${currentValue}`);
|
||||
t.message(ds, SetBox(currentValue + 1));
|
||||
}
|
||||
})));
|
||||
t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({
|
||||
assert(_t: Turn, _assertion: Assertion): void {
|
||||
count++;
|
||||
// console.log('inc to', count, _assertion);
|
||||
},
|
||||
retract(t: Turn) {
|
||||
if (--count === 0) {
|
||||
console.log(`Client ${t.actor.id}: detected box termination`);
|
||||
t.quit();
|
||||
}
|
||||
})));
|
||||
t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({
|
||||
assert(_t: Turn, _assertion: Assertion): void {
|
||||
count++;
|
||||
// console.log('inc to', count, _assertion);
|
||||
},
|
||||
retract(t: Turn) {
|
||||
if (--count === 0) {
|
||||
console.log(`Client ${t.actor.id}: detected box termination`);
|
||||
t.quit();
|
||||
}
|
||||
// console.log('dec to', count);
|
||||
},
|
||||
})));
|
||||
});
|
||||
// console.log('dec to', count);
|
||||
},
|
||||
})));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue