From 4b975d2af5ad9843510e8517cdd791ad25ea2ca3 Mon Sep 17 00:00:00 2001 From: Marcus Date: Mon, 27 May 2019 17:13:46 -0400 Subject: [PATCH] basioc ping pong system. this.data, this.send all working. --- core/cache.js | 20 +++++++++++++++++--- core/compiler.js | 5 +++-- core/entity.js | 38 ++++++++++++++++++++++++++++++++++++++ core/system.js | 26 ++++++++++++-------------- package.json | 1 + tests/modules/module.js | 24 +++++++++++++++--------- tests/system/index.js | 13 +++++-------- 7 files changed, 91 insertions(+), 36 deletions(-) create mode 100644 core/entity.js diff --git a/core/cache.js b/core/cache.js index bb4758e..808e199 100644 --- a/core/cache.js +++ b/core/cache.js @@ -4,6 +4,7 @@ const {Signale} = require('signale'); const log = new Signale({ scope: 'CACHE' }); +const rmrf = require('rimraf'); module.exports.Cache = class Cache { constructor (basePath) { @@ -12,14 +13,22 @@ module.exports.Cache = class Cache { code: path.join(basePath, 'code'), instances: path.join(basePath, 'instances') } - + this.createStructure(); + + this.loadCache(); + } + + createStructure() { try { fs.mkdirSync(this.paths.base); fs.mkdirSync(this.paths.code); fs.mkdirSync(this.paths.instances); } catch (e) {}; + } - this.loadCache(); + cleanup() { + rmrf.sync(this.paths.base); + this.createStructure(); } loadCache() { @@ -51,6 +60,11 @@ module.exports.Cache = class Cache { } getDataFromUuid(uuid) { let instancePath = path.join(this.paths.instances, `${uuid}.json`); - return require(instancePath).Data; + return require(instancePath).data; + } + + getInstanceFromUuid(uuid) { + let instancePath = path.join(this.paths.instances, `${uuid}.json`); + return require(instancePath); } } \ No newline at end of file diff --git a/core/compiler.js b/core/compiler.js index 7254393..3145f38 100644 --- a/core/compiler.js +++ b/core/compiler.js @@ -32,6 +32,7 @@ async function compile({cache: cachePath, index}) { async function createCache({cache: cachePath, index}) { const cache = new Cache(cachePath); // const modules = {}; + cache.cleanup(); for(const symbol in index.Entities) { const module = index.Entities[symbol]; @@ -70,13 +71,13 @@ function compileLinks (index) { // loopback and replace all #links in Data with _ids for(const symbol in index.Entities) { - let data = index.Entities[symbol].Data; + let data = index.Entities[symbol].data; for(const targetSymbol in index.Entities) { data = recursiveReplace(data, `#${targetSymbol}`, index.Entities[targetSymbol]._id) } - index.Entities[symbol].Data = data; + index.Entities[symbol].data = data; } return index; diff --git a/core/entity.js b/core/entity.js new file mode 100644 index 0000000..6da7c5b --- /dev/null +++ b/core/entity.js @@ -0,0 +1,38 @@ +const {Signale} = require('signale'); +const log = new Signale({ + scope: 'ENTITY' +}); + +module.exports.Entity = class Entity { + constructor(userCode, instanceData = {}, systemPtr) { + this.systemPtr = systemPtr; + this.instanceData = instanceData; + this.data = this.instanceData.data || {}; + this.userCode = userCode; + this.instance = new this.userCode(); + const that = this; + Object.defineProperty(this.instance, 'data', { + get() { + return that.data; + } + }); + Object.defineProperty(this.instance, 'send', { + value: systemPtr.send.bind(systemPtr) + }); + } + + start() { + this.systemPtr.send('start', this.instanceData._id); + } + + dispatch(name, options) { + + if(name in this.instance) + this.instance[name](options); + + else if('*' in this.instance) + this.instance['*'](options); + + else log.warn(`method ${name} does not exist for ${this.instanceData._id}`); + } +} \ No newline at end of file diff --git a/core/system.js b/core/system.js index a3a49f6..cd4e8ef 100644 --- a/core/system.js +++ b/core/system.js @@ -8,42 +8,40 @@ const uuid = require('uuid'); const timeout = 3000; const root = (typeof window === 'undefined' ? global : window) +const {Entity} = require('./entity.js') module.exports.System = class System { constructor(cache) { this.cache = cache; this.entities = {}; for(const uuid of cache.getInstances()) { - this.send('Start', uuid) + this.loadEntity(uuid) } } async send(name, destination, options) { // log.debug(`sending ${name}`); - if(!(destination in this.entities)) + if(!(destination in this.entities)) { this.loadEntity(destination); + log.debug('loading entity...') + } - let a = require('./../tests/modules/module.js').entity - let b = new a({}); - - if(name in this.entities[destination]) - this.entities[destination][name](options); - - else if('*' in this.entities[destination]) - this.entities[destination]['*'](options); - - else log.warn(`method ${name} does not exist for ${destination}`); + this.entities[destination].dispatch(name, options); } loadEntity(uuid) { // log.debug(`spinning up ${uuid}`); + log.debug(uuid) if(typeof require === 'function') { let codePath = this.cache.getEntityCodePathFromUuid(uuid); // log.debug(codePath) let data = this.cache.getDataFromUuid(uuid); - let entityProto = require(codePath).entity; - this.entities[uuid] = new entityProto(data); + let instanceData = this.cache.getInstanceFromUuid(uuid); + let userCode = require(codePath).entity; + this.entities[uuid] = new Entity(userCode, instanceData, this); + this.entities[uuid].start(); + log.debug('starting', uuid) } else { //TODO COMPLICATED EVAL SHIT, MIRRORING REQUIRE FUNCTIONALITY } diff --git a/package.json b/package.json index 73e9667..458761d 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ }, "homepage": "https://github.com/marcus13345/AquaTin#readme", "dependencies": { + "rimraf": "^2.6.3", "signale": "^1.4.0", "uuid": "^3.3.2", "yargs": "^13.2.4" diff --git a/tests/modules/module.js b/tests/modules/module.js index afd8424..b1dd669 100644 --- a/tests/modules/module.js +++ b/tests/modules/module.js @@ -1,14 +1,20 @@ - +const {Signale} = require('signale'); +const log = new Signale({ + scope: 'MODULE' +}); module.exports.entity = class module { - async Start() { - console.log('starting...'); - await new Promise(res => { - setTimeout(_ => { - console.log('done'); - res(); - }, 1000); - }) + 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/system/index.js b/tests/system/index.js index 6ca0548..58e2a93 100644 --- a/tests/system/index.js +++ b/tests/system/index.js @@ -1,8 +1,4 @@ const path = require('path'); -const {Signale} = require('signale'); -const log = new Signale({ - scope: 'CLI' -}); let local = path.join(__dirname, './../modules/') module.exports = { @@ -10,15 +6,16 @@ module.exports = { A: { Name: 'module', From: local, - Data: { - Thing: '#B' + data: { + thing: '#B', + boop: true } }, B: { Name: 'module', From: local, - Data: { - Thing: '#A' + data: { + thing: '#A' } } }