rxjs - In what order is the observable code called? -
in following code, having hard time understanding order of chained calls are:
function registerdomain(casewebsiteurl) { return this._adminservice.registerdomain(casewebsiteurl.url) .concatmap(registerid => observable.interval(5000) .mergemap(() => this._adminservice.getchange(registerid)) .takewhile((info) => info.changeinfo.status.value !== 'insync') ) } registerdomain.subscribe(res => console.log('res:'+res));
i trying understand above code, having hard time understanding order. easier me understand when see more simple example this:
function registerdomain() { return register() .concatmap(registerid => rx.observable.interval(5000) .mergemap(() => getchange(registerid)) .takewhile((info) => info === 'pending') ); } registerdomain().subscribe(x=>console.log(x)); function register() { return rx.observable.of("registerid"); } function getchange(registerid) { //at point return insync return rx.observable.of("pending"); }
when @ code, way read is:
- call register() returns observable
- merge getchange() , takewhile 1 observable
- concatenate merged result of getchange , takewhile interval observable.
- call getchange every 5 seconds until status !== 'pending'
is above assessment correct? how know when use merge or concat?
if wanted catch errors, example, register() throws error, should fail.
based on @mark van straten's answer, additional questions:
so if want preserve order, should use concatmap? in case, want registerdomain complete before calling getchange?
if don't care order, use mergemap?
in getregisterstatus method, merging here? why not use concatmap?
there 3 operators flattening higher-order observables:
mergemap
(akaflatmap
) - merge streams together, emissions interleaveconcatmap
- after first stream completes concat next etcswitchmap
- abandon first stream, switch next stream when arrives given code:
function registerdomain(casewebsiteurl) { return this._adminservice.registerdomain(casewebsiteurl.url) .concatmap(registerid => observable.interval(5000) .mergemap(() => this._adminservice.getchange(registerid)) .takewhile((info) => info.changeinfo.status.value !== 'insync') ) } registerdomain.subscribe(res => console.log('res:'+res));
we can rewrite follows:
function registerdomain(casewebsiteurl) { return this._adminservice.registerdomain(casewebsiteurl.url) .concatmap(registerid => getregisterstatus(registerid)) } function getregisterstatus(registerid) { return observable.interval(5000) .mergemap(() => this._adminservice.getchange(registerid)) .takewhile((info) => info.changeinfo.status.value !== 'insync') }
first
_adminservice .registerdomain
returns observableafter has emitted value
concatmap
second observable returnedgetregisterstatus
getregisterstatus
completes when retrieved info becomes 'insync', every non-insync emission emitted.
i don't think concatmap needed , can replaced mergemap since registerdomain emits 1 value
Comments
Post a Comment