Provides a conventient way to communicate between VSCode extension and his webviews. Use RPC calls to invoke functions on the webview, receive callbacks and vice versa.
You need to have node.js installed on your machine. Also, to use this library, you need to run it inside a VSCode extension using VSCode or Theia.
An example of using this libary can be seen under the "example" folder.
- Create VSCode extension with a Webview. To create your extension go to https://code.visualstudio.com/api/extension-guides/webview.
- Install using npm
This will install the library in your node_modules folder. The extension library can be used as any node.js module (with TypeScript). The webview library needs to be imported to your html.
npm install @sap-devx/webview-rpc
- Add the following script to the root html of your Webview
<head> <script>var exports = {};</script> <script type="module" src="vscode-resource:/node_modules/@sap-devx/webview-rpc/out.browser/rpc-common.js"></script> <script type="module" src="vscode-resource:/node_modules/@sap-devx/webview-rpc/out.browser/rpc-browser.js"></script> <script type="module" src="vscode-resource:/out/media/main.js"></script> </head>
Create new instance of the Rpc in the extension side and the Webview side
- In the extension code use the following example to start new instance of the RpcExtension:
this._rpc = new RpcExtension(this._panel.webview);
- In the Webview JS code use the following example to start new instance of the RpcBrowser:
const vscode = acquireVsCodeApi(); let rpc = new RpcBrowser(window, vscode);
In order to invoke an extension method from the webbiew or webview method from the extension, you will have to register the functions that can be invoked. Here is an example on how to register the methods
function add(a,b) {
return a+b;
}
rpc.registerMethod({func: add});
To invoke a method use the invoke method on the rpc instance. You can pass a callback that will be invoked once the repsonce recieved.
rpc.invoke("add", [1,2]).then((response)=>{
console.log("1+2="+response);
});
To build for development purpose do the following:
- Run "npm install" on the repo to download the project dependencies.
npm install @sap-devx/webview-rpc
- Run "npm run compile-ext" to compile the extension library sources to javascript. The compilation results will be on the directory "out.ext".
npm run compile-ext
- Run "npm run compile-browser" to compile the browser library sources to javascript. The compilation results will be on the directory "out.browser".
npm run compile-browser
- Run the test using "npm run test".
npm run test
- Browser library is does not generate d.ts files.
- To get more help, support and information please open a github issue.
Contributing information can be found in the CONTRIBUTING.md file.
- remove the need to decalre exposed functions
Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file.