Prevent surveillance from restarting to complete completion
In Angular4 / Ionic3 environment using observables. Slider with slides on the page. When the person scrambles to move to the next slide, a multi-stage, long-term observable is triggered. Currently, when the person searches for the second observable instance again (before the first instance completes).
How do I make the second observable wait for the first observable to finish? I do not want to block a person from being able to carry. The swipe event should just enqueue the next observable.
Current timeline
- Swipe1
- Observable instance1 = starting
- Swipe2
- Observable instance2 = startup
- Observed instance1 = end
- Observed instance2 = end
Desired timeline
- Swipe1
- Observable instance1 = starting
- Observed instance1 = end
- Swipe2
- Observable instance2 = startup
- Observed instance2 = end
Current observable pseudocode
private slideNextStart(event) {
// Don't let this Observable be called again until it finishes.
Observable.of(null)
.flatMap(() => {
console.log('start');
// Do something.
})
.flatMap(() => {
// Do something else.
})
.flatMap(() => {
// Do another something else.
})
.subscribe(() => { console.log('end'); });
Angular2 Translated Code from Julia Answer
import { Observable } from 'rxjs/Rx';
import { Subject } from 'rxjs/Subject';
let subject = new Subject();
subject.asObservable()
.concatMap((x: Observable<any>) => { return x })
.subscribe(x => console.log(x));
// request 1
subject.next(Observable
.timer(4000)
.map(() => { console.log('req1') })
.mapTo('req1'));
// request 2
subject.next(Observable.of('req2'));
source to share
Observable.concatMap is for this. "Projects each original value onto the Observable that is concatenated into the output of the Observable, in a serialized manner, waiting for each to complete before merging the next." concatMap
// subject to emit observables
let subject = new Rx.Subject();
// do concatMap to sequence the requests
subject.asObservable()
.concatMap(x=>x)
.subscribe(x=>console.log(x))
// request 1
subject.next(Rx.Observable
.timer(4000)
.mapTo('req1'));
// request 2
subject.next(Rx.Observable.of('req2'));
source to share