Adds restart method and auxiliar callback to polling class
This commit is contained in:
parent
7ac4127e4c
commit
9f7c19b352
|
@ -5,23 +5,37 @@ import httpStatusCodes from './http_status';
|
||||||
* Service for vue resouce and method need to be provided as props
|
* Service for vue resouce and method need to be provided as props
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* new poll({
|
* new Poll({
|
||||||
* resource: resource,
|
* resource: resource,
|
||||||
* method: 'name',
|
* method: 'name',
|
||||||
* data: {page: 1, scope: 'all'},
|
* data: {page: 1, scope: 'all'},
|
||||||
* successCallback: () => {},
|
* successCallback: () => {},
|
||||||
* errorCallback: () => {},
|
* errorCallback: () => {},
|
||||||
|
* auxiliarCallback: () => {},
|
||||||
* }).makeRequest();
|
* }).makeRequest();
|
||||||
*
|
*
|
||||||
* this.service = new BoardsService(endpoint);
|
* Usage in pipelines table with visibility lib:
|
||||||
* new poll({
|
|
||||||
* resource: this.service,
|
|
||||||
* method: 'get',
|
|
||||||
* data: {page: 1, scope: 'all'},
|
|
||||||
* successCallback: () => {},
|
|
||||||
* errorCallback: () => {},
|
|
||||||
* }).makeRequest();
|
|
||||||
*
|
*
|
||||||
|
* const poll = new Poll({
|
||||||
|
* resource: this.service,
|
||||||
|
* method: 'getPipelines',
|
||||||
|
* data: { page: pageNumber, scope },
|
||||||
|
* successCallback: this.successCallback,
|
||||||
|
* errorCallback: this.errorCallback,
|
||||||
|
* auxiliarCallback: this.updateLoading,
|
||||||
|
* });
|
||||||
|
*
|
||||||
|
* if (!Visibility.hidden()) {
|
||||||
|
* poll.makeRequest();
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* Visibility.change(() => {
|
||||||
|
* if (!Visibility.hidden()) {
|
||||||
|
* poll.restart();
|
||||||
|
* } else {
|
||||||
|
* poll.stop();
|
||||||
|
* }
|
||||||
|
* });
|
||||||
*
|
*
|
||||||
* 1. Checks for response and headers before start polling
|
* 1. Checks for response and headers before start polling
|
||||||
* 2. Interval is provided by `Poll-Interval` header.
|
* 2. Interval is provided by `Poll-Interval` header.
|
||||||
|
@ -54,7 +68,10 @@ export default class Poll {
|
||||||
}
|
}
|
||||||
|
|
||||||
makeRequest() {
|
makeRequest() {
|
||||||
const { resource, method, data, errorCallback } = this.options;
|
const { resource, method, data, errorCallback, auxiliarCallback } = this.options;
|
||||||
|
|
||||||
|
// It's called everytime a new request is made. Useful to update the status.
|
||||||
|
auxiliarCallback(true);
|
||||||
|
|
||||||
return resource[method](data)
|
return resource[method](data)
|
||||||
.then(response => this.checkConditions(response))
|
.then(response => this.checkConditions(response))
|
||||||
|
@ -70,4 +87,12 @@ export default class Poll {
|
||||||
this.canPoll = false;
|
this.canPoll = false;
|
||||||
clearTimeout(this.timeoutID);
|
clearTimeout(this.timeoutID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restarts polling after it has been stoped
|
||||||
|
*/
|
||||||
|
restart() {
|
||||||
|
this.canPoll = true;
|
||||||
|
this.makeRequest();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -160,4 +160,44 @@ describe('Poll', () => {
|
||||||
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
|
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('restart', () => {
|
||||||
|
it('should restart polling when its 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();
|
||||||
|
setTimeout(() => {
|
||||||
|
Polling.restart();
|
||||||
|
}, 0);
|
||||||
|
},
|
||||||
|
errorCallback: callbacks.error,
|
||||||
|
});
|
||||||
|
|
||||||
|
spyOn(Polling, 'stop').and.callThrough();
|
||||||
|
|
||||||
|
Polling.makeRequest();
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
expect(service.fetch.calls.count()).toEqual(2);
|
||||||
|
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
|
||||||
|
expect(Polling.stop).toHaveBeenCalled();
|
||||||
|
done();
|
||||||
|
}, 10);
|
||||||
|
|
||||||
|
Vue.http.interceptors = _.without(Vue.http.interceptors, pollInterceptor);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue