gitlab-org--gitlab-foss/app/assets/javascripts/lib/utils/poll.js

151 lines
3.9 KiB
JavaScript
Raw Normal View History

import { normalizeHeaders } from './common_utils';
import httpStatusCodes, { successCodes } from './http_status';
2017-03-17 13:46:45 -04:00
/**
* Polling utility for handling realtime updates.
* Requirements: Promise based HTTP client
*
* Service for promise based http client and method need to be provided as props
2017-03-17 13:46:45 -04:00
*
* @example
* new Poll({
* resource: resource,
* method: 'name',
2017-03-24 14:27:01 -04:00
* data: {page: 1, scope: 'all'}, // optional
* successCallback: () => {},
* errorCallback: () => {},
2017-03-24 14:27:01 -04:00
* notificationCallback: () => {}, // optional
* }).makeRequest();
*
* Usage in pipelines table with visibility lib:
*
* const poll = new Poll({
* resource: this.service,
* method: 'getPipelines',
* data: { page: pageNumber, scope },
* successCallback: this.successCallback,
* errorCallback: this.errorCallback,
2017-03-24 14:27:01 -04:00
* notificationCallback: this.updateLoading,
* });
*
* if (!Visibility.hidden()) {
* poll.makeRequest();
* }
2017-03-17 13:46:45 -04:00
*
* Visibility.change(() => {
* if (!Visibility.hidden()) {
* poll.restart();
* } else {
* poll.stop();
* }
* });
2017-03-17 13:46:45 -04:00
*
* 1. Checks for response and headers before start polling
2017-03-17 14:04:35 -04:00
* 2. Interval is provided by `Poll-Interval` header.
* 3. If `Poll-Interval` is -1, we stop polling
2017-03-17 13:46:45 -04:00
* 4. If HTTP response is 200, we poll.
* 5. If HTTP response is different from 200, we stop polling.
*
* @example
* // With initial delay (for, for example, reducing unnecessary requests)
*
* const poll = new Poll({
* resource: this.service,
* method: 'fetchNotes',
* successCallback: () => {},
* errorCallback: () => {},
* });
*
* // Performs the first request in 2.5s and then uses the `Poll-Interval` header.
* poll.makeDelayedRequest(2500);
*
2017-03-17 13:46:45 -04:00
*/
2017-03-22 06:01:29 -04:00
export default class Poll {
2017-03-17 14:04:35 -04:00
constructor(options = {}) {
this.options = options;
this.options.data = options.data || {};
this.options.notificationCallback =
options.notificationCallback || function notificationCallback() {};
2017-03-17 13:46:45 -04:00
this.intervalHeader = 'POLL-INTERVAL';
2017-03-23 12:12:43 -04:00
this.timeoutID = null;
this.canPoll = true;
2017-03-17 13:46:45 -04:00
}
checkConditions(response) {
const headers = normalizeHeaders(response.headers);
2017-03-28 09:20:33 -04:00
const pollInterval = parseInt(headers[this.intervalHeader], 10);
if (pollInterval > 0 && successCodes.indexOf(response.status) !== -1 && this.canPoll) {
if (this.timeoutID) {
clearTimeout(this.timeoutID);
}
2017-03-23 12:12:43 -04:00
this.timeoutID = setTimeout(() => {
this.makeRequest();
2017-03-17 13:46:45 -04:00
}, pollInterval);
}
this.options.successCallback(response);
2017-03-17 13:46:45 -04:00
}
makeDelayedRequest(delay = 0) {
// So we don't make our specs artificially slower
this.timeoutID = setTimeout(
() => this.makeRequest(),
process.env.NODE_ENV !== 'test' ? delay : 1,
);
}
2017-03-17 13:46:45 -04:00
makeRequest() {
2017-03-24 14:27:01 -04:00
const { resource, method, data, errorCallback, notificationCallback } = this.options;
// It's called everytime a new request is made. Useful to update the status.
2017-03-24 14:27:01 -04:00
notificationCallback(true);
2017-03-21 14:07:02 -04:00
return resource[method](data)
.then((response) => {
2017-03-29 11:01:47 -04:00
this.checkConditions(response);
notificationCallback(false);
})
.catch((error) => {
2017-03-29 11:01:47 -04:00
notificationCallback(false);
if (error.status === httpStatusCodes.ABORTED) {
return;
}
2017-03-29 11:01:47 -04:00
errorCallback(error);
});
2017-03-17 13:46:45 -04:00
}
2017-03-23 12:12:43 -04:00
/**
* Stops the polling recursive chain
* and guarantees if the timeout is already running it won't make another request by
* cancelling the previously established timeout.
*/
stop() {
this.canPoll = false;
2017-03-23 12:12:43 -04:00
clearTimeout(this.timeoutID);
}
/**
* Enables polling after it has been stopped
*/
enable(options) {
if (options && options.data) {
this.options.data = options.data;
}
this.canPoll = true;
if (options && options.response) {
this.checkConditions(options.response);
}
}
/**
* Restarts polling after it has been stopped and makes a request
*/
restart(options) {
this.enable(options);
this.makeRequest();
}
2017-03-17 13:46:45 -04:00
}