wormhole-explorer/blockchain-watcher/src/infrastructure/repositories/common/utils.ts

22 lines
549 B
TypeScript

/**
* This method divide in batches the object to send, because we have one restriction about how many object send to the endpoint
* the maximum is 10 object per request
*/
export function divideIntoBatches<T>(set: Set<T>, batchSize = 10): Set<T>[] {
const batches: Set<T>[] = [];
let batch: any[] = [];
set.forEach((item) => {
batch.push(item);
if (batch.length === batchSize) {
batches.push(new Set(batch));
batch = [];
}
});
if (batch.length > 0) {
batches.push(new Set(batch));
}
return batches;
}