So say for example I have a factory:
app.factory('exposeService', function() {
var service = {};
service.expose = function(name, callback) {
service[name] = callback;
};
service.destroyExposed = function(name) {
if (service[name]) {
service[name] = null;
}
};
return service;
})
This factory is pretty simple it has two methods one to set a callback and the other to destroy said callback. A controller would consume this service and 'expose' its methods which other services can now call directly. Here's an example:
app.controller("simpleController", ["exposeService", "$scope", function(exposeService, $scope) {
var vm = this;
vm.counter = 0;
var exposedCallback = function() {
console.log('This is what I need to access: ', vm);
vm.counter++;
};
playerOverlayService.expose('exposedCallback', exposedCallback);
$scope.$on('$destroy', function() {
playerOverlayService.destroyExposed('onSystemMessage');
});
}]);
This controller needs to increment the counter whenever the service decides it's appropriate to call the callback. Now my question is, if I destroy the reference to the callback when the controller is destroyed, will the scope inside the callback leak memory because it is no longer being used or referenced? Or is the destroyExposed method good enough at telling the GC that it can clean up?