UNPKG

@eclipse-emfcloud/model-service-theia

Version:
135 lines 6.18 kB
"use strict"; // ***************************************************************************** // 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.ModelHubServer = void 0; const inversify_1 = require("@theia/core/shared/inversify"); const lodash_1 = require("lodash"); const model_hub_tracker_1 = require("../common/model-hub-tracker"); const model_hub_provider_1 = require("./model-hub-provider"); let ModelHubServer = class ModelHubServer { constructor() { this.subscriptionTokens = new Map(); this.nextSubscriptionId = 0; } dispose() { this.disposeSubscriptions(); } 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(); this.clientTrackingSub?.close(); this.clientTrackingSub = undefined; } setClient(client) { if (this.client !== client) { // All subscriptions and hub tracking are invalidated this.disposeSubscriptions(); if (client !== undefined) { // Create the tracking subscription now so that we will find out // about all model hubs already in existence and forward those this.clientTrackingSub = this.modelHubTracker.trackModelHubs(); this.clientTrackingSub.onModelHubCreated = (context) => client.onModelHubCreated(context); this.clientTrackingSub.onModelHubDestroyed = (context) => client.onModelHubDestroyed(context); } } this.client = client; } getClient() { return this.client; } getModel(context, modelId) { return this.modelHub(context).then((hub) => hub.getModel(modelId)); } async subscribe(context, ...modelIds) { const token = { id: ++this.nextSubscriptionId, modelIds, }; const hub = await this.modelHub(context); const subscription = hub.subscribe(...modelIds); if (this.client) { const client = this.client; const adapt = (callback) => { return (0, lodash_1.bind)((0, lodash_1.rearg)(callback, 0, 1, 3), client, token.id); }; subscription.onModelChanged = adapt(client.onModelChanged); subscription.onModelDirtyState = adapt(client.onModelDirtyState); subscription.onModelValidated = adapt(client.onModelValidated); subscription.onModelLoaded = adapt(client.onModelLoaded); subscription.onModelUnloaded = adapt(client.onModelUnloaded); subscription.onModelHubDisposed = adapt(client.onModelHubDisposed); const doCloseSub = subscription.close.bind(subscription); subscription.close = () => { client.closeSubscription(token.id); doCloseSub(); }; } this.subscriptionTokens.set(token.id, { token, subscription }); return token; } async closeSubscription(token) { const subscription = this.subscriptionTokens.get(token.id)?.subscription; this.subscriptionTokens.delete(token.id); subscription?.close(); } validateModels(context, ...modelIds) { return this.modelHub(context).then((hub) => hub.validateModels(...modelIds)); } getValidationState(context, ...modelIds) { return this.modelHub(context).then((hub) => hub.getValidationState(...modelIds)); } save(context, ...commandStackIds) { return this.modelHub(context).then((hub) => hub.save(...commandStackIds)); } isDirty(context, commandStackId) { return this.modelHub(context).then((hub) => hub.isDirty(commandStackId)); } undo(context, commandStackId) { return this.modelHub(context).then((hub) => hub.undo(commandStackId)); } redo(context, commandStackId) { return this.modelHub(context).then((hub) => hub.redo(commandStackId)); } flush(context, commandStackId) { return this.modelHub(context).then((hub) => hub.flush(commandStackId)); } }; exports.ModelHubServer = ModelHubServer; __decorate([ (0, inversify_1.inject)(model_hub_provider_1.ModelHubProvider), __metadata("design:type", Function) ], ModelHubServer.prototype, "modelHub", void 0); __decorate([ (0, inversify_1.inject)(model_hub_tracker_1.ModelHubTracker), __metadata("design:type", Object) ], ModelHubServer.prototype, "modelHubTracker", void 0); exports.ModelHubServer = ModelHubServer = __decorate([ (0, inversify_1.injectable)() ], ModelHubServer); //# sourceMappingURL=model-hub-server.js.map