2015-02-19 21:21:54 +00:00
|
|
|
/**
|
|
|
|
* Specialized class for conducting an HTTP poll, while properly preventing multiple calls.
|
|
|
|
*/
|
2015-05-21 21:16:38 +00:00
|
|
|
angular.module('quay').factory('AngularPollChannel', ['ApiService', '$timeout', 'DocumentVisibilityService',
|
|
|
|
function(ApiService, $timeout, DocumentVisibilityService) {
|
2015-02-19 21:21:54 +00:00
|
|
|
var _PollChannel = function(scope, requester, opt_sleeptime) {
|
|
|
|
this.scope_ = scope;
|
|
|
|
this.requester_ = requester;
|
|
|
|
this.sleeptime_ = opt_sleeptime || (60 * 1000 /* 60s */);
|
|
|
|
this.timer_ = null;
|
|
|
|
|
|
|
|
this.working = false;
|
|
|
|
this.polling = false;
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
scope.$on('$destroy', function() {
|
|
|
|
that.stop();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-05-06 22:16:03 +00:00
|
|
|
_PollChannel.prototype.setSleepTime = function(sleepTime) {
|
|
|
|
this.sleeptime_ = sleepTime;
|
|
|
|
this.stop();
|
|
|
|
this.start(true);
|
|
|
|
};
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
_PollChannel.prototype.stop = function() {
|
|
|
|
if (this.timer_) {
|
|
|
|
$timeout.cancel(this.timer_);
|
|
|
|
this.timer_ = null;
|
|
|
|
this.polling_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.working = false;
|
|
|
|
};
|
|
|
|
|
2015-05-06 22:16:03 +00:00
|
|
|
_PollChannel.prototype.start = function(opt_skipFirstCall) {
|
2015-02-19 21:21:54 +00:00
|
|
|
// Make sure we invoke call outside the normal digest cycle, since
|
|
|
|
// we'll call $scope.$apply ourselves.
|
|
|
|
var that = this;
|
2015-05-06 22:16:03 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
if (opt_skipFirstCall) {
|
|
|
|
that.setupTimer_();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
that.call_();
|
|
|
|
}, 0);
|
2015-02-19 21:21:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
_PollChannel.prototype.call_ = function() {
|
|
|
|
if (this.working) { return; }
|
|
|
|
|
2015-05-21 21:16:38 +00:00
|
|
|
// If the document is currently hidden, skip the call.
|
|
|
|
if (DocumentVisibilityService.isHidden()) {
|
|
|
|
this.setupTimer_();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
var that = this;
|
|
|
|
this.working = true;
|
|
|
|
this.scope_.$apply(function() {
|
|
|
|
that.requester_(function(status) {
|
|
|
|
if (status) {
|
|
|
|
that.working = false;
|
|
|
|
that.setupTimer_();
|
|
|
|
} else {
|
|
|
|
that.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
_PollChannel.prototype.setupTimer_ = function() {
|
|
|
|
if (this.timer_) { return; }
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
this.polling = true;
|
|
|
|
this.timer_ = $timeout(function() {
|
|
|
|
that.timer_ = null;
|
|
|
|
that.call_();
|
|
|
|
}, this.sleeptime_)
|
|
|
|
};
|
|
|
|
|
|
|
|
var service = {
|
|
|
|
'create': function(scope, requester, opt_sleeptime) {
|
|
|
|
return new _PollChannel(scope, requester, opt_sleeptime);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return service;
|
|
|
|
}]);
|