Last active
April 26, 2019 09:42
-
-
Save kosich/4d14270613c5bdb8a60ac414945b29f2 to your computer and use it in GitHub Desktop.
Cancel a delay if same observable emits
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
const { rxObserver } = require('api/v0.3'); | |
const { Subject, of } = require('rxjs'); | |
const { switchMap, delay } = require('rxjs/operators'); | |
const subject = new Subject(0); | |
const result$ = subject | |
.pipe( | |
switchMap(value => | |
// switchMap to a delayed value | |
of(value).pipe(delay(50)) | |
) | |
); | |
subject.subscribe(rxObserver('source')); | |
result$.subscribe(rxObserver('result$')); | |
subject.next(0); // immediately emit 0 | |
setTimeout(()=>{ | |
subject.next(1); // emit 1 in 100 ms | |
}, 100); | |
setTimeout(()=>{ | |
subject.next(2); // emit 2 in 120 ms | |
}, 120); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment