chore: tidy

This commit is contained in:
cha0s 2019-09-08 19:50:39 -05:00
parent e2ff23d35c
commit d4c67d298e

View File

@ -16,7 +16,7 @@ export class Actions extends decorate(Traversals) {
constructor() { constructor() {
super(); super();
this._index = 0; this._index = 0;
this._pending = null; this._actionPromise = null;
} }
emitFinished() { emitFinished() {
@ -36,33 +36,34 @@ export class Actions extends decorate(Traversals) {
} }
tick(context, elapsed) { tick(context, elapsed) {
// Empty resolves immediately.
if (this.traversals.length === 0) { if (this.traversals.length === 0) {
this.emitFinished(); this.emitFinished();
return; return;
} }
if (this._pending) { // If the action promise ticks, tick it.
if (this._pending instanceof TickingPromise) { if (this._actionPromise && this._actionPromise instanceof TickingPromise) {
this._pending.tick(elapsed); this._actionPromise.tick(elapsed);
}
return; return;
} }
// Actions execute immediately until a promise is made, or they're all // Actions execute immediately until a promise is made, or they're all
// executed. // executed.
while (true) { while (true) {
// Run the action.
const result = this.traversals[this.index].traverse(context); const result = this.traversals[this.index].traverse(context);
// Deferred result.
if (result instanceof Promise) { if (result instanceof Promise) {
result.then(() => { this._actionPromise = result;
// Handle any errors.
result.catch(console.error).finally(() => {
// Finally, run the prologue.
this.prologue(); this.prologue();
}); });
result.catch((error) => {
console.error(error);
this.prologue();
});
this._pending = result;
break; break;
} }
// Immediate result.
this.prologue(); this.prologue();
// Need to break out immediately if required.
if (0 === this.index) { if (0 === this.index) {
break; break;
} }
@ -70,15 +71,21 @@ export class Actions extends decorate(Traversals) {
} }
parallel(context) { parallel(context) {
// Map all traversals to results.
const results = this.traversals.map((traversal) => { const results = this.traversals.map((traversal) => {
return traversal.traverse(context); return traversal.traverse(context);
}); });
// Wrap all results in a TickingPromise.
return TickingPromise.all(results); return TickingPromise.all(results);
} }
prologue() { prologue() {
this._pending = null; // Clear out the action promise.
if (0 === (this.index = (this.index + 1) % this.traversals.length)) { this._actionPromise = null;
// Increment and wrap the index.
this.index = (this.index + 1) % this.traversals.length;
// If rolled over, the actions are finished.
if (0 === this.index) {
this.emitFinished(); this.emitFinished();
} }
} }
@ -90,9 +97,7 @@ export class Actions extends decorate(Traversals) {
tickingPromise(context) { tickingPromise(context) {
return new TickingPromise( return new TickingPromise(
(resolve) => { (resolve) => {
this.once('actionsFinished', () => { this.once('actionsFinished', resolve);
resolve();
});
}, },
(elapsed) => { (elapsed) => {
this.tick(context, elapsed); this.tick(context, elapsed);