From 6b124ab1831fee7c702953147693bdfc70ff4e88 Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 8 Sep 2019 05:34:17 -0500 Subject: [PATCH] refactor: TickingPromise is more of a joy --- packages/behavior/context/timing.js | 18 ++++----- packages/behavior/item/actions.js | 47 +++++++++++------------- packages/behavior/item/traversals.js | 28 ++++++++------ packages/core/index.js | 22 +++++++++-- packages/entity/traits/existent.trait.js | 15 ++++---- packages/entity/traits/mobile.trait.js | 20 +++++----- 6 files changed, 84 insertions(+), 66 deletions(-) diff --git a/packages/behavior/context/timing.js b/packages/behavior/context/timing.js index 9506cac..9afd0c1 100644 --- a/packages/behavior/context/timing.js +++ b/packages/behavior/context/timing.js @@ -1,13 +1,13 @@ import {TickingPromise} from '@avocado/core'; export const wait = (duration) => { - let resolve; - const promise = new TickingPromise(_resolve => resolve = _resolve); - promise.ticker = (elapsed) => { - duration -= elapsed; - if (duration <= 0) { - resolve(); - } - } - return promise; + return new TickingPromise( + () => {}, + (elapsed, resolve) => { + duration -= elapsed; + if (duration <= 0) { + resolve(); + } + }, + ); }; diff --git a/packages/behavior/item/actions.js b/packages/behavior/item/actions.js index b47931a..98f80a9 100644 --- a/packages/behavior/item/actions.js +++ b/packages/behavior/item/actions.js @@ -41,11 +41,8 @@ export class Actions extends decorate(Traversals) { return; } if (this.pending) { - if ( - this.pending instanceof TickingPromise - && 'function' === typeof this.pending.ticker - ) { - this.pending.ticker(elapsed); + if (this.pending instanceof TickingPromise) { + this.pending.tick(elapsed); } return; } @@ -91,15 +88,16 @@ export class Actions extends decorate(Traversals) { } } if (tickingPromises.length > 0) { - const tickingPromise = new TickingPromise((resolve) => { - resolve(Promise.all(results)); - }); - tickingPromise.ticker = (elapsed) => { - for (let i = 0; i < tickingPromises.length; i++) { - tickingPromises[i].ticker(elapsed); - } - }; - return tickingPromise; + return new TickingPromise( + (resolve) => { + resolve(Promise.all(results)); + }, + (elapsed) => { + for (let i = 0; i < tickingPromises.length; i++) { + tickingPromises[i].tick(elapsed); + } + }, + ); } if (promises.length > 0) { return Promise.all(results); @@ -119,17 +117,16 @@ export class Actions extends decorate(Traversals) { this.tick(context, 0); // If it's pending, we have to return a ticking promise. if (this.pending) { - let resolve_; - const tickingPromise = new TickingPromise((resolve) => { - resolve_ = resolve; - }); - tickingPromise.ticker = (elapsed) => { - this.tick(context, elapsed); - } - this.once('actionsFinished', () => { - resolve_(); - }); - return tickingPromise; + return new TickingPromise( + (resolve) => { + this.once('actionsFinished', () => { + resolve(); + }); + }, + (elapsed) => { + this.tick(context, elapsed); + }, + ); } } diff --git a/packages/behavior/item/traversals.js b/packages/behavior/item/traversals.js index 1699eba..e32c026 100644 --- a/packages/behavior/item/traversals.js +++ b/packages/behavior/item/traversals.js @@ -15,22 +15,26 @@ export class Traversals extends Collection('traversal') { }, false)) { return results; } - // Otherwise, wrap in a promise. - const tpromise = new TickingPromise((resolve, reject) => { - return Promise.all(results); - }); - // Proxy all ticks. + // Proxy any tickers. const tickableResults = results.filter((result) => { - if (!(result instanceof TickingPromise)) return false; - if ('function' !== typeof result.ticker) return false; + if (!(result instanceof TickingPromise)) { + return false; + } return true; }); - if (tickableResults.length > 0) { - tpromise.ticker = (elapsed) => { - tickableResults.forEach((result) => result.ticker(elapsed)); - }; + // If none, just promise. + if (0 === tickableResults.length) { + return Promise.all(results); } - return tpromise; + // Otherwise, tick all tickers and resolve. + return new TickingPromise( + (resolve) => { + resolve(Promise.all(results)); + }, + (elapsed) => { + tickableResults.forEach((result) => result.tick(elapsed)); + }, + ); } } diff --git a/packages/core/index.js b/packages/core/index.js index 924ee4e..c8906ec 100644 --- a/packages/core/index.js +++ b/packages/core/index.js @@ -62,10 +62,26 @@ export function virtualizeStatic(fields) { } export class TickingPromise extends Promise { - constructor(resolve, reject) { - super(resolve, reject); - this.ticker = null; + + constructor(executor, ticker) { + let _reject, _resolve; + super((resolve, reject) => { + _reject = reject; + _resolve = resolve; + if (executor) { + executor(resolve, reject); + } + }); + this.executor = executor; + this.reject = _reject; + this.resolve = _resolve; + this.ticker = ticker; } + + tick(elapsed) { + this.ticker(elapsed, this.resolve, this.reject); + } + } export {EventEmitterMixin as EventEmitter} from './event-emitter'; diff --git a/packages/entity/traits/existent.trait.js b/packages/entity/traits/existent.trait.js index 13b10e8..94c792f 100644 --- a/packages/entity/traits/existent.trait.js +++ b/packages/entity/traits/existent.trait.js @@ -56,13 +56,14 @@ export class Existent extends decorate(Trait) { duration, easing ); - const promise = new TickingPromise((resolve) => { - resolve(result.promise); - }); - promise.ticker = (elapsed) => { - result.tick(elapsed); - } - return promise; + return new TickingPromise( + (resolve) => { + resolve(result.promise); + }, + (elapsed) => { + result.tick(elapsed); + }, + ); }, }; diff --git a/packages/entity/traits/mobile.trait.js b/packages/entity/traits/mobile.trait.js index 1621997..8f80160 100644 --- a/packages/entity/traits/mobile.trait.js +++ b/packages/entity/traits/mobile.trait.js @@ -30,16 +30,16 @@ export class Mobile extends decorate(Trait) { return { moveFor: (vector, duration) => { - let resolve; - const promise = new TickingPromise(_resolve => resolve = _resolve); - promise.ticker = (elapsed) => { - duration -= elapsed; - if (duration <= 0) { - return resolve(); - } - this.entity.requestMovement(Vector.normalize(vector)); - } - return promise; + return new TickingPromise( + () => {}, + (elapsed, resolve) => { + duration -= elapsed; + if (duration <= 0) { + return resolve(); + } + this.entity.requestMovement(Vector.normalize(vector)); + }, + ); }, applyMovement: (vector) => {