2015-05-04 21:46:30 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
loader.lazyRequireGetter(this, "defer",
|
2015-08-26 16:05:14 +03:00
|
|
|
"promise", true);
|
2015-05-04 21:46:30 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor Poller
|
|
|
|
* Takes a function that is to be called on an interval,
|
|
|
|
* and can be turned on and off via methods to execute `fn` on the interval
|
|
|
|
* specified during `on`. If `fn` returns a promise, the polling waits for
|
|
|
|
* that promise to resolve before waiting the interval to call again.
|
|
|
|
*
|
|
|
|
* Specify the `wait` duration between polling here, and optionally
|
|
|
|
* an `immediate` boolean, indicating whether the function should be called
|
|
|
|
* immediately when toggling on.
|
|
|
|
*
|
|
|
|
* @param {function} fn
|
|
|
|
* @param {number} wait
|
|
|
|
* @param {boolean?} immediate
|
|
|
|
*/
|
2016-05-17 21:25:54 +03:00
|
|
|
function Poller(fn, wait, immediate) {
|
2015-05-04 21:46:30 +03:00
|
|
|
this._fn = fn;
|
|
|
|
this._wait = wait;
|
|
|
|
this._immediate = immediate;
|
|
|
|
this._poll = this._poll.bind(this);
|
|
|
|
this._preparePoll = this._preparePoll.bind(this);
|
|
|
|
}
|
|
|
|
exports.Poller = Poller;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a boolean indicating whether or not poller
|
|
|
|
* is polling.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2016-05-17 21:25:54 +03:00
|
|
|
Poller.prototype.isPolling = function pollerIsPolling() {
|
2015-05-04 21:46:30 +03:00
|
|
|
return !!this._timer;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turns polling on.
|
|
|
|
*
|
|
|
|
* @return {Poller}
|
|
|
|
*/
|
2016-05-17 21:25:54 +03:00
|
|
|
Poller.prototype.on = function pollerOn() {
|
2015-05-04 21:46:30 +03:00
|
|
|
if (this._destroyed) {
|
|
|
|
throw Error("Poller cannot be turned on after destruction.");
|
|
|
|
}
|
|
|
|
if (this._timer) {
|
|
|
|
this.off();
|
|
|
|
}
|
|
|
|
this._immediate ? this._poll() : this._preparePoll();
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turns off polling. Returns a promise that resolves when
|
|
|
|
* the last outstanding `fn` call finishes if it's an async function.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2016-05-17 21:25:54 +03:00
|
|
|
Poller.prototype.off = function pollerOff() {
|
2015-05-04 21:46:30 +03:00
|
|
|
let { resolve, promise } = defer();
|
|
|
|
if (this._timer) {
|
2016-04-21 20:20:14 +03:00
|
|
|
clearTimeout(this._timer);
|
2015-05-04 21:46:30 +03:00
|
|
|
this._timer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settle an inflight poll call before resolving
|
|
|
|
// if using a promise-backed poll function
|
|
|
|
if (this._inflight) {
|
|
|
|
this._inflight.then(resolve);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turns off polling and removes the reference to the poller function.
|
2016-05-19 17:35:45 +03:00
|
|
|
* Resolves when the last outstanding `fn` call finishes if it's an async
|
|
|
|
* function.
|
2015-05-04 21:46:30 +03:00
|
|
|
*/
|
2016-05-17 21:25:54 +03:00
|
|
|
Poller.prototype.destroy = function pollerDestroy() {
|
2015-05-04 21:46:30 +03:00
|
|
|
return this.off().then(() => {
|
|
|
|
this._destroyed = true;
|
2016-05-17 21:25:54 +03:00
|
|
|
this._fn = null;
|
2015-05-04 21:46:30 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-05-17 21:25:54 +03:00
|
|
|
Poller.prototype._preparePoll = function pollerPrepare() {
|
2016-04-21 20:20:14 +03:00
|
|
|
this._timer = setTimeout(this._poll, this._wait);
|
2015-05-04 21:46:30 +03:00
|
|
|
};
|
|
|
|
|
2016-05-17 21:25:54 +03:00
|
|
|
Poller.prototype._poll = function pollerPoll() {
|
2015-05-04 21:46:30 +03:00
|
|
|
let response = this._fn();
|
|
|
|
if (response && typeof response.then === "function") {
|
|
|
|
// Store the most recent in-flight polling
|
|
|
|
// call so we can clean it up when disabling
|
|
|
|
this._inflight = response;
|
|
|
|
response.then(() => {
|
|
|
|
// Only queue up the next call if poller was not turned off
|
|
|
|
// while this async poll call was in flight.
|
|
|
|
if (this._timer) {
|
|
|
|
this._preparePoll();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._preparePoll();
|
|
|
|
}
|
|
|
|
};
|