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.
49 lines (48 loc) • 1.28 kB
JavaScript
import { Semaphore } from "async-mutex";
class MappableSemaphore extends Semaphore {
threads;
constructor(threads) {
super(threads);
this.threads = threads;
}
/**
* Return the number of currently held semaphore slots.
*/
openLocks() {
return this.threads - Math.max(this.getValue(), 0);
}
/**
* Run some {@link callback}. for every {@link values}.
*/
async map(values, callback) {
if (values.length === 0) {
return [];
}
let firstError;
const results = await Promise.allSettled(
values.map(
async (value) => await this.runExclusive(async () => {
if (firstError !== void 0) {
throw firstError;
}
try {
return await callback(value);
} catch (error) {
const wrappedError = error instanceof Error ? error : new Error(String(error));
firstError ??= wrappedError;
this.cancel();
throw wrappedError;
}
})
)
);
if (firstError !== void 0) {
throw firstError;
}
return results.filter((result) => result.status === "fulfilled").map((result) => result.value);
}
}
export {
MappableSemaphore as default
};
//# sourceMappingURL=mappableSemaphore.js.map