novy-syndicate/src/examples/client.ts

36 lines
1.3 KiB
TypeScript
Raw Normal View History

import { Embedded } from "@preserves/core";
2021-10-11 10:55:45 +00:00
import { $BoxState, fromSetBox, SetBox } from "../gen/examples/boxProtocol.js";
import { fromObserve, Observe, P } from "../runtime/dataspace.js";
import { Assertion, Ref, Turn } from "../runtime/actor.js";
2021-03-02 08:50:23 +00:00
export default function (t: Turn, ds_p: Embedded<Ref>) {
const ds = ds_p.embeddedValue;
console.log('Spawning Client');
let count = 0;
2021-03-23 18:18:26 +00:00
t.assert(ds, fromObserve(Observe({
2021-10-11 11:06:57 +00:00
pattern: P.rec($BoxState, P.bind()),
2021-03-23 18:18:26 +00:00
observer: t.ref({
assert(t: Turn, [currentValue]: [number]): void {
// console.log(`Client ${t.actor.id}: got ${currentValue}`);
t.message(ds, fromSetBox(SetBox(currentValue + 1)));
2021-03-02 08:50:23 +00:00
}
2021-03-23 18:18:26 +00:00
})
})));
t.assert(ds, fromObserve(Observe({
2021-10-11 10:55:45 +00:00
pattern: P.rec($BoxState, P.discard()),
2021-03-23 18:18:26 +00:00
observer: t.ref({
assert(_t: Turn, _assertion: Assertion): void {
count++;
// console.log('inc to', count, _assertion);
},
retract(t: Turn) {
if (--count === 0) {
2021-04-16 18:29:16 +00:00
console.log(`Client ${t.activeFacet.id}: detected box termination`);
t.stopActor();
2021-03-23 18:18:26 +00:00
}
// console.log('dec to', count);
},
})
})));
2021-03-02 08:50:23 +00:00
}