2018-10-27 19:32:12 +00:00
|
|
|
"use strict";
|
2018-11-01 14:55:45 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// @syndicate-lang/core, an implementation of Syndicate dataspaces for JS.
|
|
|
|
// Copyright (C) 2016-2018 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
|
|
|
//
|
|
|
|
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
//---------------------------------------------------------------------------
|
2018-10-27 19:32:12 +00:00
|
|
|
|
|
|
|
const Immutable = require('immutable');
|
2018-11-01 14:55:45 +00:00
|
|
|
const Syndicate = require('../src/index.js');
|
2018-10-27 19:32:12 +00:00
|
|
|
const Skeleton = Syndicate.Skeleton;
|
|
|
|
const Dataspace = Syndicate.Dataspace;
|
2019-06-13 21:18:57 +00:00
|
|
|
const Ground = Syndicate.Ground;
|
|
|
|
const Record = Syndicate.Record;
|
|
|
|
const __ = Syndicate.Discard._instance;
|
|
|
|
const _$ = Syndicate.Capture(__);
|
2018-10-27 19:32:12 +00:00
|
|
|
|
2019-06-13 21:18:57 +00:00
|
|
|
const BoxState = Record.makeConstructor('BoxState', ['value']);
|
|
|
|
const SetBox = Record.makeConstructor('SetBox', ['newValue']);
|
2018-10-27 19:32:12 +00:00
|
|
|
|
|
|
|
const N = 100000;
|
|
|
|
|
|
|
|
console.time('box-and-client-' + N.toString());
|
2019-06-13 21:18:57 +00:00
|
|
|
let _savedGlobalFacet = Dataspace._currentFacet;
|
|
|
|
Dataspace._currentFacet = new Syndicate._Dataspace.ActionCollector();
|
2018-10-27 19:32:12 +00:00
|
|
|
|
2019-06-13 21:18:57 +00:00
|
|
|
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));
|
2018-10-27 19:32:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2019-06-13 21:18:57 +00:00
|
|
|
return [Syndicate.Observe(SetBox(_$)), handler];
|
2018-10-27 19:32:12 +00:00
|
|
|
});
|
2019-06-13 21:18:57 +00:00
|
|
|
});
|
2018-10-27 19:32:12 +00:00
|
|
|
|
2019-06-13 21:18:57 +00:00
|
|
|
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));
|
|
|
|
});
|
|
|
|
}
|
2018-10-27 19:32:12 +00:00
|
|
|
});
|
2019-06-13 21:18:57 +00:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
}
|
2018-11-02 00:16:00 +00:00
|
|
|
});
|
2019-06-13 21:18:57 +00:00
|
|
|
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());
|
2018-10-27 19:32:12 +00:00
|
|
|
});
|
2018-11-01 23:30:17 +00:00
|
|
|
});
|