UNPKG

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.

37 lines (36 loc) • 1.29 kB
import KeyedMutex from "./keyedMutex.js"; import MappableSemaphore from "./mappableSemaphore.js"; class CandidateWriterSemaphore { mappableSemaphore; outputPathsMutex = new KeyedMutex(1e3); constructor(threads) { this.mappableSemaphore = new MappableSemaphore(threads); } /** * Return the number of currently active candidate write operations. */ openLocks() { return this.mappableSemaphore.openLocks(); } /** * Run some {@link callback}. for every {@link candidates}. */ async map(candidates, callback) { const candidatesSorted = candidates.toSorted((a, b) => { if (a.getRomsWithFiles().length !== b.getRomsWithFiles().length) { return a.getRomsWithFiles().length - b.getRomsWithFiles().length; } return a.getName().localeCompare(b.getName()); }); return await this.mappableSemaphore.map(candidatesSorted, async (candidate) => { const outputFilePaths = candidate.getRomsWithFiles().map((romWithFiles) => romWithFiles.getOutputFile().getFilePath()); return await this.outputPathsMutex.runExclusiveForKeys(outputFilePaths, async () => { return await callback(candidate); }); }); } } export { CandidateWriterSemaphore as default }; //# sourceMappingURL=candidateWriterSemaphore.js.map