"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.withAzureSessionExecutionLock = withAzureSessionExecutionLock;
exports.clearAzureSessionExecutionLock = clearAzureSessionExecutionLock;
const sessionLockTails = new Map();
/**
 * Serialize Azure Sessions HTTP calls per session (upload, extract, invoke).
 */
async function withAzureSessionExecutionLock(sessionIdentifier, fn) {
    const previous = sessionLockTails.get(sessionIdentifier) ?? Promise.resolve();
    let release;
    const gate = new Promise((resolve) => {
        release = resolve;
    });
    const tail = previous.then(() => gate);
    sessionLockTails.set(sessionIdentifier, tail.catch(() => undefined));
    await previous;
    try {
        return await fn();
    }
    finally {
        release();
        if (sessionLockTails.get(sessionIdentifier) === tail) {
            sessionLockTails.delete(sessionIdentifier);
        }
    }
}
function clearAzureSessionExecutionLock(sessionIdentifier) {
    sessionLockTails.delete(sessionIdentifier);
}
//# sourceMappingURL=sessionExecutionLock.js.map