Commit 5ae245d8 authored by Robert Knight's avatar Robert Knight

Rename `pageSize` => `getPageSize`

Per PR feedback, rename the `pageSize` option to something that sounds more like a
function/callback, which is what this value now is.
parent 9c96e1fe
...@@ -35,10 +35,10 @@ export default class SearchClient extends TinyEmitter { ...@@ -35,10 +35,10 @@ export default class SearchClient extends TinyEmitter {
/** /**
* @param {(query: SearchQuery) => Promise<SearchResult>} searchFn - Function for querying the search API * @param {(query: SearchQuery) => Promise<SearchResult>} searchFn - Function for querying the search API
* @param {Object} options * @param {Object} options
* @param {(index: number) => number} [options.pageSize] - Callback that returns * @param {(index: number) => number} [options.getPageSize] -
* the page size to use when fetching the index'th page of results. * Callback that returns the page size to use when fetching the index'th
* Callers can vary this to balance the latency of getting some results * page of results. Callers can vary this to balance the latency of
* against the time taken to fetch all results. * getting some results against the time taken to fetch all results.
* *
* The returned page size must be at least 1 and no more than the maximum * The returned page size must be at least 1 and no more than the maximum
* value of the `limit` query param for the search API. * value of the `limit` query param for the search API.
...@@ -61,7 +61,7 @@ export default class SearchClient extends TinyEmitter { ...@@ -61,7 +61,7 @@ export default class SearchClient extends TinyEmitter {
constructor( constructor(
searchFn, searchFn,
{ {
pageSize = defaultPageSize, getPageSize = defaultPageSize,
separateReplies = true, separateReplies = true,
incremental = true, incremental = true,
maxResults = null, maxResults = null,
...@@ -71,7 +71,7 @@ export default class SearchClient extends TinyEmitter { ...@@ -71,7 +71,7 @@ export default class SearchClient extends TinyEmitter {
) { ) {
super(); super();
this._searchFn = searchFn; this._searchFn = searchFn;
this._pageSize = pageSize; this._getPageSize = getPageSize;
this._separateReplies = separateReplies; this._separateReplies = separateReplies;
this._incremental = incremental; this._incremental = incremental;
this._maxResults = maxResults; this._maxResults = maxResults;
...@@ -94,7 +94,7 @@ export default class SearchClient extends TinyEmitter { ...@@ -94,7 +94,7 @@ export default class SearchClient extends TinyEmitter {
* @param {number} [pageIndex] * @param {number} [pageIndex]
*/ */
async _getPage(query, searchAfter, pageIndex = 0) { async _getPage(query, searchAfter, pageIndex = 0) {
const pageSize = this._pageSize(pageIndex); const pageSize = this._getPageSize(pageIndex);
/** @type {SearchQuery} */ /** @type {SearchQuery} */
const searchQuery = { const searchQuery = {
......
...@@ -68,7 +68,7 @@ describe('SearchClient', () => { ...@@ -68,7 +68,7 @@ describe('SearchClient', () => {
}); });
it('fetches pages of results for a single URI', async () => { it('fetches pages of results for a single URI', async () => {
const client = new SearchClient(fakeSearchFn, { pageSize: () => 3 }); const client = new SearchClient(fakeSearchFn, { getPageSize: () => 3 });
client.get({ uri: 'http://example.com' }); client.get({ uri: 'http://example.com' });
await awaitEvent(client, 'end'); await awaitEvent(client, 'end');
...@@ -115,7 +115,7 @@ describe('SearchClient', () => { ...@@ -115,7 +115,7 @@ describe('SearchClient', () => {
}); });
it('emits "end" only once', done => { it('emits "end" only once', done => {
const client = new SearchClient(fakeSearchFn, { pageSize: () => 2 }); const client = new SearchClient(fakeSearchFn, { getPageSize: () => 2 });
client.on('results', sinon.stub()); client.on('results', sinon.stub());
let emitEndCounter = 0; let emitEndCounter = 0;
client.on('end', () => { client.on('end', () => {
...@@ -127,7 +127,7 @@ describe('SearchClient', () => { ...@@ -127,7 +127,7 @@ describe('SearchClient', () => {
}); });
it('emits "results" with pages in incremental mode', async () => { it('emits "results" with pages in incremental mode', async () => {
const client = new SearchClient(fakeSearchFn, { pageSize: () => 2 }); const client = new SearchClient(fakeSearchFn, { getPageSize: () => 2 });
const onResults = sinon.stub(); const onResults = sinon.stub();
client.on('results', onResults); client.on('results', onResults);
...@@ -139,7 +139,7 @@ describe('SearchClient', () => { ...@@ -139,7 +139,7 @@ describe('SearchClient', () => {
}); });
it('emits "resultCount" only once in incremental mode', async () => { it('emits "resultCount" only once in incremental mode', async () => {
const client = new SearchClient(fakeSearchFn, { pageSize: () => 2 }); const client = new SearchClient(fakeSearchFn, { getPageSize: () => 2 });
const onResultCount = sinon.stub(); const onResultCount = sinon.stub();
client.on('resultCount', onResultCount); client.on('resultCount', onResultCount);
...@@ -152,7 +152,7 @@ describe('SearchClient', () => { ...@@ -152,7 +152,7 @@ describe('SearchClient', () => {
it('emits "results" once in non-incremental mode', async () => { it('emits "results" once in non-incremental mode', async () => {
const client = new SearchClient(fakeSearchFn, { const client = new SearchClient(fakeSearchFn, {
pageSize: () => 2, getPageSize: () => 2,
incremental: false, incremental: false,
}); });
const onResults = sinon.stub(); const onResults = sinon.stub();
...@@ -280,7 +280,7 @@ describe('SearchClient', () => { ...@@ -280,7 +280,7 @@ describe('SearchClient', () => {
].forEach(({ sortBy, sortOrder, expectedSearchAfter }) => { ].forEach(({ sortBy, sortOrder, expectedSearchAfter }) => {
it('sets correct "search_after" query parameter depending on `sortBy` and `sortOrder`', async () => { it('sets correct "search_after" query parameter depending on `sortBy` and `sortOrder`', async () => {
const client = new SearchClient(fakeSearchFn, { const client = new SearchClient(fakeSearchFn, {
pageSize: () => 2, getPageSize: () => 2,
sortBy, sortBy,
sortOrder, sortOrder,
}); });
...@@ -297,10 +297,10 @@ describe('SearchClient', () => { ...@@ -297,10 +297,10 @@ describe('SearchClient', () => {
it('fetches pages in sizes specified by `pageSize` callback', async () => { it('fetches pages in sizes specified by `pageSize` callback', async () => {
const pageSizes = [1, 2, 10]; const pageSizes = [1, 2, 10];
const pageSizeCallback = sinon.spy(index => pageSizes[index]); const getPageSize = sinon.spy(index => pageSizes[index]);
const client = new SearchClient(fakeSearchFn, { const client = new SearchClient(fakeSearchFn, {
pageSize: pageSizeCallback, getPageSize,
}); });
client.get({ uri: 'http://example.com' }); client.get({ uri: 'http://example.com' });
...@@ -308,7 +308,7 @@ describe('SearchClient', () => { ...@@ -308,7 +308,7 @@ describe('SearchClient', () => {
const limitParams = fakeSearchFn.getCalls().map(call => call.args[0].limit); const limitParams = fakeSearchFn.getCalls().map(call => call.args[0].limit);
assert.deepEqual(limitParams, pageSizes); assert.deepEqual(limitParams, pageSizes);
const pageIndexes = pageSizeCallback.getCalls().map(call => call.args[0]); const pageIndexes = getPageSize.getCalls().map(call => call.args[0]);
assert.deepEqual(pageIndexes, [0, 1, 2]); assert.deepEqual(pageIndexes, [0, 1, 2]);
}); });
}); });
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