Commit 14b077b7 authored by Filipa Lacerda's avatar Filipa Lacerda

Adds stop function so we can stop polling anytime

parent d576fd82
......@@ -36,20 +36,23 @@ export default class Poll {
this.options.data = options.data || {};
this.intervalHeader = 'POLL-INTERVAL';
this.canPoll = true;
}
checkConditions(response) {
const headers = gl.utils.normalizeHeaders(response.headers);
const pollInterval = headers[this.intervalHeader];
if (pollInterval > 0 && response.status === httpStatusCodes.OK) {
this.options.successCallback(response);
if (pollInterval > 0 && response.status === httpStatusCodes.OK && this.canPoll) {
setTimeout(() => {
this.makeRequest();
// Stop can be called in the meanwhile, so let's check again.
if (this.canPoll) {
this.makeRequest();
}
}, pollInterval);
} else {
this.options.successCallback(response);
}
this.options.successCallback(response);
}
makeRequest() {
......@@ -59,4 +62,8 @@ export default class Poll {
.then(response => this.checkConditions(response))
.catch(error => errorCallback(error));
}
stop() {
this.canPoll = false;
}
}
......@@ -124,4 +124,40 @@ describe('Poll', () => {
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
});
describe('stop', () => {
it('stops polling when method is called', (done) => {
const pollInterceptor = (request, next) => {
next(request.respondWith(JSON.stringify([]), { status: 200, headers: { 'poll-interval': 2 } }));
};
Vue.http.interceptors.push(pollInterceptor);
const service = new ServiceMock('endpoint');
spyOn(service, 'fetch').and.callThrough();
const Polling = new Poll({
resource: service,
method: 'fetch',
data: { page: 1 },
successCallback: () => {
Polling.stop();
},
errorCallback: callbacks.error,
});
spyOn(Polling, 'stop').and.callThrough();
Polling.makeRequest();
setTimeout(() => {
expect(service.fetch.calls.count()).toEqual(1);
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
expect(Polling.stop).toHaveBeenCalled();
done();
}, 100);
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
});
});
});
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