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({
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);
}
}

View File

@ -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;

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 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
}

View File

@ -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"

View File

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

View File

@ -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'
}
}
}