feat: ticking promises in core entity

This commit is contained in:
cha0s 2020-01-06 00:29:18 -06:00
parent 1c8e8d025a
commit 320a20e695

View File

@ -96,6 +96,7 @@ export class Entity extends decorate(Resource) {
this._hooks = {};
this._hydrationPromise = undefined;
this._json = Entity.jsonWithDefaults(json);
this._tickingPromisesTickers = [];
this._traits = {};
this._traitsFlat = [];
this._traitTickers = [];
@ -130,6 +131,17 @@ export class Entity extends decorate(Resource) {
}
}
addTickingPromise(tickingPromise) {
const ticker = tickingPromise.tick.bind(tickingPromise);
this._tickingPromisesTickers.push(ticker);
return tickingPromise.then(() => {
const index = this._tickingPromisesTickers.indexOf(ticker);
if (-1 !== index) {
this._tickingPromisesTickers.splice(index, 1);
}
})
}
addTrait(type, json = {}) {
if (this.is(type)) {
debug(`Tried to add trait "${type}" when it already exists!`);
@ -389,6 +401,9 @@ export class Entity extends decorate(Resource) {
for (let i = 0; i < this._traitTickers.length; i++) {
this._traitTickers[i](elapsed);
}
for (let i = 0; i < this._tickingPromisesTickers.length; i++) {
this._tickingPromisesTickers[i](elapsed);
}
}
toNetwork(informed) {