rxjs5 - Is it possible to add Teardown logic to an already existing Observable? -


for instance, i'm calling unsubscribe observable returned angular 2's http.

but have custom logic surrounding it.

is possible add custom teardown logic existing observable, 1 returned angular 2's http?

something along lines of observable.prototype.whenunsubscribed(customteardownlogic) maybe?

this might not want may help:

suppose have (taken the hero guide @ angular 2 website):

(typescript)

@injectable() export class heroservice {   private heroesurl = 'api/heroes';  // url web api   constructor (private http: http) {}   getheroes(): observable<hero[]> {     return this.http.get(this.heroesurl)                     .map(this.extractdata);   }   private extractdata(res: response) {     let body = res.json();     return body.data || { };   } }  // , somewhere else in code do:  let subscription = service.getheroes().subscribe( ... stuff here );  // , later on do:  subscription.unsubscribe(); 

if want add custom tear-down logic, wrap observable returned angular 2 own:

getheroes(): observable<hero[]> {     return observable.create(         //let's provide subscribe function         (observer: any) => {              const subscription = this.http.get(this.heroesurl)                         .map(this.extractdata).subscribe(observer);              // let's return tear-down/unsubscribe function             return () => {                 subscription.unsubscribe();                 this.myteardownlogic();             }         }      ); 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -