igir
Version:
🕹 A zero-setup ROM collection manager that sorts, filters, extracts or archives, patches, and reports on collections of any size on any OS.
23 lines (22 loc) • 706 B
JavaScript
import async from 'async';
import { Semaphore } from 'async-mutex';
/**
* A wrapper for an `async-mutex` {@link Semaphore} that allows mapping over an array of values.
*/
export default class MappableSemaphore extends Semaphore {
threads;
constructor(threads) {
super(threads);
this.threads = threads;
}
/**
* Run some {@link callback}. for every {@link values}.
*/
async map(values, callback) {
if (values.length === 0) {
// Don't incur any semaphore overhead
return [];
}
return async.mapLimit(values, Math.floor(this.threads * 1.5), async (value) => this.runExclusive(async () => callback(value)));
}
}