Skip to content

Instantly share code, notes, and snippets.

@lifeart
Last active March 19, 2021 07:19
Show Gist options
  • Save lifeart/1817d1a9af19275a41690033be5ee431 to your computer and use it in GitHub Desktop.
Save lifeart/1817d1a9af19275a41690033be5ee431 to your computer and use it in GitHub Desktop.
state manager, based on ember-concurrency
import {
timeout,
task
} from 'ember-concurrency';
export class StateManager {
constructor() {
this.startExecution();
}
// will add one more task into
sheduleTask(name, args) {
this.stack.push([name, args]);
}
// stepOne task
@task
(function*(msg /* hello */ ) {
alert(msg);
this.sheduleTask('stepTwo', 42);
}) stepOne;
// stepTwo task
@task
(function*(msg /* 42 */ ) {
alert(msg);
}) stepTwo;
// tasks stack
stack = [
["stepOne", "hello"]
];
async startExecution() {
while (true) {
// wait 64 ms after each task
await timeout(64);
let [taskName, args] = this.stack.shift();
yield this[taskName].perform(args);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment