"use strict";
// backend/src/memory-pool/pool-lock.ts
// Per-project Promise-chain mutex for serializing pool.json writes.
Object.defineProperty(exports, "__esModule", { value: true });
exports.withPoolLock = withPoolLock;
const locks = new Map();
function withPoolLock(poolDir, fn) {
    const prev = locks.get(poolDir) ?? Promise.resolve();
    let resolve;
    let reject;
    const resultPromise = new Promise((res, rej) => { resolve = res; reject = rej; });
    const next = prev.then(() => {
        try {
            const result = fn();
            if (result instanceof Promise) {
                return result.then((v) => resolve(v), (e) => reject(e));
            }
            resolve(result);
        }
        catch (e) {
            reject(e);
        }
    });
    // Keep chain alive (swallow rejections so next caller can proceed)
    const chain = next.catch(() => { });
    locks.set(poolDir, chain);
    chain.then(() => { if (locks.get(poolDir) === chain)
        locks.delete(poolDir); });
    return resultPromise;
}
//# sourceMappingURL=pool-lock.js.map