[LS]: WebView communication via JsonRPC
Enhance communications between the language server and WebViews by adding a JsonRPC transport.
example implementation:
import { WebViewTransport, WebViewMessage } from '../types';
import { isWebViewMessage } from '../utils';
import { Connection } from 'vscode-languageserver';
export class JsonRpcWebViewTransport implements WebViewTransport {
#connection: Connection;
#handler?: (message: WebViewMessage) => void = undefined;
constructor(connection: Connection) {
this.#connection = connection;
this.#connection.onNotification(`$/webview/message`, (message: unknown) => {
if (!isWebViewMessage(message)) {
return;
}
this.#handler?.(message);
});
}
postMessage(message: WebViewMessage): Promise<void> {
return this.#connection.sendNotification(`$/webview/message`, message);
}
onMessage(handler: (message: WebViewMessage) => void): void {
this.#handler = handler;
}
}