Skip to content

Instantly share code, notes, and snippets.

@bouzuya
Created December 19, 2015 06:42
Show Gist options
  • Select an option

  • Save bouzuya/a57b1051ce6c90890b13 to your computer and use it in GitHub Desktop.

Select an option

Save bouzuya/a57b1051ce6c90890b13 to your computer and use it in GitHub Desktop.
RxJS CurrentThreadScheduler の動きに近い何か
// 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