@eclipse-emfcloud/modelserver-client
Version:
Typescript rest client to interact with an EMF.cloud modelserver
122 lines • 6.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationSubscriptionListenerV2 = exports.NotificationSubscriptionListener = void 0;
/********************************************************************************
* Copyright (c) 2021-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 fast_json_patch_1 = require("fast-json-patch");
const _1 = require(".");
const model_server_message_1 = require("./model-server-message");
const command_model_1 = require("./model/command-model");
const diagnostic_1 = require("./model/diagnostic");
/**
* Default implementation of {@link SubscriptionListener} that maps received websocket events to the
* corresponding {@link ModelServerNotification} and delegates them to the
* {@link ModelServerNotificationListener} implementation passed via constructor.
*/
class NotificationSubscriptionListener {
constructor(notificationListener = {}) {
this.notificationListener = notificationListener;
}
onOpen(modeluri, _event) {
var _a, _b;
(_b = (_a = this.notificationListener).onOpen) === null || _b === void 0 ? void 0 : _b.call(_a, { modeluri, type: model_server_message_1.MessageType.open });
}
onClose(modeluri, event) {
var _a, _b;
(_b = (_a = this.notificationListener).onClose) === null || _b === void 0 ? void 0 : _b.call(_a, { modeluri, code: event.code, reason: event.reason, type: model_server_message_1.MessageType.close });
}
onError(modeluri, event) {
var _a, _b;
(_b = (_a = this.notificationListener).onError) === null || _b === void 0 ? void 0 : _b.call(_a, { modeluri, error: event.error, type: model_server_message_1.MessageType.error });
}
onMessage(modeluri, event) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
const message = JSON.parse(event.data.toString());
if (model_server_message_1.ModelServerMessage.is(message)) {
const type = model_server_message_1.MessageType.asMessageType(message.type);
switch (type) {
case model_server_message_1.MessageType.dirtyState: {
(_b = (_a = this.notificationListener).onDirtyStateChanged) === null || _b === void 0 ? void 0 : _b.call(_a, { modeluri, isDirty: model_server_message_1.MessageDataMapper.asBoolean(message), type });
break;
}
case model_server_message_1.MessageType.keepAlive:
case model_server_message_1.MessageType.success: {
(_d = (_c = this.notificationListener).onSuccess) === null || _d === void 0 ? void 0 : _d.call(_c, { modeluri, type });
break;
}
case model_server_message_1.MessageType.error: {
(_f = (_e = this.notificationListener).onError) === null || _f === void 0 ? void 0 : _f.call(_e, { modeluri, error: model_server_message_1.MessageDataMapper.asString(message), type });
break;
}
case model_server_message_1.MessageType.incrementalUpdate: {
(_h = (_g = this.notificationListener).onIncrementalUpdate) === null || _h === void 0 ? void 0 : _h.call(_g, {
modeluri,
result: model_server_message_1.MessageDataMapper.as(message, command_model_1.CommandExecutionResult.is),
type
});
break;
}
case model_server_message_1.MessageType.fullUpdate: {
(_k = (_j = this.notificationListener).onFullUpdate) === null || _k === void 0 ? void 0 : _k.call(_j, { modeluri, model: model_server_message_1.MessageDataMapper.asObject(message), type });
break;
}
case model_server_message_1.MessageType.validationResult: {
(_m = (_l = this.notificationListener).onValidation) === null || _m === void 0 ? void 0 : _m.call(_l, { modeluri, diagnostic: model_server_message_1.MessageDataMapper.as(message, diagnostic_1.Diagnostic.is), type });
break;
}
default: {
(_p = (_o = this.notificationListener).onUnknown) === null || _p === void 0 ? void 0 : _p.call(_o, Object.assign(Object.assign({}, message), { modeluri }));
}
}
}
}
}
exports.NotificationSubscriptionListener = NotificationSubscriptionListener;
/**
* Default implementation of {@link SubscriptionListener} that maps received websocket events to the
* corresponding {@link ModelServerNotification} and delegates them to the
* {@link ModelServerNotificationListenerV2} implementation passed via constructor.
*
* This class supports the V2 Client API.
*/
class NotificationSubscriptionListenerV2 extends NotificationSubscriptionListener {
constructor(notificationListener = {}) {
super(notificationListener);
this.notificationListener = notificationListener;
}
onMessage(modeluri, event) {
var _a, _b;
const message = JSON.parse(event.data.toString());
if (model_server_message_1.ModelServerMessage.is(message)) {
const type = model_server_message_1.MessageType.asMessageType(message.type);
switch (type) {
case model_server_message_1.MessageType.incrementalUpdate: {
const patch = model_server_message_1.MessageDataMapper.as(message, _1.Operations.isPatch);
(_b = (_a = this.notificationListener).onIncrementalUpdateV2) === null || _b === void 0 ? void 0 : _b.call(_a, {
type: message.type,
modeluri,
patch,
patchModel: (model, copy) => {
const modelToPatch = copy ? (0, fast_json_patch_1.deepClone)(model) : model;
return (0, fast_json_patch_1.applyPatch)(modelToPatch, patch).newDocument;
}
});
break;
}
default: {
super.onMessage(modeluri, event);
}
}
}
}
}
exports.NotificationSubscriptionListenerV2 = NotificationSubscriptionListenerV2;
//# sourceMappingURL=subscription-listener.js.map