Reputation: 723
I've recently ran into a factory that vaguely resembles the one shown below:
.factory("MyFactory", ['$interval', function($interval) {
var factoryObj;
factoryObj.setCheckupTime = function(intervalMs) {
var checkupInterval = $interval(function() {
// do checkup logic...
}, intervalMs);
};
factoryObj.setCheckupTime(60000);
return factoryObj;
}]);
The problem is the line right before the return:
factoryObj.setCheckupTime(60000);
This line feels wrong because we're making a singleton perform an action on itself automatically instead of by some dependent unit of code.
I wanted to get others' opinions on the matter; does this go formally go against any of Angular's best practices, or maybe any rules on how Singletons are supposed to be used?
I was also unsure how to describe this problem as I've never ran into it; is there another term/terminology for what's going on here?
Upvotes: 0
Views: 112
Reputation: 9800
So, is it a bad idea declaring a factory that self starts it's own thing when it's built by angular's core not telling anybody that something has started in the process?
Yes, that's really bad! Is really difficult to maintain and track problems when things are done automatically especially on angular factory that is an internal behavior.
I recommend starting it manually from somewhere that you can track and understand it in the future.
Upvotes: 1