New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[vis/requesting] only call when actually requesting #14017
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ export function AbstractDataSourceProvider(Private, Promise, PromiseEmitter, con | |
|
||
self.history = []; | ||
self._fetchStrategy = strategy; | ||
self._requestStartHandlers = []; | ||
} | ||
|
||
/***** | ||
|
@@ -237,6 +238,39 @@ export function AbstractDataSourceProvider(Private, Promise, PromiseEmitter, con | |
*/ | ||
SourceAbstract.prototype.destroy = function () { | ||
this.cancelQueued(); | ||
this._requestStartHandlers.length = 0; | ||
}; | ||
|
||
/** | ||
* Add a handler that will be notified whenever requests start | ||
* @param {Function} handler | ||
* @return {undefined} | ||
*/ | ||
SourceAbstract.prototype.onRequestStart = function (handler) { | ||
this._requestStartHandlers.push(handler); | ||
}; | ||
|
||
/** | ||
* Called by requests of this search source when they are started | ||
* @param {Courier.Request} request | ||
* @return {Promise<undefined>} | ||
*/ | ||
SourceAbstract.prototype.requestIsStarting = function (request) { | ||
this.activeFetchCount = (this.activeFetchCount || 0) + 1; | ||
this.history = [request]; | ||
|
||
return Promise | ||
.map(this._requestStartHandlers, fn => fn(this, request)) | ||
.then(_.noop); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come you need this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documented the method as returning |
||
}; | ||
|
||
/** | ||
* Called by requests of this search source when they are done | ||
* @param {Courier.Request} request | ||
* @return {undefined} | ||
*/ | ||
SourceAbstract.prototype.requestIsStopped = function (/* request */) { | ||
this.activeFetchCount -= 1; | ||
}; | ||
|
||
/***** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import ngMock from 'ng_mock'; | ||
import sinon from 'sinon'; | ||
|
||
import { AbstractRequestProvider } from '../request'; | ||
import { requestQueue } from '../../../_request_queue'; | ||
|
||
describe('courier/fetch abstract request', () => { | ||
beforeEach(ngMock.module('kibana')); | ||
|
||
afterEach(() => { | ||
requestQueue.clear(); | ||
}); | ||
|
||
describe('#start()', () => { | ||
it('calls this.source.requestIsStarting(request)', ngMock.inject((Private) => { | ||
const AbstractReq = Private(AbstractRequestProvider); | ||
|
||
const spy = sinon.spy(() => Promise.resolve()); | ||
const source = { requestIsStarting: spy }; | ||
|
||
const req = new AbstractReq(source); | ||
expect(req.start()).to.have.property('then').a('function'); | ||
sinon.assert.calledOnce(spy); | ||
sinon.assert.calledWithExactly(spy, req); | ||
})); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import ngMock from 'ng_mock'; | ||
import sinon from 'sinon'; | ||
|
||
import { SearchRequestProvider } from '../search'; | ||
import { requestQueue } from '../../../_request_queue'; | ||
|
||
describe('ui/courier/fetch search request', () => { | ||
beforeEach(ngMock.module('kibana')); | ||
|
||
afterEach(() => { | ||
requestQueue.clear(); | ||
}); | ||
|
||
describe('#start()', () => { | ||
it('calls this.source.requestIsStarting(request)', ngMock.inject((Private) => { | ||
const SearchReq = Private(SearchRequestProvider); | ||
|
||
const spy = sinon.spy(() => Promise.resolve()); | ||
const source = { requestIsStarting: spy }; | ||
|
||
const req = new SearchReq(source); | ||
expect(req.start()).to.have.property('then').a('function'); | ||
sinon.assert.calledOnce(spy); | ||
sinon.assert.calledWithExactly(spy, req); | ||
})); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,20 +3,26 @@ import expect from 'expect.js'; | |
import ngMock from 'ng_mock'; | ||
|
||
import { SegmentedRequestProvider } from '../segmented'; | ||
import { SearchRequestProvider } from '../search'; | ||
import { AbstractRequestProvider } from '../request'; | ||
|
||
describe('SegmentedRequestProvider', () => { | ||
let Promise; | ||
let SegmentedReq; | ||
let segmentedReq; | ||
let searchReqStart; | ||
let abstractReqStart; | ||
|
||
beforeEach(ngMock.module('kibana')); | ||
|
||
beforeEach(ngMock.inject((Private, $injector) => { | ||
Promise = $injector.get('Promise'); | ||
SegmentedReq = Private(SegmentedRequestProvider); | ||
searchReqStart = sinon.spy(Private(SearchRequestProvider).prototype, 'start'); | ||
|
||
const AbstractReq = Private(AbstractRequestProvider); | ||
abstractReqStart = sinon.stub(AbstractReq.prototype, 'start', () => { | ||
const promise = Promise.resolve(); | ||
sinon.spy(promise, 'then'); | ||
return promise; | ||
}); | ||
})); | ||
|
||
describe('#start()', () => { | ||
|
@@ -30,8 +36,14 @@ describe('SegmentedRequestProvider', () => { | |
expect(returned.then).to.be.Function; | ||
}); | ||
|
||
it('calls super.start() synchronously', () => { | ||
expect(searchReqStart.called).to.be(true); | ||
it('calls AbstractReq#start()', () => { | ||
sinon.assert.calledOnce(abstractReqStart); | ||
}); | ||
|
||
it('listens to promise from super.start()', () => { | ||
sinon.assert.calledOnce(abstractReqStart); | ||
const promise = abstractReqStart.firstCall.returnValue; | ||
sinon.assert.calledOnce(promise.then); | ||
}); | ||
}); | ||
|
||
|
@@ -41,7 +53,7 @@ describe('SegmentedRequestProvider', () => { | |
|
||
function mockSource() { | ||
return { | ||
get: sinon.stub().returns(mockIndexPattern()) | ||
get: sinon.stub().returns(mockIndexPattern()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the extra comma here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a line below it at one point and deleted it later |
||
}; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be shortened to
$scope.searchSource.onRequestStart($scope.vis.requesting);
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling vis.requesting without it's
this
context might work, but seems like an unnecessary risk.