syndicate-js/packages/core/examples/box-and-client.js

76 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-01-17 13:14:02 +00:00
#!/usr/bin/env node
2021-12-01 16:24:29 +00:00
/// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
const { bootModule, Dataspace, Skeleton, Ground, Record, Discard, Capture, Observe } = require('..');
const __ = Discard._instance;
const _$ = Capture(__);
2021-03-03 09:28:10 +00:00
const BoxState = Record.makeConstructor()(Symbol.for('BoxState'), ['value']);
const SetBox = Record.makeConstructor()(Symbol.for('SetBox'), ['newValue']);
const N = 100000;
console.time('box-and-client-' + N.toString());
2021-01-17 13:14:02 +00:00
function boot(thisFacet) {
2021-01-14 14:27:43 +00:00
thisFacet.spawn('box', function (thisFacet) {
thisFacet.declareField(this, 'value', 0);
2021-01-14 14:27:43 +00:00
thisFacet.addEndpoint(() => {
// console.log('recomputing published BoxState', this.value);
return { assertion: BoxState(this.value), analysis: null };
});
2021-01-14 14:27:43 +00:00
thisFacet.addDataflow(() => {
// console.log('dataflow saw new value', this.value);
if (this.value === N) {
2021-01-14 14:27:43 +00:00
thisFacet.stop(() => {
console.log('terminated box root facet');
});
}
});
2021-01-14 14:27:43 +00:00
thisFacet.addEndpoint(() => {
let analysis = Skeleton.analyzeAssertion(SetBox(_$));
2021-01-14 14:27:43 +00:00
analysis.callback = thisFacet.wrap((thisFacet, evt, vs) => {
if (evt === Skeleton.EventType.MESSAGE) {
2021-01-14 14:27:43 +00:00
thisFacet.scheduleScript(() => {
this.value = vs[0];
// console.log('box updated value', vs[0]);
});
}
});
return { assertion: Observe(SetBox(_$)), analysis };
});
});
2021-01-14 14:27:43 +00:00
thisFacet.spawn('client', function (thisFacet) {
thisFacet.addEndpoint(() => {
let analysis = Skeleton.analyzeAssertion(BoxState(_$));
2021-01-16 16:46:18 +00:00
analysis.callback = thisFacet.wrap((thisFacet, evt, [v]) => {
if (evt === Skeleton.EventType.ADDED) {
2021-01-14 14:27:43 +00:00
thisFacet.scheduleScript(() => {
2021-01-16 16:46:18 +00:00
// console.log('client sending SetBox', v + 1);
thisFacet.send(SetBox(v + 1));
});
}
});
return { assertion: Observe(BoxState(_$)), analysis };
});
2021-01-14 14:27:43 +00:00
thisFacet.addEndpoint(() => {
let analysis = Skeleton.analyzeAssertion(BoxState(__));
2021-01-14 14:27:43 +00:00
analysis.callback = thisFacet.wrap((thisFacet, evt, _vs) => {
if (evt === Skeleton.EventType.REMOVED) {
2021-01-14 14:27:43 +00:00
thisFacet.scheduleScript(() => {
console.log('box gone');
});
}
});
return { assertion: Observe(BoxState(__)), analysis };
});
});
2021-01-14 14:27:43 +00:00
thisFacet.actor.dataspace.ground().addStopHandler(() =>
2021-01-14 14:27:43 +00:00
console.timeEnd('box-and-client-' + N.toString()));
}
2021-01-18 22:11:53 +00:00
bootModule(boot);