@rudderstack/integrations-lib 0.2.33 → 0.2.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,15 +1,23 @@
1
1
  /**
2
- * Configuration options for batch processing operations
2
+ * Options for batch processing operations
3
3
  */
4
- export interface BatchProcessingDefaults {
4
+ export interface BatchProcessingOptions {
5
5
  /**
6
- * Default number of items to process in each batch
6
+ * Number of items to process in each batch (default: 10)
7
+ * Must be a positive integer.
7
8
  */
8
9
  batchSize?: number;
9
10
  /**
10
- * Default time threshold in milliseconds before yielding control back to the event loop
11
+ * Time threshold in milliseconds (default: 10) before yielding control back to the event loop.
12
+ * Set to 0 to yield after every batch. Must be a non-negative integer.
11
13
  */
12
14
  yieldThreshold?: number;
15
+ /**
16
+ * Whether to process items sequentially within each batch. When true (default), each item in a batch
17
+ * will be processed one at a time. When false, all items in a batch will be processed concurrently.
18
+ * Consider the implications of concurrency on your processing logic before setting this to false, e.g. race conditions, rate limits, memory pressure, etc.
19
+ */
20
+ sequentialProcessing?: boolean;
13
21
  }
14
22
  /**
15
23
  * Configure global defaults for batch processing operations
@@ -25,11 +33,14 @@ export interface BatchProcessingDefaults {
25
33
  * // Set only batch size
26
34
  * configureBatchProcessingDefaults({ batchSize: 50 });
27
35
  *
36
+ * // Enable concurrent processing within batches
37
+ * configureBatchProcessingDefaults({ sequentialProcessing: false });
38
+ *
28
39
  * // Get current configuration
29
40
  * const currentConfig = configureBatchProcessingDefaults();
30
41
  * ```
31
42
  */
32
- export declare function configureBatchProcessingDefaults(config?: BatchProcessingDefaults): BatchProcessingDefaults;
43
+ export declare function configureBatchProcessingDefaults(config?: BatchProcessingOptions): BatchProcessingOptions;
33
44
  /**
34
45
  * Maps over an array in batches to avoid blocking the event loop.
35
46
  * Processes items in chunks and yields control back to the event loop between batches
@@ -39,26 +50,20 @@ export declare function configureBatchProcessingDefaults(config?: BatchProcessin
39
50
  * @template R - The type of items in the result array
40
51
  * @param items - The array to map over
41
52
  * @param mapFn - The mapping function to apply to each item. Receives the item and its index.
42
- * @param batchSize - The number of items to process in each batch (default: @see defaultBatchSize)
43
- * @param yieldThreshold - Time threshold in milliseconds before yielding control (default: @see defaultYieldThreshold)
53
+ * @param options - Batch processing options
44
54
  * @returns A promise that resolves to the mapped array
45
55
  * @throws {Error} When batchSize is not a positive integer
46
56
  *
47
57
  * @example
48
58
  * ```typescript
49
- * // Synchronous mapping
50
- * const doubled = await mapInBatches([1, 2, 3, 4], (x) => x * 2, 2);
51
- * // Result: [2, 4, 6, 8]
52
- *
53
- * // Asynchronous mapping
54
- * const fetched = await mapInBatches(urls, async (url) => fetch(url), 3);
59
+ * // Synchronous mapping with default options (sequential processing)
60
+ * const doubled = await mapInBatches([1, 2, 3, 4], (x) => x * 2);
55
61
  *
56
- * // With index
57
- * const indexed = await mapInBatches(['a', 'b'], (item, index) => `${item}-${index}`);
58
- * // Result: ['a-0', 'b-1']
62
+ * // With concurrent processing within batches
63
+ * const doubled = await mapInBatches([1, 2, 3, 4], (x) => x * 2, { sequentialProcessing: false });
59
64
  * ```
60
65
  */
61
- export declare function mapInBatches<T, R>(items: T[], mapFn: (item: T, index: number) => R | Promise<R>, batchSize?: number, yieldThreshold?: number): Promise<R[]>;
66
+ export declare function mapInBatches<T, R>(items: T[], mapFn: (item: T, index: number) => R | Promise<R>, options?: BatchProcessingOptions): Promise<R[]>;
62
67
  /**
63
68
  * Filters an array in batches to avoid blocking the event loop.
64
69
  * Processes items in chunks and yields control back to the event loop between batches
@@ -67,29 +72,21 @@ export declare function mapInBatches<T, R>(items: T[], mapFn: (item: T, index: n
67
72
  * @template T - The type of items in the array
68
73
  * @param items - The array to filter
69
74
  * @param predicate - The predicate function to test each item. Receives the item and its index.
70
- * @param batchSize - The number of items to process in each batch (default: @see defaultBatchSize)
71
- * @param yieldThreshold - Time threshold in milliseconds before yielding control (default: @see defaultYieldThreshold)
75
+ * @param options - Batch processing options
72
76
  * @returns A promise that resolves to the filtered array
73
77
  * @throws {Error} When batchSize is not a positive integer
74
78
  *
75
79
  * @example
76
80
  * ```typescript
77
- * // Synchronous filtering
78
- * const evens = await filterInBatches([1, 2, 3, 4, 5], (x) => x % 2 === 0, 2);
81
+ * // Synchronous filtering with default options
82
+ * const evens = await filterInBatches([1, 2, 3, 4, 5], (x) => x % 2 === 0);
79
83
  * // Result: [2, 4]
80
84
  *
81
- * // Asynchronous filtering
82
- * const valid = await filterInBatches(urls, async (url) => {
83
- * const response = await fetch(url);
84
- * return response.ok;
85
- * }, 3);
86
- *
87
- * // With index
88
- * const evenIndices = await filterInBatches(['a', 'b', 'c'], (_, index) => index % 2 === 0);
89
- * // Result: ['a', 'c']
85
+ * // With custom batch size
86
+ * const evens = await filterInBatches([1, 2, 3, 4, 5], (x) => x % 2 === 0, { batchSize: 2 });
90
87
  * ```
91
88
  */
92
- export declare function filterInBatches<T>(items: T[], predicate: (item: T, index: number) => boolean | Promise<boolean>, batchSize?: number, yieldThreshold?: number): Promise<T[]>;
89
+ export declare function filterInBatches<T>(items: T[], predicate: (item: T, index: number) => boolean | Promise<boolean>, options?: BatchProcessingOptions): Promise<T[]>;
93
90
  /**
94
91
  * Groups an array by a key function in batches to avoid blocking the event loop.
95
92
  * Processes items in chunks and yields control back to the event loop between batches
@@ -99,73 +96,54 @@ export declare function filterInBatches<T>(items: T[], predicate: (item: T, inde
99
96
  * @template K - The type of the grouping key (must extend PropertyKey)
100
97
  * @param items - The array to group
101
98
  * @param keyFn - The function to extract the grouping key from each item. Receives the item and its index.
102
- * @param batchSize - The number of items to process in each batch (default: @see defaultBatchSize)
103
- * @param yieldThreshold - Time threshold in milliseconds before yielding control (default: @see defaultYieldThreshold)
99
+ * @param options - Batch processing options
104
100
  * @returns A promise that resolves to an object with grouped items
105
101
  * @throws {Error} When batchSize is not a positive integer
106
102
  *
107
103
  * @example
108
104
  * ```typescript
109
- * // Group by property
105
+ * // Group by property with default options
110
106
  * const byType = await groupByInBatches(
111
107
  * [{type: 'A', value: 1}, {type: 'B', value: 2}, {type: 'A', value: 3}],
112
- * (item) => item.type,
113
- * 2
108
+ * (item) => item.type
114
109
  * );
115
110
  * // Result: {A: [{type: 'A', value: 1}, {type: 'A', value: 3}], B: [{type: 'B', value: 2}]}
116
111
  *
117
- * // Group by computed value
118
- * const byParity = await groupByInBatches([1, 2, 3, 4], (x) => x % 2 === 0 ? 'even' : 'odd');
119
- * // Result: {odd: [1, 3], even: [2, 4]}
120
- *
121
- * // Asynchronous key function
122
- * const byCategory = await groupByInBatches(items, async (item) => {
123
- * return await getCategoryForItem(item);
124
- * });
112
+ * // With custom batch size
113
+ * const byType = await groupByInBatches(
114
+ * [{type: 'A', value: 1}, {type: 'B', value: 2}, {type: 'A', value: 3}],
115
+ * (item) => item.type,
116
+ * { batchSize: 2 }
117
+ * );
125
118
  * ```
126
119
  */
127
- export declare function groupByInBatches<T, K extends PropertyKey>(items: T[], keyFn: (item: T, index: number) => K | Promise<K>, batchSize?: number, yieldThreshold?: number): Promise<Record<K, T[]>>;
120
+ export declare function groupByInBatches<T, K extends PropertyKey>(items: T[], keyFn: (item: T, index: number) => K | Promise<K>, options?: BatchProcessingOptions): Promise<Record<K, T[]>>;
128
121
  /**
129
122
  * Reduces an array in batches to avoid blocking the event loop.
130
123
  * Processes items in chunks and yields control back to the event loop between batches
131
- * when the processing time exceeds the threshold.
124
+ * when the processing time exceeds the threshold. Sequential processing is always used for the reducer function,
125
+ * irrespective of the `sequentialProcessing` option.
132
126
  *
133
127
  * @template T - The type of items in the array
134
128
  * @template R - The type of the accumulator/result
135
129
  * @param items - The array to reduce
136
130
  * @param reducer - The reducer function. Receives the accumulator, current item, and index.
137
131
  * @param initialValue - The initial value for the accumulator
138
- * @param batchSize - The number of items to process in each batch (default: @see defaultBatchSize)
139
- * @param yieldThreshold - Time threshold in milliseconds before yielding control (default: @see defaultYieldThreshold)
132
+ * @param options - Batch processing options
140
133
  * @returns A promise that resolves to the reduced value
141
134
  * @throws {Error} When batchSize is not a positive integer
142
135
  *
143
136
  * @example
144
137
  * ```typescript
145
- * // Sum numbers
146
- * const sum = await reduceInBatches([1, 2, 3, 4], (acc, x) => acc + x, 0, 2);
138
+ * // Sum numbers with default options
139
+ * const sum = await reduceInBatches([1, 2, 3, 4], (acc, x) => acc + x, 0);
147
140
  * // Result: 10
148
141
  *
149
- * // Concatenate strings
150
- * const joined = await reduceInBatches(['a', 'b', 'c'], (acc, x) => acc + x, '');
151
- * // Result: 'abc'
152
- *
153
- * // Build object with index
154
- * const indexed = await reduceInBatches(
155
- * ['x', 'y'],
156
- * (acc, item, index) => ({ ...acc, [index]: item }),
157
- * {}
158
- * );
159
- * // Result: {0: 'x', 1: 'y'}
160
- *
161
- * // Asynchronous reducer
162
- * const processed = await reduceInBatches(urls, async (acc, url) => {
163
- * const data = await fetch(url).then(r => r.json());
164
- * return [...acc, data];
165
- * }, []);
142
+ * // With custom batch size
143
+ * const sum = await reduceInBatches([1, 2, 3, 4], (acc, x) => acc + x, 0, { batchSize: 2 });
166
144
  * ```
167
145
  */
168
- export declare function reduceInBatches<T, R>(items: T[], reducer: (acc: R, item: T, index: number) => R | Promise<R>, initialValue: R, batchSize?: number, yieldThreshold?: number): Promise<R>;
146
+ export declare function reduceInBatches<T, R>(items: T[], reducer: (acc: R, item: T, index: number) => R | Promise<R>, initialValue: R, options?: BatchProcessingOptions): Promise<R>;
169
147
  /**
170
148
  * FlatMaps an array in batches to avoid blocking the event loop.
171
149
  * Processes items in chunks, flattens the results, and yields control back to the event loop between batches
@@ -175,31 +153,45 @@ export declare function reduceInBatches<T, R>(items: T[], reducer: (acc: R, item
175
153
  * @template R - The type of items in the flattened result array
176
154
  * @param items - The array to flatMap over
177
155
  * @param mapFn - The mapping function that returns an array for each item. Receives the item and its index.
178
- * @param batchSize - The number of items to process in each batch (default: @see defaultBatchSize)
179
- * @param yieldThreshold - Time threshold in milliseconds before yielding control (default: @see defaultYieldThreshold)
156
+ * @param options - Batch processing options
180
157
  * @returns A promise that resolves to the flattened mapped array
181
158
  * @throws {Error} When batchSize is not a positive integer
182
159
  *
183
160
  * @example
184
161
  * ```typescript
185
- * // Duplicate each item
186
- * const duplicated = await flatMapInBatches([1, 2, 3], (x) => [x, x], 2);
162
+ * // Duplicate each item with default options
163
+ * const duplicated = await flatMapInBatches([1, 2, 3], (x) => [x, x]);
187
164
  * // Result: [1, 1, 2, 2, 3, 3]
188
165
  *
189
- * // Variable length results
190
- * const repeated = await flatMapInBatches([1, 2, 3], (x) => Array(x).fill(x));
191
- * // Result: [1, 2, 2, 3, 3, 3]
166
+ * // With custom batch size
167
+ * const duplicated = await flatMapInBatches([1, 2, 3], (x) => [x, x], { batchSize: 2 });
168
+ * ```
169
+ */
170
+ export declare function flatMapInBatches<T, R>(items: T[], mapFn: (item: T, index: number) => R[] | Promise<R[]>, options?: BatchProcessingOptions): Promise<R[]>;
171
+ /**
172
+ * forEach over an array in batches to avoid blocking the event loop.
173
+ * Processes items in chunks and yields control back to the event loop between batches
174
+ * when the processing time exceeds the threshold.
192
175
  *
193
- * // Split strings into characters
194
- * const chars = await flatMapInBatches(['ab', 'cd'], (str) => str.split(''));
195
- * // Result: ['a', 'b', 'c', 'd']
176
+ * @template T - The type of items in the input array
177
+ * @param items - The array to iterate over
178
+ * @param fn - The function to apply to each item. Receives the item and its index. Can be async.
179
+ * @param options - Batch processing options
180
+ * @returns A promise that resolves when all items have been processed
181
+ * @throws {Error} When batchSize is not a positive integer
196
182
  *
197
- * // Asynchronous mapping
198
- * const expanded = await flatMapInBatches(categories, async (category) => {
199
- * const items = await fetchItemsForCategory(category);
200
- * return items;
183
+ * @example
184
+ * ```typescript
185
+ * // Process items in batches with default options
186
+ * await forEachInBatches([1, 2, 3, 4], async (x) => {
187
+ * await doSomething(x);
201
188
  * });
189
+ *
190
+ * // With custom batch size
191
+ * await forEachInBatches([1, 2, 3, 4], async (x) => {
192
+ * await doSomething(x);
193
+ * }, { batchSize: 2 });
202
194
  * ```
203
195
  */
204
- export declare function flatMapInBatches<T, R>(items: T[], mapFn: (item: T, index: number) => R[] | Promise<R[]>, batchSize?: number, yieldThreshold?: number): Promise<R[]>;
196
+ export declare function forEachInBatches<T>(items: T[], fn: (item: T, index: number) => void | Promise<void>, options?: BatchProcessingOptions): Promise<void>;
205
197
  //# sourceMappingURL=batch-processing.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"batch-processing.d.ts","sourceRoot":"","sources":["../../src/utils/batch-processing.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAMD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,gCAAgC,CAC9C,MAAM,CAAC,EAAE,uBAAuB,GAC/B,uBAAuB,CAqBzB;AA0BD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,CAAC,EAC/B,KAAK,EAAE,CAAC,EAAE,EACV,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,EACjD,SAAS,SAAmB,EAC5B,cAAc,SAAwB,GACrC,OAAO,CAAC,CAAC,EAAE,CAAC,CAmBd;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAC/B,KAAK,EAAE,CAAC,EAAE,EACV,SAAS,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,EACjE,SAAS,SAAmB,EAC5B,cAAc,SAAwB,GACrC,OAAO,CAAC,CAAC,EAAE,CAAC,CAqBd;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,gBAAg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