AquaTin/core/Collexion.js

48 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2019-09-18 00:03:29 -04:00
const {Signale} = require('signale');
const log = new Signale({
2019-10-17 14:50:16 -04:00
scope: 'CORE'
2019-09-18 00:03:29 -04:00
});
2019-09-18 01:38:13 -04:00
class Collexion {
2019-10-17 14:50:16 -04:00
constructor(template) {
2019-09-18 00:03:29 -04:00
this.entities = {};
2019-10-17 14:50:16 -04:00
(async () => {
const instances = {};
log.info('Starting Collexion with ' + Object.keys(template) + ' Objects')
// call constructors on objects, and give them their data
for(const symbol in template) {
const instTemplate = template[symbol];
const _class = instTemplate.Code;
const inst = new _class(this);
// guarantee at least an object in _data
inst._data = {...instTemplate.Data};
instances[symbol] = inst;
}
2019-09-18 00:03:29 -04:00
2019-10-17 14:50:16 -04:00
//call start in each instance.
for(const symbol in instances) {
const inst = instances[symbol];
if('start' in inst)
await inst.start()
}
2019-09-18 01:38:13 -04:00
2019-10-17 14:50:16 -04:00
for(const symbol in instances) {
const inst = instances[symbol];
for(const symbol in instances) {
inst[symbol] = instances[symbol];
}
2019-09-18 01:38:13 -04:00
}
2019-10-17 14:50:16 -04:00
for(const symbol in instances) {
const inst = instances[symbol];
if('connected' in inst)
await inst.connected()
}
2019-09-18 00:03:29 -04:00
2019-10-17 14:50:16 -04:00
})();
2019-09-18 00:03:29 -04:00
}
}
2019-10-17 14:50:16 -04:00
module.exports = {Collexion, Link, Component};