2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
/**
|
|
|
|
* Instances of SmartInterval extend the functionality of `setInterval`, make it configurable
|
|
|
|
* and controllable by a public API.
|
2021-07-15 08:09:01 -04:00
|
|
|
*
|
|
|
|
* This component has two intervals:
|
|
|
|
*
|
|
|
|
* - current interval - when the page is visible - defined by `startingInterval`, `maxInterval`, and `incrementByFactorOf`
|
|
|
|
* - Example:
|
|
|
|
* - `startingInterval: 10000`, `maxInterval: 240000`, `incrementByFactorOf: 2`
|
|
|
|
* - results in `10s, 20s, 40s, 80s, ..., 240s`, it stops increasing at `240s` and keeps this interval indefinitely.
|
|
|
|
* - hidden interval - when the page is not visible
|
|
|
|
*
|
|
|
|
* Visibility transitions:
|
|
|
|
*
|
|
|
|
* - `visible -> not visible`
|
|
|
|
* - `document.addEventListener('visibilitychange', () => ...)`
|
|
|
|
*
|
|
|
|
* > This event fires with a visibilityState of hidden when a user navigates to a new page, switches tabs, closes the tab, minimizes or closes the browser, or, on mobile, switches from the browser to a different app.
|
|
|
|
*
|
|
|
|
* Source [Document: visibilitychange event - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilitychange_event)
|
|
|
|
*
|
|
|
|
* - `window.addEventListener('blur', () => ...)` - every time user clicks somewhere else then in the browser page
|
|
|
|
* - `not visible -> visible`
|
|
|
|
* - `document.addEventListener('visibilitychange', () => ...)` same as the transition `visible -> not visible`
|
|
|
|
* - `window.addEventListener('focus', () => ...)`
|
|
|
|
*
|
|
|
|
* The combination of these two listeners can result in an unexpected resumption of polling:
|
|
|
|
*
|
|
|
|
* - switch to a different window (causes `blur`)
|
|
|
|
* - switch to a different desktop (causes `visibilitychange` (not visible))
|
|
|
|
* - switch back to the original desktop (causes `visibilitychange` (visible))
|
|
|
|
* - *now the polling happens even in window that user doesn't work in*
|
2017-07-05 13:20:41 -04:00
|
|
|
*/
|
|
|
|
|
2017-10-31 08:15:54 -04:00
|
|
|
export default class SmartInterval {
|
2017-07-05 13:20:41 -04:00
|
|
|
/**
|
2017-11-06 01:38:09 -05:00
|
|
|
* @param { function } opts.callback Function that returns a promise, called on each iteration
|
|
|
|
* unless still in progress (required)
|
2017-07-05 13:20:41 -04:00
|
|
|
* @param { milliseconds } opts.startingInterval `currentInterval` is set to this initially
|
|
|
|
* @param { milliseconds } opts.maxInterval `currentInterval` will be incremented to this
|
|
|
|
* @param { milliseconds } opts.hiddenInterval `currentInterval` is set to this
|
|
|
|
* when the page is hidden
|
|
|
|
* @param { integer } opts.incrementByFactorOf `currentInterval` is incremented by this factor
|
|
|
|
* @param { boolean } opts.lazyStart Configure if timer is initialized on
|
|
|
|
* instantiation or lazily
|
|
|
|
* @param { boolean } opts.immediateExecution Configure if callback should
|
|
|
|
* be executed before the first interval.
|
|
|
|
*/
|
|
|
|
constructor(opts = {}) {
|
|
|
|
this.cfg = {
|
|
|
|
callback: opts.callback,
|
|
|
|
startingInterval: opts.startingInterval,
|
|
|
|
maxInterval: opts.maxInterval,
|
|
|
|
hiddenInterval: opts.hiddenInterval,
|
|
|
|
incrementByFactorOf: opts.incrementByFactorOf,
|
|
|
|
lazyStart: opts.lazyStart,
|
|
|
|
immediateExecution: opts.immediateExecution,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
intervalId: null,
|
|
|
|
currentInterval: this.cfg.startingInterval,
|
2020-04-14 11:09:44 -04:00
|
|
|
pagevisibile: true,
|
2017-07-05 13:20:41 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
this.initInterval();
|
|
|
|
}
|
2016-11-25 13:42:25 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
/* public */
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
start() {
|
2018-06-16 17:50:13 -04:00
|
|
|
const { cfg, state } = this;
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-10-24 11:58:05 -04:00
|
|
|
if (cfg.immediateExecution && !this.isLoading) {
|
2017-07-05 13:20:41 -04:00
|
|
|
cfg.immediateExecution = false;
|
2017-10-24 11:58:05 -04:00
|
|
|
this.triggerCallback();
|
2016-11-18 10:49:40 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
state.intervalId = window.setInterval(() => {
|
2017-10-24 11:58:05 -04:00
|
|
|
if (this.isLoading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.triggerCallback();
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
if (this.getCurrentInterval() === cfg.maxInterval) {
|
|
|
|
return;
|
2016-11-25 13:42:25 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
this.incrementInterval();
|
|
|
|
this.resume();
|
|
|
|
}, this.getCurrentInterval());
|
|
|
|
}
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
// cancel the existing timer, setting the currentInterval back to startingInterval
|
|
|
|
cancel() {
|
|
|
|
this.setCurrentInterval(this.cfg.startingInterval);
|
|
|
|
this.stopTimer();
|
|
|
|
}
|
2016-11-25 13:42:25 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
onVisibilityHidden() {
|
|
|
|
if (this.cfg.hiddenInterval) {
|
|
|
|
this.setCurrentInterval(this.cfg.hiddenInterval);
|
|
|
|
this.resume();
|
|
|
|
} else {
|
2016-11-18 10:49:40 -05:00
|
|
|
this.cancel();
|
|
|
|
}
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
// start a timer, using the existing interval
|
|
|
|
resume() {
|
2017-10-24 11:58:05 -04:00
|
|
|
this.stopTimer(); // stop existing timer, in case timer was not previously stopped
|
2017-07-05 13:20:41 -04:00
|
|
|
this.start();
|
|
|
|
}
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
onVisibilityVisible() {
|
|
|
|
this.cancel();
|
|
|
|
this.start();
|
|
|
|
}
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
destroy() {
|
2020-04-14 11:09:44 -04:00
|
|
|
document.removeEventListener('visibilitychange', this.onVisibilityChange);
|
|
|
|
window.removeEventListener('blur', this.onWindowVisibilityChange);
|
|
|
|
window.removeEventListener('focus', this.onWindowVisibilityChange);
|
2017-07-05 13:20:41 -04:00
|
|
|
this.cancel();
|
2020-12-08 16:10:06 -05:00
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2020-12-23 07:10:26 -05:00
|
|
|
$(document).off('visibilitychange').off('beforeunload');
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
/* private */
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
initInterval() {
|
2018-06-16 17:50:13 -04:00
|
|
|
const { cfg } = this;
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
if (!cfg.lazyStart) {
|
|
|
|
this.start();
|
2016-11-18 10:49:40 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
this.initVisibilityChangeHandling();
|
|
|
|
this.initPageUnloadHandling();
|
|
|
|
}
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-10-24 11:58:05 -04:00
|
|
|
triggerCallback() {
|
|
|
|
this.isLoading = true;
|
2018-10-10 03:13:34 -04:00
|
|
|
this.cfg
|
|
|
|
.callback()
|
2017-10-24 11:58:05 -04:00
|
|
|
.then(() => {
|
|
|
|
this.isLoading = false;
|
|
|
|
})
|
2020-12-23 19:10:25 -05:00
|
|
|
.catch((err) => {
|
2017-10-24 11:58:05 -04:00
|
|
|
this.isLoading = false;
|
2017-11-07 02:27:32 -05:00
|
|
|
throw err;
|
2017-10-24 11:58:05 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-14 11:09:44 -04:00
|
|
|
onWindowVisibilityChange(e) {
|
|
|
|
this.state.pagevisibile = e.type === 'focus';
|
|
|
|
this.handleVisibilityChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
onVisibilityChange(e) {
|
|
|
|
this.state.pagevisibile = e.target.visibilityState === 'visible';
|
|
|
|
this.handleVisibilityChange();
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
initVisibilityChangeHandling() {
|
2020-04-14 11:09:44 -04:00
|
|
|
// cancel interval when tab or window is no longer shown (prevents cached pages from polling)
|
|
|
|
document.addEventListener('visibilitychange', this.onVisibilityChange.bind(this));
|
|
|
|
window.addEventListener('blur', this.onWindowVisibilityChange.bind(this));
|
|
|
|
window.addEventListener('focus', this.onWindowVisibilityChange.bind(this));
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
initPageUnloadHandling() {
|
|
|
|
// TODO: Consider refactoring in light of turbolinks removal.
|
|
|
|
// prevent interval continuing after page change, when kept in cache by Turbolinks
|
|
|
|
$(document).on('beforeunload', () => this.cancel());
|
|
|
|
}
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2020-04-14 11:09:44 -04:00
|
|
|
handleVisibilityChange() {
|
2018-10-10 03:13:34 -04:00
|
|
|
const intervalAction = this.isPageVisible()
|
|
|
|
? this.onVisibilityVisible
|
|
|
|
: this.onVisibilityHidden;
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
intervalAction.apply(this);
|
|
|
|
}
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
getCurrentInterval() {
|
|
|
|
return this.state.currentInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
setCurrentInterval(newInterval) {
|
|
|
|
this.state.currentInterval = newInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
incrementInterval() {
|
2018-06-16 17:50:13 -04:00
|
|
|
const { cfg } = this;
|
2017-07-05 13:20:41 -04:00
|
|
|
const currentInterval = this.getCurrentInterval();
|
|
|
|
if (cfg.hiddenInterval && !this.isPageVisible()) return;
|
|
|
|
let nextInterval = currentInterval * cfg.incrementByFactorOf;
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
if (nextInterval > cfg.maxInterval) {
|
|
|
|
nextInterval = cfg.maxInterval;
|
2016-11-18 10:49:40 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
this.setCurrentInterval(nextInterval);
|
|
|
|
}
|
2016-11-25 13:42:25 -05:00
|
|
|
|
2018-10-10 03:13:34 -04:00
|
|
|
isPageVisible() {
|
2020-04-14 11:09:44 -04:00
|
|
|
return this.state.pagevisibile;
|
2018-10-10 03:13:34 -04:00
|
|
|
}
|
2016-11-18 10:49:40 -05:00
|
|
|
|
2017-07-05 13:20:41 -04:00
|
|
|
stopTimer() {
|
2018-06-16 17:50:13 -04:00
|
|
|
const { state } = this;
|
2017-07-05 13:20:41 -04:00
|
|
|
|
|
|
|
state.intervalId = window.clearInterval(state.intervalId);
|
2016-11-18 10:49:40 -05:00
|
|
|
}
|
2017-07-05 13:20:41 -04:00
|
|
|
}
|