"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mutex = void 0;
/**
 * A simple promise-based mutual exclusion lock. Callers acquire the lock and receive a release function;
 * subsequent acquire() calls queue behind the current holder until it releases.
 */
class Mutex {
    constructor() {
        this.queue = Promise.resolve();
    }
    /**
     * Wait until the lock is available, then acquire it.
     * @returns a release function that MUST be called (typically in a finally block) to let the next waiter proceed.
     */
    acquire() {
        let release;
        const prev = this.queue;
        this.queue = new Promise((resolve) => {
            release = resolve;
        });
        return prev.then(() => release);
    }
}
exports.Mutex = Mutex;
//# sourceMappingURL=mutex.js.map