rxjs - angular 2, return Observables according to if statement -


hi want create this.

somefunction():observable<response>{   let response$ = new observerable();    if (...){     response$ = this.http.get(url1)   }else{     response$ = this.http.get(url2)   }   return response$ } 

if subscribe it, i'm getting error

cannot read property 'subscribe' of undefined

thanks in advance!

edit--------- actual function using ionic 2 , cordova. checks whether app on mobile device or not , opens popup url (intend used social logins)

once window closed need perfom http.get request fetch token backend.

public social(providername:string):observable<response>{  let response$ = new observable<response>(); let url =providername+'/someurl'  let authwindow;  if(window.cordova){     let authwindow= inappbrowser.open(url, '_blank', 'location=no');      authwindow.addeventlistener('loadstop', event => {         if(event.url.indexof(`${providername}/callback`) > -1){             authwindow.close()         }     });      authwindow.addeventlistener('loaderror', event =>{         if(event.url.indexof(`${providername}/callback`) > -1){             authwindow.close()         }     });      authwindow.addeventlistener('exit', () => {         settimeout(()=>{              response$ = this.http.get(...)         },500)     })  }else{     authwindow = createwindow(popupurl);     let window = observable.interval(1000);      let  pingwindow = window.subscribe(         ()=>{             if(authwindow.closed){                 pingwindow.unsubscribe();                  response$ = this.http.get(...)              }         }) }  return response$ 

}

the update reveals problem.
not assigning response$ directly in async callbacks. when return response$ executed, none of assignments response$

    settimeout(()=>{          response$ = this.http.get(...)     },500) 

nor

let  pingwindow = window.subscribe(     ()=>{         if(authwindow.closed){             pingwindow.unsubscribe();              response$ = this.http.get(...)          }     }) 

were executed yet.

the 1st executed 0.5s later, 2nd when function passed window.subscribe(...) executed because of emitted value @ some point later.

public social(providername:string):observable<response>{      let response$ = new subject<response>();     let url =providername+'/someurl'      let authwindow;      if(window.cordova){         let authwindow= inappbrowser.open(url, '_blank', 'location=no');          authwindow.addeventlistener('loadstop', event => {             if(event.url.indexof(`${providername}/callback`) > -1){                 authwindow.close()             }         });          authwindow.addeventlistener('loaderror', event =>{             if(event.url.indexof(`${providername}/callback`) > -1){                 authwindow.close()             }         });          authwindow.addeventlistener('exit', () => {             settimeout(()=>{                  this.http.get(...)                  .map(val => val.json())                  .subscribe(val => response$.next(val);             },500)         })      }else{         authwindow = createwindow(popupurl);         let window = observable.interval(1000);          let  pingwindow = window.subscribe(             ()=>{                 if(authwindow.closed){                     pingwindow.unsubscribe();                      this.http.get(...)                     .map(val => val.json())                     .subscribe(val => response$.next(val);                 }             })     }      return response$.asobservable() } 

this should fix @ least immediate problem.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -