@eclipse-emfcloud/model-service-theia
Version:
Model service Theia
95 lines • 4.11 kB
JavaScript
;
// *****************************************************************************
// Copyright (C) 2023-2024 STMicroelectronics.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: MIT License which is
// available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: EPL-2.0 OR MIT
// *****************************************************************************
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelAccessorBusServer = void 0;
const inversify_1 = require("@theia/core/shared/inversify");
const model_hub_provider_1 = require("./model-hub-provider");
let ModelAccessorBusServer = class ModelAccessorBusServer {
constructor() {
this.nextSubscriptionId = 0;
this.subscriptionTokens = new Map();
}
async getModelAccessorBus(context) {
const hub = await this.modelHub(context);
return hub.getModelAccessorBus();
}
setClient(client) {
if (this.client !== client) {
// All subscriptions are invalidated
this.disposeSubscriptions();
}
this.client = client;
}
getClient() {
return this.client;
}
async subscribe(context, accessorId) {
const token = {
id: ++this.nextSubscriptionId,
accessorId: accessorId,
};
if (this.client) {
const client = this.client;
const subscription = (await this.getModelAccessorBus(context)).subscribe(accessorId, () => {
client.onAccessorChanged(token.id);
});
const doCloseSub = subscription.close.bind(subscription);
subscription.close = () => {
client.closeSubscription(token.id);
doCloseSub();
};
this.subscriptionTokens.set(token.id, { token, subscription });
}
return token;
}
closeSubscription(token) {
const subscription = this.subscriptionTokens.get(token.id)?.subscription;
this.subscriptionTokens.delete(token.id);
subscription?.close();
}
disposeSubscriptions() {
for (const next of this.subscriptionTokens.values()) {
// The subscription is bound to a particular client and
// closes itself in that client
next.subscription.close();
}
this.subscriptionTokens.clear();
}
async get(context, accessorId, ...parameters) {
return (await this.getModelAccessorBus(context)).get(accessorId, ...parameters);
}
dispose() {
this.disposeSubscriptions();
}
};
exports.ModelAccessorBusServer = ModelAccessorBusServer;
__decorate([
(0, inversify_1.inject)(model_hub_provider_1.ModelHubProvider),
__metadata("design:type", Function)
], ModelAccessorBusServer.prototype, "modelHub", void 0);
exports.ModelAccessorBusServer = ModelAccessorBusServer = __decorate([
(0, inversify_1.injectable)()
], ModelAccessorBusServer);
//# sourceMappingURL=model-accessor-bus-server.js.map