@shopify/cli-kit
Version:
A set of utilities, interfaces, and models that are common across all the platform features
40 lines • 1.25 kB
JavaScript
/**
* Handles serial processing of items with automatic batching
* Only one processing operation runs at a time, with new items
* automatically queued for the next batch.
*/
export class SerialBatchProcessor {
constructor(processBatch) {
this.processBatch = processBatch;
this.queue = [];
this.processingPromise = undefined;
}
enqueue(item) {
this.queue.push(item);
if (!this.processingPromise) {
this.processingPromise = this.startProcessing();
}
}
async waitForCompletion() {
if (this.processingPromise) {
return this.processingPromise;
}
}
async startProcessing() {
try {
while (this.queue.length > 0) {
// Get current batch and clear queue
const batch = [...this.queue];
this.queue = [];
// Process the current batch
// eslint-disable-next-line no-await-in-loop
await this.processBatch(batch);
}
}
finally {
// Always make sure we reset the processing state
this.processingPromise = undefined;
}
}
}
//# sourceMappingURL=serial-batch-processor.js.map