diff --git a/packages/core/examples/box-and-client.js b/packages/core/examples/box-and-client.js index cd40f8c..22e9bbb 100644 --- a/packages/core/examples/box-and-client.js +++ b/packages/core/examples/box-and-client.js @@ -21,70 +21,81 @@ const Immutable = require('immutable'); const Syndicate = require('../src/index.js'); const Skeleton = Syndicate.Skeleton; const Dataspace = Syndicate.Dataspace; -const Ground = Syndicate.Ground.Ground; -const Struct = Syndicate.Struct; -const __ = Syndicate.__; -const _$ = Syndicate._$; +const Ground = Syndicate.Ground; +const Record = Syndicate.Record; +const __ = Syndicate.Discard._instance; +const _$ = Syndicate.Capture(__); -const BoxState = Struct.makeConstructor('BoxState', ['value']); -const SetBox = Struct.makeConstructor('SetBox', ['newValue']); +const BoxState = Record.makeConstructor('BoxState', ['value']); +const SetBox = Record.makeConstructor('SetBox', ['newValue']); const N = 100000; console.time('box-and-client-' + N.toString()); +let _savedGlobalFacet = Dataspace._currentFacet; +Dataspace._currentFacet = new Syndicate._Dataspace.ActionCollector(); -new Ground(() => { - Dataspace.spawn('box', function () { - Dataspace.declareField(this, 'value', 0); - Dataspace.currentFacet().addEndpoint(() => { - return [BoxState(this.value), null]; - }); - Dataspace.currentFacet().addDataflow(() => { - if (this.value === N) { - Dataspace.currentFacet().stop(() => { - console.log('terminated box root facet'); +Dataspace.spawn('box', function () { + Dataspace.declareField(this, 'value', 0); + Dataspace.currentFacet().addEndpoint(() => { + return [BoxState(this.value), null]; + }); + Dataspace.currentFacet().addDataflow(() => { + if (this.value === N) { + Dataspace.currentFacet().stop(() => { + console.log('terminated box root facet'); + }); + } + }); + Dataspace.currentFacet().addEndpoint(() => { + let handler = Skeleton.analyzeAssertion(SetBox(_$)); + handler.callback = Dataspace.wrap((evt, vs) => { + if (evt === Skeleton.EVENT_MESSAGE) { + Dataspace.currentFacet().actor.scheduleScript(() => { + this.value = vs.get(0); + // console.log('box updated value', vs.get(0)); }); } }); - Dataspace.currentFacet().addEndpoint(() => { - let handler = Skeleton.analyzeAssertion(SetBox(_$)); - handler.callback = Dataspace.wrap((evt, vs) => { - if (evt === Skeleton.EVENT_MESSAGE) { - Dataspace.currentFacet().actor.scheduleScript(() => { - this.value = vs.get(0); - // console.log('box updated value', vs.get(0)); - }); - } - }); - return [Syndicate.Observe(SetBox(_$)), handler]; - }); + return [Syndicate.Observe(SetBox(_$)), handler]; + }); +}); + +Dataspace.spawn('client', () => { + Dataspace.currentFacet().addEndpoint(() => { + let handler = Skeleton.analyzeAssertion(BoxState(_$)); + handler.callback = Dataspace.wrap((evt, vs) => { + if (evt === Skeleton.EVENT_ADDED) { + Dataspace.currentFacet().actor.scheduleScript(() => { + // console.log('client sending SetBox', vs.get(0) + 1); + Dataspace.send(SetBox(vs.get(0) + 1)); + }); + } + }); + return [Syndicate.Observe(BoxState(_$)), handler]; + }); + Dataspace.currentFacet().addEndpoint(() => { + let handler = Skeleton.analyzeAssertion(BoxState(__)); + handler.callback = Dataspace.wrap((evt, vs) => { + if (evt === Skeleton.EVENT_REMOVED) { + Dataspace.currentFacet().actor.scheduleScript(() => { + console.log('box gone'); + }); + } + }); + return [Syndicate.Observe(BoxState(__)), handler]; + }); +}); + +module.exports[Dataspace.BootSteps] = { + module: module, + steps: Dataspace._currentFacet.actions +}; +Dataspace._currentFacet = _savedGlobalFacet; +_savedGlobalFacet = null; + +Ground.bootModule(module, (g) => { + g.addStopHandler(() => { + console.timeEnd('box-and-client-' + N.toString()); }); - - Dataspace.spawn('client', () => { - Dataspace.currentFacet().addEndpoint(() => { - let handler = Skeleton.analyzeAssertion(BoxState(_$)); - handler.callback = Dataspace.wrap((evt, vs) => { - if (evt === Skeleton.EVENT_ADDED) { - Dataspace.currentFacet().actor.scheduleScript(() => { - // console.log('client sending SetBox', vs.get(0) + 1); - Dataspace.send(SetBox(vs.get(0) + 1)); - }); - } - }); - return [Syndicate.Observe(BoxState(_$)), handler]; - }); - Dataspace.currentFacet().addEndpoint(() => { - let handler = Skeleton.analyzeAssertion(BoxState(__)); - handler.callback = Dataspace.wrap((evt, vs) => { - if (evt === Skeleton.EVENT_REMOVED) { - Dataspace.currentFacet().actor.scheduleScript(() => { - console.log('box gone'); - }); - } - }); - return [Syndicate.Observe(BoxState(__)), handler]; - }); - }); -}).start().addStopHandler(() => { - console.timeEnd('box-and-client-' + N.toString()); }); diff --git a/packages/core/src/ground.js b/packages/core/src/ground.js index 871de72..943ebc9 100644 --- a/packages/core/src/ground.js +++ b/packages/core/src/ground.js @@ -88,7 +88,7 @@ Ground.prototype.addStopHandler = function (h) { this.stopHandlers.push(h); }; -function bootModule(mod) { +function bootModule(mod, k) { let g = new Ground(() => { Worker.spawnWorkerRelay(); if (Dataspace.BootSteps in mod) { @@ -104,7 +104,10 @@ function bootModule(mod) { } }); if (typeof document !== 'undefined') { - document.addEventListener("DOMContentLoaded", (e) => { g.start(); }); + document.addEventListener("DOMContentLoaded", (e) => { + g.start(); + if (k) k(g); + }); } else { process.on('SIGQUIT', () => { console.log('---------------------------------------------------------------------------'); @@ -119,6 +122,7 @@ function bootModule(mod) { // sp.stdin.end(g._dotGraph()); }); g.start(); + if (k) k(g); } }