Commit ec5bbd27 authored by Stan Hu's avatar Stan Hu

Remove duplicate XHR request when requesting new pipeline page

When a user clicked on another page in the pipeline page, the following
would happen:

1. PipelinesService.getPipelines() would be called to fetch the new page
2. In the success handler, Poll.restart() would be called
3. This would invoke Poll.makeRequest(), which would fire off a new
request.

To fix this, we introduce a enable(), which will:

1. Update the request data accordingly
2. Clear the old timeout if necessary and start a new timer

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/58095
parent 7e37b4e4
...@@ -63,6 +63,10 @@ export default class Poll { ...@@ -63,6 +63,10 @@ export default class Poll {
const headers = normalizeHeaders(response.headers); const headers = normalizeHeaders(response.headers);
const pollInterval = parseInt(headers[this.intervalHeader], 10); const pollInterval = parseInt(headers[this.intervalHeader], 10);
if (pollInterval > 0 && successCodes.indexOf(response.status) !== -1 && this.canPoll) { if (pollInterval > 0 && successCodes.indexOf(response.status) !== -1 && this.canPoll) {
if (this.timeoutID) {
clearTimeout(this.timeoutID);
}
this.timeoutID = setTimeout(() => { this.timeoutID = setTimeout(() => {
this.makeRequest(); this.makeRequest();
}, pollInterval); }, pollInterval);
...@@ -101,15 +105,25 @@ export default class Poll { ...@@ -101,15 +105,25 @@ export default class Poll {
} }
/** /**
* Restarts polling after it has been stoped * Enables polling after it has been stopped
*/ */
restart(options) { enable(options) {
// update data
if (options && options.data) { if (options && options.data) {
this.options.data = options.data; this.options.data = options.data;
} }
this.canPoll = true; 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(); this.makeRequest();
} }
} }
...@@ -94,8 +94,7 @@ export default { ...@@ -94,8 +94,7 @@ export default {
this.isLoading = false; this.isLoading = false;
this.successCallback(response); this.successCallback(response);
// restart polling this.poll.enable({ data: this.requestData, response });
this.poll.restart({ data: this.requestData });
}) })
.catch(() => { .catch(() => {
this.isLoading = false; this.isLoading = false;
......
---
title: Remove duplicate XHR request when requesting new pipeline page
merge_request: 25506
author:
type: fixed
...@@ -153,6 +153,36 @@ describe('Poll', () => { ...@@ -153,6 +153,36 @@ describe('Poll', () => {
}); });
}); });
describe('enable', () => {
it('should enable polling upon a response', done => {
jasmine.clock().install();
const Polling = new Poll({
resource: service,
method: 'fetch',
data: { page: 1 },
successCallback: () => {},
});
Polling.enable({
data: { page: 4 },
response: { status: 200, headers: { 'poll-interval': 1 } },
});
jasmine.clock().tick(1);
jasmine.clock().uninstall();
waitForAllCallsToFinish(service, 1, () => {
Polling.stop();
expect(service.fetch.calls.count()).toEqual(1);
expect(service.fetch).toHaveBeenCalledWith({ page: 4 });
expect(Polling.options.data).toEqual({ page: 4 });
done();
});
});
});
describe('restart', () => { describe('restart', () => {
it('should restart polling when its called', done => { it('should restart polling when its called', done => {
mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } }); mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } });
...@@ -171,6 +201,7 @@ describe('Poll', () => { ...@@ -171,6 +201,7 @@ describe('Poll', () => {
}); });
spyOn(Polling, 'stop').and.callThrough(); spyOn(Polling, 'stop').and.callThrough();
spyOn(Polling, 'enable').and.callThrough();
spyOn(Polling, 'restart').and.callThrough(); spyOn(Polling, 'restart').and.callThrough();
Polling.makeRequest(); Polling.makeRequest();
...@@ -181,6 +212,7 @@ describe('Poll', () => { ...@@ -181,6 +212,7 @@ describe('Poll', () => {
expect(service.fetch.calls.count()).toEqual(2); expect(service.fetch.calls.count()).toEqual(2);
expect(service.fetch).toHaveBeenCalledWith({ page: 4 }); expect(service.fetch).toHaveBeenCalledWith({ page: 4 });
expect(Polling.stop).toHaveBeenCalled(); expect(Polling.stop).toHaveBeenCalled();
expect(Polling.enable).toHaveBeenCalled();
expect(Polling.restart).toHaveBeenCalled(); expect(Polling.restart).toHaveBeenCalled();
expect(Polling.options.data).toEqual({ page: 4 }); expect(Polling.options.data).toEqual({ page: 4 });
done(); done();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment