@eclipse-emfcloud/modelserver-theia
Version:
## Typescript Client API
122 lines • 5.86 kB
JavaScript
;
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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TheiaBackendModelServerClientV2 = void 0;
/********************************************************************************
* Copyright (c) 2022 STMicroelectronics and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0, or the MIT License which is
* available at https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: EPL-2.0 OR MIT
*******************************************************************************/
const modelserver_client_1 = require("@eclipse-emfcloud/modelserver-client");
const inversify_1 = require("@theia/core/shared/inversify");
const urijs_1 = __importDefault(require("urijs"));
const vscode_jsonrpc_1 = require("vscode-jsonrpc");
const launch_options_1 = require("./launch-options");
(0, inversify_1.decorate)((0, inversify_1.injectable)(), modelserver_client_1.ModelServerClientV2);
let TheiaBackendModelServerClientV2 = class TheiaBackendModelServerClientV2 extends modelserver_client_1.ModelServerClientV2 {
constructor(launchOptions = launch_options_1.DEFAULT_MODELSERVER_NODE_LAUNCH_OPTIONS) {
super();
this.launchOptions = launchOptions;
const baseUrl = this.getBaseUrl();
this.initialize(baseUrl);
}
dispose() {
Array.from(this.openSockets.values()).forEach(openSocket => openSocket.close());
}
getBaseUrl() {
const baseUrl = new urijs_1.default({
protocol: 'http',
hostname: this.launchOptions.hostname,
port: this.launchOptions.serverPort,
path: this.launchOptions.baseURL
});
return baseUrl;
}
setClient(client) {
this.subscriptionClient = client;
}
subscribe(modeluri, listener, options = {}) {
// Handle optional arguments finding the RPC cancellation token in their place
if (vscode_jsonrpc_1.CancellationToken.is(listener)) {
listener = undefined;
}
else if (vscode_jsonrpc_1.CancellationToken.is(options)) {
options = {};
}
if (!listener && options.listener) {
// Handle the v1 behaviour
listener = options.listener;
delete options.listener;
}
if (!listener) {
// Could still be undefined if not in the options
listener = this.subscriptionClient;
}
return super.subscribe(modeluri, listener, options);
}
selfSubscribe(modeluri, options = {}) {
// Handle optional arguments finding the RPC cancellation token in their place
if (vscode_jsonrpc_1.CancellationToken.is(options)) {
options = {};
}
this.subscribe(modeluri, undefined, options);
}
edit(modeluri, patchOrCommand, format) {
if (modelserver_client_1.ModelServerCommand.is(patchOrCommand)) {
return super.edit(modeluri, ensureCommandPrototype(patchOrCommand));
}
return super.edit(modeluri, patchOrCommand, format);
}
};
TheiaBackendModelServerClientV2 = __decorate([
(0, inversify_1.injectable)(),
__param(0, (0, inversify_1.inject)(launch_options_1.LaunchOptions)),
__param(0, (0, inversify_1.optional)()),
__metadata("design:paramtypes", [Object])
], TheiaBackendModelServerClientV2);
exports.TheiaBackendModelServerClientV2 = TheiaBackendModelServerClientV2;
/**
* The client API that we extend expects actual instance-of relationships
* in the command classes, but we lose the prototype chain when sending
* commands over the RPC wire from the frontend to the backend. So this
* function restores the correct prototype to a `command`.
*
* @param command a command that came from the Theia frontend
* @returns the `command`, now with the expected prototype chain
*/
function ensureCommandPrototype(command) {
if (modelserver_client_1.SetCommand.is(command)) {
Object.setPrototypeOf(command, modelserver_client_1.SetCommand.prototype);
}
else if (modelserver_client_1.AddCommand.is(command)) {
Object.setPrototypeOf(command, modelserver_client_1.AddCommand.prototype);
}
else if (modelserver_client_1.RemoveCommand.is(command)) {
Object.setPrototypeOf(command, modelserver_client_1.RemoveCommand.prototype);
}
else if (modelserver_client_1.CompoundCommand.is(command)) {
Object.setPrototypeOf(command, modelserver_client_1.CompoundCommand.prototype);
command.commands.forEach(ensureCommandPrototype);
}
return Object.setPrototypeOf(command, modelserver_client_1.ModelServerCommand.prototype);
}
//# sourceMappingURL=theia-model-server-client-v2.js.map