2019-09-18 00:03:29 -04:00
|
|
|
const {Signale} = require('signale');
|
|
|
|
|
const log = new Signale({
|
2019-09-18 01:38:13 -04:00
|
|
|
scope: 'COLLEXION'
|
2019-09-18 00:03:29 -04:00
|
|
|
});
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const uuid = require('uuid');
|
|
|
|
|
const root = (typeof window === 'undefined' ? global : window)
|
|
|
|
|
const {Entity} = require('./entity.js')
|
|
|
|
|
|
2019-09-18 01:38:13 -04:00
|
|
|
class Link {
|
|
|
|
|
constructor(symbol) {
|
|
|
|
|
this.symbol = symbol;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
|
return "" + this.symbol;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-18 00:03:29 -04:00
|
|
|
|
2019-09-18 01:38:13 -04:00
|
|
|
class Collexion {
|
|
|
|
|
constructor() {
|
|
|
|
|
// this.cache = cache;
|
2019-09-18 00:03:29 -04:00
|
|
|
this.entities = {};
|
2019-09-18 01:38:13 -04:00
|
|
|
// for(const uuid of cache.getInstances()) {
|
|
|
|
|
// this.loadEntity(uuid)
|
|
|
|
|
// }
|
2019-09-18 00:03:29 -04:00
|
|
|
}
|
|
|
|
|
|
2019-09-18 01:38:13 -04:00
|
|
|
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;
|
|
|
|
|
}
|
2019-09-18 00:03:29 -04:00
|
|
|
|
2019-09-18 01:38:13 -04:00
|
|
|
for(const symbol in instances) {
|
|
|
|
|
const inst = instances[symbol];
|
|
|
|
|
inst.start()
|
2019-09-18 00:03:29 -04:00
|
|
|
}
|
|
|
|
|
|
2019-09-18 01:38:13 -04:00
|
|
|
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()
|
|
|
|
|
}
|
2019-09-18 00:03:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-18 01:38:13 -04:00
|
|
|
module.exports = {Collexion, Link};
|