Skip to content
Open
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
16 changes: 16 additions & 0 deletions packages/core/src/__tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ describe('#chunk', () => {
chunk([about500bString, about500bString, about500bString], 2, 1)
).toEqual([[about500bString, about500bString], [about500bString]]);
});

it('returns a dense array when the first item alone exceeds max kb', () => {
const big = { blob: 'x'.repeat(2 * 1024) };
const result = chunk([big, { id: 'b' }, { id: 'c' }], 100, 1);

expect(Object.keys(result)).toHaveLength(result.length);
expect(result[0]).toEqual([big]);
result.forEach((batch) => expect(batch.length).toBeGreaterThan(0));
});

it('resets the rolling size per chunk so the count limit is reachable', () => {
const items = Array.from({ length: 12 }, (_, i) => ({ id: i }));
const result = chunk(items, 5, 500);

expect(result.map((batch) => batch.length)).toEqual([5, 5, 2]);
});
});

describe('allSettled', () => {
Expand Down
43 changes: 18 additions & 25 deletions packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,27 @@ export const chunk = <T>(array: T[], count: number, maxKB?: number): T[][] => {
return [];
}

let currentChunk = 0;
let rollingKBSize = 0;
const result: T[][] = array.reduce(
(chunks: T[][], item: T, index: number) => {
if (maxKB !== undefined) {
rollingKBSize += sizeOf(item);
// If we overflow chunk until the previous index, else keep going
if (rollingKBSize >= maxKB) {
chunks[++currentChunk] = [item];
return chunks;
}
}

if (index !== 0 && index % count === 0) {
chunks[++currentChunk] = [item];
} else {
if (chunks[currentChunk] === undefined) {
chunks[currentChunk] = [];
}
chunks[currentChunk].push(item);
}

return array.reduce((chunks: T[][], item: T) => {
const itemKBSize = maxKB === undefined ? 0 : sizeOf(item);
const currentChunk = chunks[chunks.length - 1];
const isOverMaxKB =
maxKB !== undefined && rollingKBSize + itemKBSize >= maxKB;

if (
currentChunk === undefined ||
currentChunk.length >= count ||
isOverMaxKB
) {
rollingKBSize = itemKBSize;
chunks.push([item]);
return chunks;
},
[]
);
}

return result;
rollingKBSize += itemKBSize;
currentChunk.push(item);
return chunks;
}, []);
};

export const getAllPlugins = (timeline: Timeline) => {
Expand Down
Loading