From 595a13dfd61fb9bdc7202e1f34adf704bc057c0d Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 14 Jan 2021 15:27:43 +0100 Subject: [PATCH] Approach new syntax --- packages/core/examples/box-and-client.js | 41 +++++++++------- .../core/examples/box-and-client.syndicate.js | 48 +++++++++++++++++++ 2 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 packages/core/examples/box-and-client.syndicate.js diff --git a/packages/core/examples/box-and-client.js b/packages/core/examples/box-and-client.js index b155ee5..e89a17b 100755 --- a/packages/core/examples/box-and-client.js +++ b/packages/core/examples/box-and-client.js @@ -28,26 +28,26 @@ const N = 100000; console.time('box-and-client-' + N.toString()); -new Ground(groundRoot => { - groundRoot.spawn('box', function (boxRoot) { - boxRoot.actor.dataspace.declareField(this, 'value', 0); - boxRoot.addEndpoint(() => { +export function boot(thisFacet) { + thisFacet.spawn('box', function (thisFacet) { + thisFacet.actor.dataspace.declareField(this, 'value', 0); + thisFacet.addEndpoint(() => { // console.log('recomputing published BoxState', this.value); return { assertion: BoxState(this.value), analysis: null }; }); - boxRoot.addDataflow(() => { + thisFacet.addDataflow(() => { // console.log('dataflow saw new value', this.value); if (this.value === N) { - boxRoot.stop(() => { + thisFacet.stop(() => { console.log('terminated box root facet'); }); } }); - boxRoot.addEndpoint(() => { + thisFacet.addEndpoint(() => { let analysis = Skeleton.analyzeAssertion(SetBox(_$)); - analysis.callback = boxRoot.wrap((facet, evt, vs) => { + analysis.callback = thisFacet.wrap((thisFacet, evt, vs) => { if (evt === Skeleton.EventType.MESSAGE) { - boxRoot.scheduleScript(() => { + thisFacet.scheduleScript(() => { this.value = vs[0]; // console.log('box updated value', vs[0]); }); @@ -57,24 +57,24 @@ new Ground(groundRoot => { }); }); - groundRoot.spawn('client', function (clientRoot) { - clientRoot.addEndpoint(() => { + thisFacet.spawn('client', function (thisFacet) { + thisFacet.addEndpoint(() => { let analysis = Skeleton.analyzeAssertion(BoxState(_$)); - analysis.callback = clientRoot.wrap((facet, evt, vs) => { + analysis.callback = thisFacet.wrap((thisFacet, evt, vs) => { if (evt === Skeleton.EventType.ADDED) { - clientRoot.scheduleScript(() => { + thisFacet.scheduleScript(() => { // console.log('client sending SetBox', vs[0] + 1); - clientRoot.send(SetBox(vs[0] + 1)); + thisFacet.send(SetBox(vs[0] + 1)); }); } }); return { assertion: Observe(BoxState(_$)), analysis }; }); - clientRoot.addEndpoint(() => { + thisFacet.addEndpoint(() => { let analysis = Skeleton.analyzeAssertion(BoxState(__)); - analysis.callback = clientRoot.wrap((facet, evt, _vs) => { + analysis.callback = thisFacet.wrap((thisFacet, evt, _vs) => { if (evt === Skeleton.EventType.REMOVED) { - clientRoot.scheduleScript(() => { + thisFacet.scheduleScript(() => { console.log('box gone'); }); } @@ -82,4 +82,9 @@ new Ground(groundRoot => { return { assertion: Observe(BoxState(__)), analysis }; }); }); -}).addStopHandler(() => console.timeEnd('box-and-client-' + N.toString())).start(); + + thisFacet.actor.dataspace.addStopHandler(() => + console.timeEnd('box-and-client-' + N.toString())); +} + +new Ground(boot).start(); diff --git a/packages/core/examples/box-and-client.syndicate.js b/packages/core/examples/box-and-client.syndicate.js new file mode 100644 index 0000000..337d274 --- /dev/null +++ b/packages/core/examples/box-and-client.syndicate.js @@ -0,0 +1,48 @@ +#!/usr/bin/env -S node --es-module-specifier-resolution=node +//--------------------------------------------------------------------------- +// @syndicate-lang/core, an implementation of Syndicate dataspaces for JS. +// Copyright (C) 2016-2021 Tony Garnock-Jones +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +//--------------------------------------------------------------------------- + +assertion type BoxState(value); +message type SetBox(newValue); + +const N = 100000; + +console.time('box-and-client-' + N.toString()); + +boot { + spawn named 'box' { + field this.value = 0; + assert BoxState(this.value); + dataflow { + if (this.value === N) { + stop { + console.log('terminated box root facet'); + } + } + } + on message SetBox($v) => this.value = v; + } + + spawn named 'client' { + on asserted BoxState($v) => send SetBox(v + 1); + on retracted BoxState(_) => console.log('box gone'); + } + + thisFacet.actor.dataspace.addStopHandler(() => + console.timeEnd('box-and-client-' + N.toString())); +}