"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainThreadConnection = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const ide_connection_1 = require("@opensumi/ide-connection");
const ide_core_browser_1 = require("@opensumi/ide-core-browser");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const vscode_1 = require("../../../common/vscode");
let MainThreadConnection = class MainThreadConnection {
    constructor(rpcProtocol) {
        this.rpcProtocol = rpcProtocol;
        this.connections = new Map();
        this.connectionsReady = new Map();
        this.toDispose = new ide_core_common_1.DisposableCollection();
        this.proxy = this.rpcProtocol.getProxy(vscode_1.ExtHostAPIIdentifier.ExtHostConnection);
    }
    dispose() {
        this.connections.forEach((connection) => {
            connection.dispose();
        });
        this.connections.clear();
        this.toDispose.dispose();
    }
    /**
     * 通过ID获取Connection并发送对应消息
     * @param id
     * @param message
     */
    async $sendMessage(id, message) {
        const ready = this.connectionsReady.get(id);
        if (ready) {
            await ready.promise;
        }
        if (this.connections.has(id)) {
            this.connections.get(id).readMessage(message);
        }
        else {
            this.logger.warn(`Do not found connection ${id}`);
        }
    }
    /**
     * 创建新的Connection
     * 当链接ID存在时，返回已有Connection
     * @param id
     */
    async $createConnection(id) {
        this.logger.log(`create connection ${id}`);
        await this.doEnsureConnection(id);
    }
    /**
     * 根据ID删除Connection
     * @param id
     */
    async $deleteConnection(id) {
        this.logger.log(`delete connection ${id}`);
        this.connections.delete(id);
    }
    /**
     * 返回已存在的Connection或创建新的Connection
     * @param id
     */
    async ensureConnection(id) {
        const connection = await this.doEnsureConnection(id);
        await this.proxy.$createConnection(id);
        return connection;
    }
    /**
     * 执行获取/新建Connection操作
     * @param id
     */
    async doEnsureConnection(id) {
        let connection = this.connections.get(id);
        if (!connection) {
            const ready = new ide_core_browser_1.Deferred();
            this.connectionsReady.set(id, ready);
            connection = await this.doCreateConnection(id);
            ready.resolve();
            this.connections.set(id, connection);
            this.connectionsReady.delete(id);
        }
        return connection;
    }
    async doCreateConnection(id) {
        const connection = new vscode_1.ExtensionConnection(id, this.proxy, () => {
            this.connections.delete(id);
            this.proxy.$deleteConnection(id);
        });
        this.toDispose.push(ide_core_common_1.Disposable.create(() => connection.fireClose()));
        return connection;
    }
};
exports.MainThreadConnection = MainThreadConnection;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_common_1.ILogger),
    tslib_1.__metadata("design:type", Object)
], MainThreadConnection.prototype, "logger", void 0);
exports.MainThreadConnection = MainThreadConnection = tslib_1.__decorate([
    (0, di_1.Injectable)({ multiple: true }),
    tslib_1.__param(0, (0, di_1.Optional)(ide_connection_1.IRPCProtocol)),
    tslib_1.__metadata("design:paramtypes", [Object])
], MainThreadConnection);
//# sourceMappingURL=main.thread.connection.js.map