Skip to content

Commit

Permalink
return chunkedDynamic.fn index
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Sep 11, 2024
1 parent 71e0ea7 commit bf4d071
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 1.5.1

### Patch Changes

- return chunkedDynamic.fn index

## 1.5.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deverything",
"version": "1.5.0",
"version": "1.5.1",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
6 changes: 4 additions & 2 deletions src/helpers/chunkedDynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { sleep } from "./sleep";
export const chunkedDynamic = async <T>(
array: T[],
initialChunkSize: number,
fn: (chunk: T[], currentChunkSize: number) => Promise<any>,
fn: (chunk: T[], currentChunkIndex: number) => Promise<any>,
{
sleepTime,
maxChunkDuration,
Expand All @@ -26,13 +26,14 @@ export const chunkedDynamic = async <T>(
const chunkResults: any[] = [];

let currentIndex = 0;
let chunkIndex = 0;
let currentChunkSize = initialChunkSize;
const step = chunkSizeStep || 1;

while (currentIndex < array.length) {
const start = performance.now();
const chunk = array.slice(currentIndex, currentIndex + currentChunkSize);
const chunkResult = await fn(chunk, currentChunkSize);
const chunkResult = await fn(chunk, chunkIndex);
chunkResults.push(chunkResult);
if (sleepTime) await sleep(sleepTime);
if (maxChunkDuration) {
Expand All @@ -51,6 +52,7 @@ export const chunkedDynamic = async <T>(
}

currentIndex += currentChunkSize;
chunkIndex += 1;
}

return chunkResults;
Expand Down

0 comments on commit bf4d071

Please sign in to comment.