2019-04-28 23:45:03 -05:00
|
|
|
import {compose, EventEmitter, TickingPromise} from '@avocado/core';
|
2019-03-17 23:45:48 -05:00
|
|
|
|
|
|
|
import {Traversal} from './traversal';
|
|
|
|
import {Traversals} from './traversals';
|
|
|
|
|
2019-04-08 17:41:20 -05:00
|
|
|
const decorate = compose(
|
|
|
|
EventEmitter,
|
|
|
|
);
|
|
|
|
|
|
|
|
export class Actions extends decorate(Traversals) {
|
2019-03-17 23:45:48 -05:00
|
|
|
|
2019-05-05 04:26:35 -05:00
|
|
|
static type() {
|
|
|
|
return 'actions';
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:45:48 -05:00
|
|
|
constructor() {
|
|
|
|
super();
|
2019-04-07 12:00:11 -05:00
|
|
|
this._index = 0;
|
2019-03-17 23:45:48 -05:00
|
|
|
this.pending = null;
|
|
|
|
}
|
|
|
|
|
2019-04-23 00:30:44 -05:00
|
|
|
emitFinished() {
|
|
|
|
this.emit('actionsFinished');
|
2019-04-20 16:02:52 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 23:45:48 -05:00
|
|
|
get index() {
|
2019-04-07 12:00:11 -05:00
|
|
|
return this._index;
|
2019-03-17 23:45:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
set index(index) {
|
2019-04-07 12:00:11 -05:00
|
|
|
this._index = index;
|
2019-03-17 23:45:48 -05:00
|
|
|
}
|
|
|
|
|
2019-06-06 00:09:51 -05:00
|
|
|
get() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:45:48 -05:00
|
|
|
tick(context, elapsed) {
|
|
|
|
if (this.traversals.length === 0) {
|
2019-04-23 00:30:44 -05:00
|
|
|
this.emitFinished();
|
2019-03-17 23:45:48 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-05-27 21:51:39 -05:00
|
|
|
if (this.pending) {
|
2019-09-08 05:34:17 -05:00
|
|
|
if (this.pending instanceof TickingPromise) {
|
|
|
|
this.pending.tick(elapsed);
|
2019-05-27 21:51:39 -05:00
|
|
|
}
|
2019-03-17 23:45:48 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actions execute immediately until a promise is made, or they're all
|
|
|
|
// executed.
|
|
|
|
while (true) {
|
|
|
|
const result = this.traversals[this.index].traverse(context);
|
|
|
|
if (result instanceof Promise) {
|
2019-05-27 21:51:39 -05:00
|
|
|
result.then(() => {
|
|
|
|
this.prologue();
|
|
|
|
});
|
2019-03-17 23:45:48 -05:00
|
|
|
result.catch((error) => {
|
2019-05-27 21:51:39 -05:00
|
|
|
console.error(error);
|
|
|
|
this.prologue();
|
2019-03-17 23:45:48 -05:00
|
|
|
});
|
|
|
|
this.pending = result;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.prologue();
|
|
|
|
if (0 === this.index) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 00:09:51 -05:00
|
|
|
parallel(context) {
|
2019-09-08 08:29:31 -05:00
|
|
|
const results = this.traversals.map((traversal) => {
|
2019-09-08 19:31:14 -05:00
|
|
|
return traversal.traverse(context);
|
2019-09-08 08:29:31 -05:00
|
|
|
});
|
2019-09-08 19:31:14 -05:00
|
|
|
return TickingPromise.all(results);
|
2019-06-06 00:09:51 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 23:45:48 -05:00
|
|
|
prologue() {
|
|
|
|
this.pending = null;
|
2019-04-09 09:39:00 -05:00
|
|
|
if (0 === (this.index = (this.index + 1) % this.traversals.length)) {
|
2019-04-23 00:30:44 -05:00
|
|
|
this.emitFinished();
|
2019-03-17 23:45:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 00:14:16 -05:00
|
|
|
serial(context) {
|
2019-09-08 19:31:14 -05:00
|
|
|
return this.tickingPromise(context);
|
2019-06-07 00:14:16 -05:00
|
|
|
}
|
|
|
|
|
2019-09-08 07:39:55 -05:00
|
|
|
tickingPromise(context) {
|
|
|
|
return new TickingPromise(
|
|
|
|
(resolve) => {
|
|
|
|
this.once('actionsFinished', () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(elapsed) => {
|
|
|
|
this.tick(context, elapsed);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:45:48 -05:00
|
|
|
}
|