@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
42 lines • 1.46 kB
JavaScript
/**
* @module Collection
*/
import {} from "../../../../collection/contracts/_module-exports.js";
import {} from "../../../../utilities/_module-exports.js";
/**
* @internal
*/
export class AsyncSplitIterable {
collection;
chunkAmount;
makeCollection;
constructor(collection, chunkAmount, makeCollection) {
this.collection = collection;
this.chunkAmount = chunkAmount;
this.makeCollection = makeCollection;
}
async *[Symbol.asyncIterator]() {
const size = await this.collection.size(), minChunkSize = Math.floor(size / this.chunkAmount), restSize = size % this.chunkAmount, chunkSizes = Array.from({
length: this.chunkAmount,
}).fill(minChunkSize);
for (let i = 1; i <= restSize; i++) {
const chunkIndex = (i - 1) % this.chunkAmount;
if (chunkSizes[chunkIndex]) {
chunkSizes[chunkIndex] = chunkSizes[chunkIndex] + 1;
}
}
const iterator = this.collection.toIterator();
const array = [];
for (const chunkSize of chunkSizes) {
for (let i = 0; i < chunkSize; i++) {
const item = await iterator.next();
if (item.value !== undefined) {
array.push(item.value);
}
}
yield this.makeCollection(array);
array.length = 0;
}
}
}
//# sourceMappingURL=async-split-iterable.js.map