mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 09:19:04 -04:00
remove running state, use deferred instead, fix tests and mocks
This commit is contained in:
parent
69a4541044
commit
7d082dd887
2 changed files with 35 additions and 29 deletions
|
@ -15,7 +15,6 @@ define(function (require) {
|
|||
this.queue = [];
|
||||
this.completedQueue = [];
|
||||
this.requestHandlers = {};
|
||||
this.running = false;
|
||||
this.activeRequest = null;
|
||||
this.notifyEvent = null;
|
||||
}
|
||||
|
@ -47,6 +46,9 @@ define(function (require) {
|
|||
self._setRequestHandlers(opts);
|
||||
|
||||
return Promise.try(function () {
|
||||
return self._startRequest();
|
||||
})
|
||||
.then(function () {
|
||||
return self._extractQueue(opts.direction);
|
||||
})
|
||||
.then(function () {
|
||||
|
@ -54,7 +56,7 @@ define(function (require) {
|
|||
return req;
|
||||
})
|
||||
.then(function (req) {
|
||||
return self._startRequest(req);
|
||||
return self._setRequest(req);
|
||||
})
|
||||
.then(function () {
|
||||
return self._executeRequest(req, opts);
|
||||
|
@ -69,7 +71,6 @@ define(function (require) {
|
|||
|
||||
return new Promise(function (resolve) {
|
||||
self._setRequest();
|
||||
self.running = false;
|
||||
|
||||
if (self.searchPromise && 'abort' in self.searchPromise) {
|
||||
self.searchPromise.abort();
|
||||
|
@ -79,7 +80,7 @@ define(function (require) {
|
|||
});
|
||||
};
|
||||
|
||||
segmentedFetch.prototype._startRequest = function (req) {
|
||||
segmentedFetch.prototype._startRequest = function () {
|
||||
var self = this;
|
||||
self.requestStats = {
|
||||
took: 0,
|
||||
|
@ -90,16 +91,23 @@ define(function (require) {
|
|||
}
|
||||
};
|
||||
|
||||
return new Promise(function (resolve) {
|
||||
// stop any existing segmentedFetches
|
||||
if (self.running) {
|
||||
self._setRequest();
|
||||
}
|
||||
|
||||
self._setRequest(req);
|
||||
self.running = true;
|
||||
function initRequest() {
|
||||
self._processDeferred = Promise.defer();
|
||||
self.notifyEvent = notify.event(eventName);
|
||||
resolve();
|
||||
}
|
||||
|
||||
return new Promise(function (resolve) {
|
||||
// cause existing request to exit
|
||||
if (self._processDeferred) {
|
||||
self._setRequest();
|
||||
self._processDeferred.promise.then(function () {
|
||||
initRequest();
|
||||
resolve();
|
||||
});
|
||||
} else {
|
||||
initRequest();
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -109,7 +117,7 @@ define(function (require) {
|
|||
return new Promise(function (resolve) {
|
||||
self._setRequest();
|
||||
self._clearNotification();
|
||||
self.running = false;
|
||||
self._processDeferred.resolve();
|
||||
resolve();
|
||||
});
|
||||
};
|
||||
|
@ -235,9 +243,8 @@ define(function (require) {
|
|||
|
||||
return self._executeSearch(index, state)
|
||||
.then(function (resp) {
|
||||
// abort if not in running state, or fetch is called twice quickly
|
||||
if (!self.running || req !== self.activeRequest) {
|
||||
// return self._processQueueComplete(req, loopCount);
|
||||
// abort if request changed (fetch is called twice quickly)
|
||||
if (req !== self.activeRequest) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -67,7 +67,6 @@ define(function (require) {
|
|||
|
||||
describe('fetch', function () {
|
||||
it('should return a promise', function () {
|
||||
sinon.stub(SegmentedFetch.prototype, '_startRequest', Promise.resolve);
|
||||
sinon.stub(SegmentedFetch.prototype, '_executeRequest', Promise.resolve);
|
||||
|
||||
var fetch = segmentedFetch.fetch();
|
||||
|
@ -75,34 +74,31 @@ define(function (require) {
|
|||
return fetch;
|
||||
});
|
||||
|
||||
it('should set the running state', function () {
|
||||
var stopStub = sinon.stub(SegmentedFetch.prototype, '_stopRequest', Promise.resolve);
|
||||
it('should stop the request', function () {
|
||||
var stopSpy = sinon.spy(SegmentedFetch.prototype, '_stopRequest');
|
||||
sinon.stub(SegmentedFetch.prototype, '_executeRequest', Promise.resolve);
|
||||
|
||||
return segmentedFetch.fetch().then(function () {
|
||||
expect(segmentedFetch.running).to.be(true);
|
||||
expect(stopStub.callCount).to.be(1);
|
||||
expect(stopSpy.callCount).to.be(1);
|
||||
});
|
||||
});
|
||||
|
||||
it('should stop existing requests', function () {
|
||||
var stopStub = sinon.stub(SegmentedFetch.prototype, '_stopRequest', Promise.resolve);
|
||||
it('should stop multiple requests', function () {
|
||||
var stopSpy = sinon.spy(SegmentedFetch.prototype, '_stopRequest');
|
||||
sinon.stub(SegmentedFetch.prototype, '_executeRequest').returns(Promise.delay(5));
|
||||
|
||||
segmentedFetch.fetch();
|
||||
|
||||
return Promise.delay(1).then(function () {
|
||||
expect(segmentedFetch.running).to.be(true);
|
||||
return segmentedFetch.fetch().then(function () {
|
||||
// 1 for stopping the first request early
|
||||
// 1 for finishing the second request
|
||||
expect(stopStub.callCount).to.be(2);
|
||||
expect(stopSpy.callCount).to.be(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should perform actions on searchSource', function () {
|
||||
sinon.stub(SegmentedFetch.prototype, '_startRequest', Promise.resolve);
|
||||
sinon.stub(SegmentedFetch.prototype, '_executeRequest', Promise.resolve);
|
||||
|
||||
return segmentedFetch.fetch().then(function () {
|
||||
|
@ -230,11 +226,14 @@ define(function (require) {
|
|||
var SegmentedFetchSelf = this;
|
||||
var fakeRequest = {};
|
||||
|
||||
return SegmentedFetchSelf._startRequest(fakeRequest)
|
||||
return SegmentedFetchSelf._startRequest()
|
||||
.then(function () {
|
||||
SegmentedFetchSelf._setRequest(fakeRequest);
|
||||
})
|
||||
.then(function () {
|
||||
// dumb mock or the fetch lifecycle
|
||||
// loop, running each
|
||||
while (SegmentedFetchSelf.running) {
|
||||
while (SegmentedFetchSelf.activeRequest !== null) {
|
||||
if (typeof opts.each === 'function') {
|
||||
opts.each();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue