38 lines
883 B
JavaScript
38 lines
883 B
JavaScript
|
|
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}`);
|
||
|
|
}
|
||
|
|
}
|