Last active
March 19, 2021 07:19
-
-
Save lifeart/1817d1a9af19275a41690033be5ee431 to your computer and use it in GitHub Desktop.
state manager, based on ember-concurrency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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