Created
December 19, 2015 06:42
-
-
Save bouzuya/a57b1051ce6c90890b13 to your computer and use it in GitHub Desktop.
RxJS CurrentThreadScheduler の動きに近い何か
This file contains hidden or 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
| // RxJS CurrentThreadScheduler | |
| const queue = []; | |
| const enqueue = (action) => queue.push(action); | |
| const peek = () => queue[0]; | |
| const dequeue = () => queue.shift(); | |
| const run = () => { | |
| while (queue.length > 0) { | |
| const action = peek(); | |
| action(); | |
| dequeue(); | |
| } | |
| }; | |
| const schedule = (action) => { | |
| if (queue.length > 0) { | |
| enqueue(action); | |
| } else { | |
| enqueue(action); | |
| run(); | |
| } | |
| }; | |
| schedule(() => console.log('aiueo')); | |
| schedule(() => { | |
| console.log('1'); | |
| schedule(() => { | |
| console.log('2') | |
| }); | |
| schedule(() => { | |
| console.log('3'); | |
| schedule(() => { | |
| console.log('3-1'); | |
| }); | |
| }); | |
| schedule(() => { | |
| console.log('4'); | |
| }) | |
| }); | |
| // aiueo | |
| // 1 | |
| // 2 | |
| // 3 | |
| // 4 | |
| // 3-1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment