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

67 lines
1.7 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
2023-01-17 10:43:15 +00:00
/// SPDX-FileCopyrightText: Copyright © 2016-2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
2021-12-02 15:04:07 +00:00
import {
Pattern as P,
2021-12-02 23:55:42 +00:00
assertObserve,
2021-12-02 15:04:07 +00:00
Record,
2021-12-09 21:12:41 +00:00
Dataspace, Turn,
2021-12-02 15:04:07 +00:00
} from '..';
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-12-09 21:12:41 +00:00
Dataspace.boot(ds => {
Turn.active.spawn(() => {
Turn.activeFacet.actor.name = 'box';
const boxValue = Turn.active.field(0, 'value');
2021-12-02 15:04:07 +00:00
Turn.active.assertDataflow(() => {
2021-12-02 15:04:07 +00:00
// console.log('recomputing published BoxState', boxValue.value);
return {
target: ds,
assertion: BoxState(boxValue.value),
};
});
2021-12-02 15:04:07 +00:00
Turn.active.dataflow(() => {
2021-12-02 15:04:07 +00:00
// console.log('dataflow saw new value', boxValue.value);
if (boxValue.value === N) {
Turn.active.stop(Turn.activeFacet, () => {
console.log('terminated box root facet');
});
}
});
2021-12-02 15:04:07 +00:00
assertObserve(ds, P.rec(SetBox.constructorInfo.label, P.bind()), {
message([v]) {
2021-12-02 23:55:42 +00:00
boxValue.value = v;
// console.log('box updated value', v);
}
});
});
Turn.active.spawn(() => {
Turn.activeFacet.actor.name = 'client';
2021-12-02 15:04:07 +00:00
assertObserve(ds, P.rec(BoxState.constructorInfo.label, P.bind()), {
assert([v], _handle) {
2021-12-02 23:55:42 +00:00
// console.log('client sending SetBox', v + 1);
Turn.active.message(ds, SetBox(v + 1));
2021-12-02 23:55:42 +00:00
}
});
2021-12-02 15:04:07 +00:00
assertObserve(ds, P.rec(BoxState.constructorInfo.label, P._), {
retract() {
2021-12-02 23:55:42 +00:00
console.log('box gone');
console.timeEnd('box-and-client-' + N.toString());
}
});
});
2021-12-02 15:04:07 +00:00
});