diff --git a/core/Collexion.js b/core/Collexion.js index 7186370..0b9b7e6 100644 --- a/core/Collexion.js +++ b/core/Collexion.js @@ -1,34 +1,63 @@ const {Signale} = require('signale'); const log = new Signale({ - scope: 'EXEC' + scope: 'COLLEXION' }); const path = require('path'); const fs = require('fs'); const uuid = require('uuid'); - -const timeout = 3000; const root = (typeof window === 'undefined' ? global : window) const {Entity} = require('./entity.js') - -class System { - constructor(cache) { - this.cache = cache; - this.entities = {}; - for(const uuid of cache.getInstances()) { - this.loadEntity(uuid) - } +class Link { + constructor(symbol) { + this.symbol = symbol; } - async send(name, destination, options) { - // log.debug(`sending ${name}`); + toString() { + return "" + this.symbol; + } +} - if(!(destination in this.entities)) { - this.loadEntity(destination); - log.debug('loading entity...') +class Collexion { + constructor() { + // this.cache = cache; + this.entities = {}; + // for(const uuid of cache.getInstances()) { + // this.loadEntity(uuid) + // } + } + + createInstances(template) { + const instances = {}; + + for(const symbol in template) { + const instTemplate = template[symbol]; + const _class = instTemplate.Code; + const inst = new _class(this); + inst._data = instTemplate.Data; + instances[symbol] = inst; } - this.entities[destination].dispatch(name, options); + for(const symbol in instances) { + const inst = instances[symbol]; + inst.start() + } + + for(const symbol in instances) { + const inst = instances[symbol]; + const instTemplate = template[symbol]; + inst._links = instTemplate.Links; + + for (const linkKey in inst._links) { + const link = inst._links[linkKey]; + inst._links[linkKey] = instances[link]; + } + } + + for(const symbol in instances) { + const inst = instances[symbol]; + inst.connected() + } } loadEntity(uuid) { @@ -51,4 +80,4 @@ class System { } -module.exports = {System}; \ No newline at end of file +module.exports = {Collexion, Link}; \ No newline at end of file diff --git a/tests/pingpong/index.js b/tests/pingpong/index.js index 0f237c4..3d66312 100644 --- a/tests/pingpong/index.js +++ b/tests/pingpong/index.js @@ -1,5 +1,30 @@ -const = require('.'); -console.log(ref) +const {Collexion, Link, LinkArray} = require('./../../core/Collexion.js'); +const PingPong = require('./pingpong.js') + +;(async () => { + const collexion = new Collexion({}); + collexion.createInstances({ + 'A': { + Code: PingPong, + Data: { + boop: true + }, + Links: { + thing: new Link('B') + } + }, + 'B': { + Code: PingPong, + Data: { + + }, + Links: { + thing: new Link('B') + } + } + }) +})(); + // const ExampleModule = require('./module.js.js'); diff --git a/tests/pingpong/module.js b/tests/pingpong/module.js deleted file mode 100644 index b1dd669..0000000 --- a/tests/pingpong/module.js +++ /dev/null @@ -1,20 +0,0 @@ -const {Signale} = require('signale'); -const log = new Signale({ - scope: 'MODULE' -}); - -module.exports.entity = class module { - async start() { - - console.log(this.data) - if(this.data.boop) - this.boop(); - } - - async boop() { - log.info(`Boop! ${this.data.thing}`) - await new Promise(res => setTimeout(res, 1000)); - this.send('boop', this.data.thing) - } -} - diff --git a/tests/pingpong/pingpong.js b/tests/pingpong/pingpong.js new file mode 100644 index 0000000..2e1c96f --- /dev/null +++ b/tests/pingpong/pingpong.js @@ -0,0 +1,22 @@ +const {Signale} = require('signale'); +const log = new Signale({ + scope: 'PING_PONG' +}); + +module.exports = class module { + async start() { + console.log(this._data) + } + + connected() { + if(this._data.boop) + this.boop(); + } + + async boop() { + log.info(`Boop!`) + await new Promise(res => setTimeout(res, 1000)); + this._links.thing.boop(); + } +} +