"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmartCancelBridge = void 0;
class SmartCancelBridge {
    constructor() {
        this.controllers = new Map();
        this.terminalClaims = new Set();
    }
    create(parentSessionId) {
        this.release(parentSessionId);
        const controller = new AbortController();
        this.controllers.set(parentSessionId, controller);
        return {
            signal: controller.signal,
            throwIfAborted() {
                if (controller.signal.aborted) {
                    throw new Error('Smart analysis cancelled');
                }
            },
        };
    }
    cancel(parentSessionId) {
        const controller = this.controllers.get(parentSessionId);
        if (!controller)
            return false;
        controller.abort();
        return true;
    }
    tryClaimTerminal(parentSessionId) {
        if (this.terminalClaims.has(parentSessionId))
            return false;
        this.terminalClaims.add(parentSessionId);
        return true;
    }
    release(parentSessionId) {
        this.controllers.delete(parentSessionId);
        this.terminalClaims.delete(parentSessionId);
    }
}
exports.SmartCancelBridge = SmartCancelBridge;
//# sourceMappingURL=smartCancelBridge.js.map