From 320a20e69545012e29ef5aa81bb84e30de33d41f Mon Sep 17 00:00:00 2001 From: cha0s Date: Mon, 6 Jan 2020 00:29:18 -0600 Subject: [PATCH] feat: ticking promises in core entity --- packages/entity/entity.synchronized.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/entity/entity.synchronized.js b/packages/entity/entity.synchronized.js index 3d1b2cc..9da79b1 100644 --- a/packages/entity/entity.synchronized.js +++ b/packages/entity/entity.synchronized.js @@ -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) {