Do you need to unsubscribe from Angular 2 http calls to prevent memory leak? -
do need unsubscribe angular 2 http calls prevent memory leak?
fetchfilm(index) { var sub = this._http.get(`http://example.com`) .map(result => result.json()) .map(json => { dispatch(this.receivefilm(json)); }) .subscribe(e=>sub.unsubscribe()); ...
so answer no, don't ng2 cleanup after
the http service source, angular's http xhr backend source:
notice how runs "complete" after getting result. means unsubscribes on completion. don't need yourself.
here test validate:
fetchfilms() { return (dispatch) => { dispatch(this.requestfilms()); let observer = this._http.get(`${base_url}`) .map(result => result.json()) .map(json => { dispatch(this.receivefilms(json.results)); dispatch(this.receivenumberoffilms(json.count)); console.log("2 isunsubscribed",observer.isunsubscribed); window.settimeout(() => { console.log("3 isunsubscribed",observer.isunsubscribed); },10); }) .subscribe(); console.log("1 isunsubscribed",observer.isunsubscribed); }; }
as expected, can see unsubscribed automatically after getting result , finishing observable operators. happens on timeout (#3) can check status of observable when it's done , completed.
and result
no leak ng2 auto unsubscribes! rubyboy on this..
Comments
Post a Comment