Skip to content

Commit 1120370

Browse files
authoredFeb 26, 2021
Change the stackAllItems option to be false by default (#1645)
1 parent 31d80ef commit 1120370

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed
 

‎readme.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,8 @@ Default: [`Link` header logic](source/index.ts)
939939

940940
The function takes an object with the following properties:
941941
- `response` - The current response object.
942-
- `allItems` - An array of the emitted items.
943942
- `currentItems` - Items from the current response.
943+
- `allItems` - An empty array, unless [`pagination.stackAllItems`](#pagination.stackAllItems) is set to `true`, in which case, it's an array of the emitted items.
944944

945945
It should return an object representing Got options pointing to the next page. The options are merged automatically with the previous request, therefore the options returned `pagination.paginate(...)` must reflect changes only. If there are no more pages, `false` should be returned.
946946

@@ -958,7 +958,7 @@ const got = require('got');
958958
offset: 0
959959
},
960960
pagination: {
961-
paginate: ({response, allItems, currentItems}) => {
961+
paginate: ({response, currentItems, allItems}) => {
962962
const previousSearchParams = response.request.options.searchParams;
963963
const previousOffset = previousSearchParams.get('offset');
964964

@@ -983,14 +983,14 @@ const got = require('got');
983983
###### pagination.filter
984984

985985
Type: `Function`\
986-
Default: `({item, allItems, currentItems}) => true`
986+
Default: `({item, currentItems, allItems}) => true`
987987

988988
Checks whether the item should be emitted or not.
989989

990990
###### pagination.shouldContinue
991991

992992
Type: `Function`\
993-
Default: `({item, allItems, currentItems}) => true`
993+
Default: `({item, currentItems, allItems}) => true`
994994

995995
Checks whether the pagination should continue.
996996

@@ -1022,11 +1022,12 @@ For example, it can be helpful during development to avoid an infinite number of
10221022
###### pagination.stackAllItems
10231023

10241024
Type: `boolean`\
1025-
Default: `true`
1025+
Default: `false`
1026+
1027+
Defines how the property `allItems` in [`pagination.paginate`](#pagination.paginate), [`pagination.filter`](#pagination.filter) and [`pagination.shouldContinue`](#pagination.shouldContinue) is managed.
10261028

1027-
Defines how the parameter `allItems` in [pagination.paginate](#pagination.paginate), [pagination.filter](#pagination.filter) and [pagination.shouldContinue](#pagination.shouldContinue) is managed. When set to `false`, the parameter `allItems` is always an empty array.
1029+
When set to `false`, the `allItems` parameter is always an empty array. If `true`, it can hugely increase memory usage when working with a large dataset.
10281030

1029-
This option can be helpful to save on memory usage when working with a large dataset.
10301031

10311032
##### localAddress
10321033

‎source/as-promise/types.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ export type ResponseType = 'json' | 'buffer' | 'text';
1313

1414
export interface PaginateData<BodyType, ElementType> {
1515
response: Response<BodyType>;
16-
allItems: ElementType[];
1716
currentItems: ElementType[];
17+
allItems: ElementType[];
1818
}
1919

2020
export interface FilterData<ElementType> {
2121
item: ElementType;
22-
allItems: ElementType[];
2322
currentItems: ElementType[];
23+
allItems: ElementType[];
2424
}
2525

2626
export interface PaginationOptions<ElementType, BodyType> {
@@ -39,15 +39,15 @@ export interface PaginationOptions<ElementType, BodyType> {
3939
/**
4040
Checks whether the item should be emitted or not.
4141
42-
@default ({item, allItems, currentItems}) => true
42+
@default ({item, currentItems, allItems}) => true
4343
*/
4444
filter?: (data: FilterData<ElementType>) => boolean;
4545

4646
/**
4747
The function takes an object with the following properties:
4848
- `response` - The current response object.
49-
- `allItems` - An array of the emitted items.
5049
- `currentItems` - Items from the current response.
50+
- `allItems` - An empty array, unless when `pagination.stackAllItems` is set to `true`. In the later case, an array of the emitted items.
5151
5252
It should return an object representing Got options pointing to the next page.
5353
The options are merged automatically with the previous request, therefore the options returned `pagination.paginate(...)` must reflect changes only.
@@ -66,7 +66,7 @@ export interface PaginationOptions<ElementType, BodyType> {
6666
offset: 0
6767
},
6868
pagination: {
69-
paginate: (response, allItems, currentItems) => {
69+
paginate: ({response, currentItems}) => {
7070
const previousSearchParams = response.request.options.searchParams;
7171
const previousOffset = previousSearchParams.get('offset');
7272
@@ -98,7 +98,7 @@ export interface PaginationOptions<ElementType, BodyType> {
9898
If you want to stop **after** emitting the entry, you should use
9999
`({item, allItems}) => allItems.some(item => item.flag)` instead.
100100
101-
@default ({item, allItems, currentItems}) => true
101+
@default ({item, currentItems, allItems}) => true
102102
*/
103103
shouldContinue?: (data: FilterData<ElementType>) => boolean;
104104

@@ -126,10 +126,11 @@ export interface PaginationOptions<ElementType, BodyType> {
126126
requestLimit?: number;
127127

128128
/**
129-
Defines how the parameter `allItems` in pagination.paginate, pagination.filter and pagination.shouldContinue is managed.
130-
When set to `false`, the parameter `allItems` is always an empty array.
129+
Defines how the property `allItems` in pagination.paginate, pagination.filter and pagination.shouldContinue is managed.
130+
131+
By default, the property `allItems` is always an empty array. This setting can be helpful to save on memory usage when working with a large dataset.
131132
132-
This option can be helpful to save on memory usage when working with a large dataset.
133+
When set to `true`, the property `allItems` is an array of the emitted items.
133134
*/
134135
stackAllItems?: boolean;
135136
};

‎source/create.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ const create = (defaults: InstanceDefaults): Got => {
245245
const currentItems: T[] = [];
246246

247247
for (const item of parsed) {
248-
if (pagination.filter({item, allItems, currentItems})) {
249-
if (!pagination.shouldContinue({item, allItems, currentItems})) {
248+
if (pagination.filter({item, currentItems, allItems})) {
249+
if (!pagination.shouldContinue({item, currentItems, allItems})) {
250250
return;
251251
}
252252

@@ -266,8 +266,8 @@ const create = (defaults: InstanceDefaults): Got => {
266266

267267
const optionsToMerge = pagination.paginate({
268268
response,
269-
allItems,
270-
currentItems
269+
currentItems,
270+
allItems
271271
});
272272

273273
if (optionsToMerge === false) {

‎source/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const defaults: InstanceDefaults = {
113113
countLimit: Number.POSITIVE_INFINITY,
114114
backoff: 0,
115115
requestLimit: 10000,
116-
stackAllItems: true
116+
stackAllItems: false
117117
},
118118
parseJson: (text: string) => JSON.parse(text),
119119
stringifyJson: (object: unknown) => JSON.stringify(object),

‎test/pagination.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ test('filters elements', withServer, async (t, server, got) => {
8585

8686
const result = await got.paginate.all<number>({
8787
pagination: {
88-
filter: ({item, allItems, currentItems}) => {
88+
filter: ({item, currentItems, allItems}) => {
8989
t.true(Array.isArray(allItems));
9090
t.true(Array.isArray(currentItems));
9191

@@ -209,7 +209,7 @@ test('`shouldContinue` works', withServer, async (t, server, got) => {
209209

210210
const options = {
211211
pagination: {
212-
shouldContinue: ({allItems, currentItems}: {allItems: number[]; currentItems: number[]}) => {
212+
shouldContinue: ({currentItems, allItems}: {allItems: number[]; currentItems: number[]}) => {
213213
t.true(Array.isArray(allItems));
214214
t.true(Array.isArray(currentItems));
215215

@@ -496,11 +496,11 @@ test('`stackAllItems` set to true', withServer, async (t, server, got) => {
496496

497497
return true;
498498
},
499-
paginate: ({response, allItems, currentItems}) => {
499+
paginate: ({response, currentItems, allItems}) => {
500500
itemCount += 1;
501501
t.is(allItems.length, itemCount);
502502

503-
return got.defaults.options.pagination!.paginate({response, allItems, currentItems});
503+
return got.defaults.options.pagination!.paginate({response, currentItems, allItems});
504504
}
505505
}
506506
});
@@ -524,7 +524,7 @@ test('`stackAllItems` set to false', withServer, async (t, server, got) => {
524524

525525
return true;
526526
},
527-
paginate: ({response, allItems, currentItems}) => {
527+
paginate: ({response, currentItems, allItems}) => {
528528
t.is(allItems.length, 0);
529529

530530
return got.defaults.options.pagination!.paginate({response, allItems, currentItems});

0 commit comments

Comments
 (0)
Please sign in to comment.