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:

  1. call register() returns observable
  2. merge getchange() , takewhile 1 observable
  3. concatenate merged result of getchange , takewhile interval observable.
    1. 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:

  1. so if want preserve order, should use concatmap? in case, want registerdomain complete before calling getchange?

  2. if don't care order, use mergemap?

  3. in getregisterstatus method, merging here? why not use concatmap?

there 3 operators flattening higher-order observables:

  • mergemap (aka flatmap) - merge streams together, emissions interleave
  • concatmap - after first stream completes concat next etc
  • switchmap - 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') } 
  1. first _adminservice .registerdomain returns observable

  2. after has emitted value concatmap second observable returned getregisterstatus

  3. 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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -