From a3964326999c9eeefa00417f6396117bb27a1c85 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 1 Nov 2018 16:57:07 +0000 Subject: [PATCH] Asynchronous ground dataspace --- examples/box-and-client.js | 15 ++++------- src/ground.js | 51 ++++++++++++++++++++++++++++++++++++++ src/index.js | 2 ++ 3 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 src/ground.js diff --git a/examples/box-and-client.js b/examples/box-and-client.js index eb494c9..2fdc29e 100644 --- a/examples/box-and-client.js +++ b/examples/box-and-client.js @@ -21,6 +21,7 @@ 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._$; @@ -32,7 +33,7 @@ const N = 100000; console.time('box-and-client-' + N.toString()); -let ds = new Dataspace(() => { +new Ground(() => { Dataspace.spawn('box', function () { Dataspace.declareField(this, 'value', 0); Dataspace.currentFacet().addEndpoint(() => { @@ -73,12 +74,6 @@ let ds = new Dataspace(() => { return [Syndicate.Observe(BoxState(_$)), handler]; }); }); -}); - -// console.log('--- starting ---'); -while (ds.runScripts()) { - // console.log('--- runScripts boundary ---'); -} -// console.log('--- done ---'); - -console.timeEnd('box-and-client-' + N.toString()); +}).start().stopHandler = () => { + console.timeEnd('box-and-client-' + N.toString()); +}; diff --git a/src/ground.js b/src/ground.js new file mode 100644 index 0000000..c6092a3 --- /dev/null +++ b/src/ground.js @@ -0,0 +1,51 @@ +"use strict"; + +const Immutable = require('immutable'); +const Dataspace = require('./dataspace.js').Dataspace; + +function Ground(bootProc) { + this.stepperId = null; + this.stepping = false; + this.startingFuel = 1000; + this.dataspace = new Dataspace(bootProc); + this.stopHandler = function () {}; +} + +Ground.prototype.start = function () { + if (!this.stepperId) { + this.stepperId = setTimeout(() => { + this.stepperId = null; + this._step(); + }, 0); + } + return this; // allows chaining start() immediately after construction +}; + +Ground.prototype._step = function () { + this.stepping = true; + try { + let stillBusy = false; + for (var fuel = this.startingFuel; fuel > 0; fuel--) { + stillBusy = this.dataspace.runScripts(); + if (!stillBusy) break; + } + if (stillBusy) { + this.start(); + } else { + if (this.stopHandler) { + this.stopHandler(this); + } + } + } finally { + this.stepping = false; + } +}; + +Ground.prototype.stop = function () { + if (this.stepperId) { + clearTimeout(this.stepperId); + this.stepperId = null; + } +}; + +module.exports.Ground = Ground; diff --git a/src/index.js b/src/index.js index 61a110a..f327be4 100644 --- a/src/index.js +++ b/src/index.js @@ -20,6 +20,7 @@ const Struct = require('./struct.js'); const Skeleton = require('./skeleton.js'); const Dataspace = require('./dataspace.js'); +const Ground = require('./ground.js'); const Assertions = require('./assertions.js'); module.exports.Immutable = require('immutable'); @@ -33,6 +34,7 @@ module.exports._$ = Skeleton._$; module.exports._Dataspace = Dataspace; module.exports.Dataspace = Dataspace.Dataspace; +module.exports.Ground = Ground; module.exports._Assertions = Assertions; module.exports.Observe = Assertions.Observe;