Skip to content
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

Add backoff option to pagination #1182

Merged
merged 3 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,13 @@ Default: `Infinity`

The maximum amount of items that should be emitted.

###### pagination.backoff

Type: `number`\
Default: `0`

Milliseconds to wait before the next request is triggered.

###### pagination.requestLimit

Type: `number`\
Expand Down
1 change: 1 addition & 0 deletions source/as-promise/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface PaginationOptions<T, R> {
paginate?: (response: Response<R>, allItems: T[], currentItems: T[]) => Options | false;
shouldContinue?: (item: T, allItems: T[], currentItems: T[]) => boolean;
countLimit?: number;
backoff?: number;
requestLimit?: number;
stackAllItems?: boolean;
};
Expand Down
8 changes: 8 additions & 0 deletions source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ const errors = {
UploadError
};

// The `delay` package weighs 10KB (!)
const delay = async (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

const {normalizeArguments, mergeOptions} = PromisableRequest;

const getPromiseOrStream = (options: NormalizedOptions): GotReturn => options.isStream ? new Request(options.url, options) : asPromise(options);
Expand Down Expand Up @@ -214,6 +217,11 @@ const create = (defaults: InstanceDefaults): Got => {

let numberOfRequests = 0;
while (numberOfRequests < pagination.requestLimit) {
if (numberOfRequests !== 0) {
// eslint-disable-next-line no-await-in-loop
await delay(pagination.backoff);
}

// TODO: Throw when result is not an instance of Response
// eslint-disable-next-line no-await-in-loop
const result = (await got(normalizedOptions)) as Response;
Expand Down
1 change: 1 addition & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const defaults: InstanceDefaults = {
filter: () => true,
shouldContinue: () => true,
countLimit: Infinity,
backoff: 0,
requestLimit: 10000,
stackAllItems: true
},
Expand Down
28 changes: 28 additions & 0 deletions test/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {URL} from 'url';
import test from 'ava';
import delay = require('delay');
import getStream = require('get-stream');
import got, {Response} from '../source';
import withServer, {withBodyParsingServer} from './helpers/with-server';
Expand Down Expand Up @@ -449,6 +450,33 @@ test('`requestLimit` works', withServer, async (t, server, got) => {
t.deepEqual(results, [1]);
});

test('`backoff` works', withServer, async (t, server, got) => {
attachHandler(server, 2);

const backoff = 200;

const asyncIterator: AsyncIterator<number> = got.paginate<number>('', {
pagination: {
backoff
}
});

t.is((await asyncIterator.next()).value, 1);

let receivedLastOne = false;
const promise = asyncIterator.next();
(async () => {
await promise;
receivedLastOne = true;
})();

await delay(backoff / 2);
t.false(receivedLastOne);

await delay((backoff / 2) + 100);
t.true(receivedLastOne);
});

test('`stackAllItems` set to true', withServer, async (t, server, got) => {
attachHandler(server, 3);

Expand Down