You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
I have a situation where I have multiple directives that subscribe to a stream from a service. If a directive is destroyed do I need to unsubscribe from the stream so I don't have zombie listeners? Or does it get garbage collected. If I need to unsubscribe how do I do it? newZoneStream.dispose(); is undefined.
/*** Directive ***/functionpanelController(rxService){varvm=this;//private variablesvarnewZoneStream=null;//public variablesvm.zones=null;//public functionsvm.init=init;vm.destroy=destroy;functioninit(){newZoneStream=rxService.getNewZoneStream();newZoneStream.subscribe(function(data){vm.zones=data;});}functiondestroy(){// DO I NEED TO DISPOSE HERE?: newZoneStream.dispose(); ?}}functionpanelLink(scope,element,link,ctrl){ctrl.init();scope.$on('$destroy',function(){ctrl.destroy();})}/*** Service ***/functiongetNewZoneStream(){if(!newZonesStream){newZonesStream=rx.Observable.create(function(observable){socket.on("new-zones",function(data){observable.onNext(data);});//clean up functionreturnfunction(){//$sails.off("new-zones");console.log("new zones stream closed");}});}returnnewZonesStream;}
The text was updated successfully, but these errors were encountered:
erceth
changed the title
Do I need to cal dispose on directive $destroy event?
Do I need to call dispose on directive $destroy event?
Feb 5, 2016
User error. rx.Observable() returns a stream not subscription. You can't call .dispose() on a stream. I needed to call subscribe() on the stream to get a subscription object. I call .dispose() on the subscription. Here's what I replaced it with:
/*** Directive ***/
function init() {
var newZoneStream = rxService.getNewZoneStream();
newZoneSubscription = newZoneStream.subscribe(function (data) {
console.log(vm.id);
vm.zones = data;
if (!$scope.$$phase) {
$scope.$apply();
}
});
}
function destroy() {
newZoneSubscription.dispose(); //stops listening to the stream
}
/*** Service ***/
function getNewZoneStream () {
if (!newZonesStream) {
newZonesStream = Rx.Observable.create(function(observable) {
socket.on("new-zones", function(data) {
observable.onNext(data);
});
//clean up function
return function() {
//$sails.off("new-zones");
console.log("unsubscribed from stream");
}
});
}
return newZonesStream;
}
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I have a situation where I have multiple directives that subscribe to a stream from a service. If a directive is destroyed do I need to unsubscribe from the stream so I don't have zombie listeners? Or does it get garbage collected. If I need to unsubscribe how do I do it? newZoneStream.dispose(); is undefined.
The text was updated successfully, but these errors were encountered: