@eclipse-glsp/vscode-integration
Version:
Glue code to integrate GLSP diagrams in VSCode extensions (extension part)
122 lines • 6.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebviewEndpoint = exports.StopRequest = exports.ShutdownServerNotification = exports.DisposeClientSessionRequest = exports.InitializeClientSessionRequest = exports.InitializeServerRequest = exports.StartRequest = exports.ClientStateChangeNotification = exports.ActionMessageNotification = exports.InitializeNotification = exports.WebviewReadyNotification = void 0;
/********************************************************************************
* Copyright (c) 2023-2024 EclipseSource 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
* 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: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
const protocol_1 = require("@eclipse-glsp/protocol");
const vscode = require("vscode");
const vscode_messenger_1 = require("vscode-messenger");
exports.WebviewReadyNotification = { method: 'ready' };
exports.InitializeNotification = { method: 'initialize' };
exports.ActionMessageNotification = { method: 'actionMessage' };
exports.ClientStateChangeNotification = { method: 'notifyClientStateChange' };
exports.StartRequest = { method: 'start' };
exports.InitializeServerRequest = { method: 'initializeServer' };
exports.InitializeClientSessionRequest = { method: 'initializeClientSession' };
exports.DisposeClientSessionRequest = { method: 'disposeClientSession' };
exports.ShutdownServerNotification = { method: 'shutdownServer' };
exports.StopRequest = { method: 'stop' };
/**
* Wrapper class around a {@link vscode.WebviewPanel}. Takes care
* of the communication between the webview and the host extension.
* It's main responsibility is sending {@link ActionMessages} to the webview
* and handling of action messages received from the webview.
*/
class WebviewEndpoint {
get onActionMessage() {
return this.onActionMessageEmitter.event;
}
get serverActions() {
return this._serverActions;
}
get clientActions() {
return this._clientActions;
}
constructor(options) {
var _a;
this._readyDeferred = new protocol_1.Deferred();
this.toDispose = new protocol_1.DisposableCollection();
this.onActionMessageEmitter = new vscode.EventEmitter();
this.webviewPanel = options.webviewPanel;
this.messenger = (_a = options.messenger) !== null && _a !== void 0 ? _a : new vscode_messenger_1.Messenger();
this.diagramIdentifier = options.diagramIdentifier;
this.messageParticipant = this.messenger.registerWebviewPanel(this.webviewPanel);
this.toDispose.push(this.webviewPanel.onDidDispose(() => {
this.dispose();
}), this.messenger.onNotification(exports.WebviewReadyNotification, () => {
this._readyDeferred.resolve();
}, {
sender: this.messageParticipant
}), this.onActionMessageEmitter);
}
async sendDiagramIdentifier() {
await this.ready;
if (this.diagramIdentifier) {
this.messenger.sendNotification(exports.InitializeNotification, this.messageParticipant, this.diagramIdentifier);
}
}
/**
* Hooks up a {@link GLSPClient} with the underlying webview and send the `initialize` message to the webview
* (once its ready)
* The GLSP client is called remotely from the webview context via the `vscode-messenger` RPC
* protocol.
* @param glspClient The client that should be connected
* @returns A {@link Disposable} to dispose the remote connection and all attached listeners
*/
initialize(glspClient) {
const toDispose = new protocol_1.DisposableCollection();
toDispose.push(this.messenger.onNotification(exports.ActionMessageNotification, msg => {
this.onActionMessageEmitter.fire(msg);
}, {
sender: this.messageParticipant
}), this.messenger.onRequest(exports.StartRequest, () => glspClient.start(), { sender: this.messageParticipant }), this.messenger.onRequest(exports.InitializeServerRequest, async (params) => {
const result = await glspClient.initializeServer(params);
if (!this._serverActions) {
this._serverActions = result.serverActions[this.diagramIdentifier.diagramType];
}
return result;
}, {
sender: this.messageParticipant
}), this.messenger.onRequest(exports.InitializeClientSessionRequest, params => {
if (!this._clientActions) {
this._clientActions = params.clientActionKinds;
}
glspClient.initializeClientSession(params);
}, {
sender: this.messageParticipant
}), this.messenger.onRequest(exports.DisposeClientSessionRequest, params => glspClient.disposeClientSession(params), {
sender: this.messageParticipant
}), this.messenger.onRequest(exports.ShutdownServerNotification, () => glspClient.shutdownServer(), {
sender: this.messageParticipant
}), this.messenger.onRequest(exports.StopRequest, () => glspClient.stop(), {
sender: this.messageParticipant
}), glspClient.onCurrentStateChanged(state => this.messenger.sendNotification(exports.ClientStateChangeNotification, this.messageParticipant, state)));
this.toDispose.push(toDispose);
this.sendDiagramIdentifier();
return toDispose;
}
sendMessage(actionMessage) {
this.messenger.sendNotification(exports.ActionMessageNotification, this.messageParticipant, actionMessage);
}
get ready() {
return this._readyDeferred.promise;
}
dispose() {
this.toDispose.dispose();
}
}
exports.WebviewEndpoint = WebviewEndpoint;
//# sourceMappingURL=webview-endpoint.js.map