basioc ping pong system. this.data, this.send all working.

master
Marcus 2019-05-27 17:13:46 -04:00
parent 860c550904
commit 4b975d2af5
7 changed files with 91 additions and 36 deletions

View File

@ -4,6 +4,7 @@ const {Signale} = require('signale');
const log = new Signale({ const log = new Signale({
scope: 'CACHE' scope: 'CACHE'
}); });
const rmrf = require('rimraf');
module.exports.Cache = class Cache { module.exports.Cache = class Cache {
constructor (basePath) { constructor (basePath) {
@ -12,14 +13,22 @@ module.exports.Cache = class Cache {
code: path.join(basePath, 'code'), code: path.join(basePath, 'code'),
instances: path.join(basePath, 'instances') instances: path.join(basePath, 'instances')
} }
this.createStructure();
this.loadCache();
}
createStructure() {
try { try {
fs.mkdirSync(this.paths.base); fs.mkdirSync(this.paths.base);
fs.mkdirSync(this.paths.code); fs.mkdirSync(this.paths.code);
fs.mkdirSync(this.paths.instances); fs.mkdirSync(this.paths.instances);
} catch (e) {}; } catch (e) {};
}
this.loadCache(); cleanup() {
rmrf.sync(this.paths.base);
this.createStructure();
} }
loadCache() { loadCache() {
@ -51,6 +60,11 @@ module.exports.Cache = class Cache {
} }
getDataFromUuid(uuid) { getDataFromUuid(uuid) {
let instancePath = path.join(this.paths.instances, `${uuid}.json`); 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);
} }
} }

View File

@ -32,6 +32,7 @@ async function compile({cache: cachePath, index}) {
async function createCache({cache: cachePath, index}) { async function createCache({cache: cachePath, index}) {
const cache = new Cache(cachePath); const cache = new Cache(cachePath);
// const modules = {}; // const modules = {};
cache.cleanup();
for(const symbol in index.Entities) { for(const symbol in index.Entities) {
const module = index.Entities[symbol]; const module = index.Entities[symbol];
@ -70,13 +71,13 @@ function compileLinks (index) {
// loopback and replace all #links in Data with _ids // loopback and replace all #links in Data with _ids
for(const symbol in index.Entities) { for(const symbol in index.Entities) {
let data = index.Entities[symbol].Data; let data = index.Entities[symbol].data;
for(const targetSymbol in index.Entities) { for(const targetSymbol in index.Entities) {
data = recursiveReplace(data, `#${targetSymbol}`, index.Entities[targetSymbol]._id) data = recursiveReplace(data, `#${targetSymbol}`, index.Entities[targetSymbol]._id)
} }
index.Entities[symbol].Data = data; index.Entities[symbol].data = data;
} }
return index; return index;

38
core/entity.js 100644
View File

@ -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}`);
}
}

View File

@ -8,42 +8,40 @@ const uuid = require('uuid');
const timeout = 3000; const timeout = 3000;
const root = (typeof window === 'undefined' ? global : window) const root = (typeof window === 'undefined' ? global : window)
const {Entity} = require('./entity.js')
module.exports.System = class System { module.exports.System = class System {
constructor(cache) { constructor(cache) {
this.cache = cache; this.cache = cache;
this.entities = {}; this.entities = {};
for(const uuid of cache.getInstances()) { for(const uuid of cache.getInstances()) {
this.send('Start', uuid) this.loadEntity(uuid)
} }
} }
async send(name, destination, options) { async send(name, destination, options) {
// log.debug(`sending ${name}`); // log.debug(`sending ${name}`);
if(!(destination in this.entities)) if(!(destination in this.entities)) {
this.loadEntity(destination); this.loadEntity(destination);
log.debug('loading entity...')
}
let a = require('./../tests/modules/module.js').entity this.entities[destination].dispatch(name, options);
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}`);
} }
loadEntity(uuid) { loadEntity(uuid) {
// log.debug(`spinning up ${uuid}`); // log.debug(`spinning up ${uuid}`);
log.debug(uuid)
if(typeof require === 'function') { if(typeof require === 'function') {
let codePath = this.cache.getEntityCodePathFromUuid(uuid); let codePath = this.cache.getEntityCodePathFromUuid(uuid);
// log.debug(codePath) // log.debug(codePath)
let data = this.cache.getDataFromUuid(uuid); let data = this.cache.getDataFromUuid(uuid);
let entityProto = require(codePath).entity; let instanceData = this.cache.getInstanceFromUuid(uuid);
this.entities[uuid] = new entityProto(data); let userCode = require(codePath).entity;
this.entities[uuid] = new Entity(userCode, instanceData, this);
this.entities[uuid].start();
log.debug('starting', uuid)
} else { } else {
//TODO COMPLICATED EVAL SHIT, MIRRORING REQUIRE FUNCTIONALITY //TODO COMPLICATED EVAL SHIT, MIRRORING REQUIRE FUNCTIONALITY
} }

View File

@ -20,6 +20,7 @@
}, },
"homepage": "https://github.com/marcus13345/AquaTin#readme", "homepage": "https://github.com/marcus13345/AquaTin#readme",
"dependencies": { "dependencies": {
"rimraf": "^2.6.3",
"signale": "^1.4.0", "signale": "^1.4.0",
"uuid": "^3.3.2", "uuid": "^3.3.2",
"yargs": "^13.2.4" "yargs": "^13.2.4"

View File

@ -1,14 +1,20 @@
const {Signale} = require('signale');
const log = new Signale({
scope: 'MODULE'
});
module.exports.entity = class module { module.exports.entity = class module {
async Start() { async start() {
console.log('starting...');
await new Promise(res => { console.log(this.data)
setTimeout(_ => { if(this.data.boop)
console.log('done'); this.boop();
res(); }
}, 1000);
}) async boop() {
log.info(`Boop! ${this.data.thing}`)
await new Promise(res => setTimeout(res, 1000));
this.send('boop', this.data.thing)
} }
} }

View File

@ -1,8 +1,4 @@
const path = require('path'); const path = require('path');
const {Signale} = require('signale');
const log = new Signale({
scope: 'CLI'
});
let local = path.join(__dirname, './../modules/') let local = path.join(__dirname, './../modules/')
module.exports = { module.exports = {
@ -10,15 +6,16 @@ module.exports = {
A: { A: {
Name: 'module', Name: 'module',
From: local, From: local,
Data: { data: {
Thing: '#B' thing: '#B',
boop: true
} }
}, },
B: { B: {
Name: 'module', Name: 'module',
From: local, From: local,
Data: { data: {
Thing: '#A' thing: '#A'
} }
} }
} }