Last active
January 10, 2019 09:02
-
-
Save kievsash/f3cd56a1f5f1b7ca096dd278511341ca to your computer and use it in GitHub Desktop.
So how does Rx.js QueueScheduler actually work?
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
//value will be emitted at once — synchronously | |
of(1).subscribe(console.log) | |
//value will be emitted at once — synchronously | |
of(1, queueScheduler).subscribe(console.log) | |
// value will be emitted in mIcrotask just after current mAcrotask | |
of(1, asapScheduler).subscribe(console.log) | |
// value will be emitted in another mAcrotask | |
of(1, asyncScheduler).subscribe(console.log) | |
// value will be emitted in another mAcrotask just before browser repaint | |
of(1, animationFrameScheduler).subscribe(console.log) | |
// combineLatest with default scheduler | |
const a$ = of(1, 2); | |
const b$ = of(10); | |
const c$ = combineLatest(a$, b$, (a,b)=>a+b); | |
c$.subscribe(console.log); | |
// combineLatest with queueScheduler | |
const a$ = of(1, 2, queueScheduler); | |
const b$ = of(10, queueScheduler); | |
const c$ = combineLatest(a$, b$, (a,b)=>a+b, queueScheduler); | |
c$.subscribe(console.log); | |
let Rx = window[‘rxjs’]; | |
const {Subject, queueScheduler} = Rx; | |
const {take, observeOn} = Rx.operators; | |
console.clear(); | |
const signal = new Subject(); | |
let count = 0; | |
const somecalculations = (count) => console.log(‘do some calculations with ‘, count); | |
console.log(‘Start’); | |
signal.pipe(take(1600)) | |
.subscribe(() => { | |
somecalculations(count); | |
signal.next(count++); //recursive call | |
}); | |
signal.next(count++); // initial data emission, that will start recursive calls | |
console.log(‘Stop’); | |
let Rx = window[‘rxjs’]; | |
const {Subject, queueScheduler} = Rx; | |
const {take, observeOn} = Rx.operators; | |
console.clear(); | |
const signal = new Subject(); | |
let count = 0; | |
const somecalculations = (count) => console.log(‘do some calculations with ‘, count); | |
console.log(‘Start’); | |
signal.pipe(take(1600), observeOn(queueScheduler)) | |
.subscribe(() => { | |
somecalculations(count); | |
signal.next(count++); //recursive call | |
}); | |
signal.next(count++); // initial data emission, that will start recursive calls | |
console.log(‘Stop’); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment