diff --git a/README.md b/README.md index 7d8c297..157a7c1 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,25 @@ connected producers in the same workspace. - **COMPAS: Clear Live Viewer** — clear that workspace and its replay history. - **COMPAS: Restart Live Server** — apply host or port changes immediately. +### Extension API + +Other extensions can discover the live server without asking the user to pin a +port. `compasThreejs.getLiveEndpoint` starts the server if it is not already +running and resolves to its WebSocket endpoint, or to `undefined` if it could +not be started: + +```ts +const endpoint = await vscode.commands.executeCommand( + "compasThreejs.getLiveEndpoint", +); +// "ws://127.0.0.1:54321/ws" +``` + +This exists because the port is normally ephemeral and otherwise appears only in +the status bar and the clipboard snippet, neither of which another extension can +read. The workspace to publish into is readable from +`compasThreejs.live.workspace`. + ### Settings - `compasThreejs.live.enabled` — start the server after VS Code starts. diff --git a/package.json b/package.json index 22910f8..e39553b 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "onCommand:compasThreejs.openLiveViewer", "onCommand:compasThreejs.copyConnectionSnippet", "onCommand:compasThreejs.restartLiveServer", - "onCommand:compasThreejs.clearLiveViewer" + "onCommand:compasThreejs.clearLiveViewer", + "onCommand:compasThreejs.getLiveEndpoint" ], "main": "./dist/extension.js", "contributes": { @@ -59,6 +60,10 @@ "command": "compasThreejs.clearLiveViewer", "title": "COMPAS: Clear Live Viewer", "icon": "$(clear-all)" + }, + { + "command": "compasThreejs.getLiveEndpoint", + "title": "COMPAS: Get Live Server Endpoint" } ], "customEditors": [ @@ -119,6 +124,10 @@ }, { "command": "compasThreejs.clearLiveViewer" + }, + { + "command": "compasThreejs.getLiveEndpoint", + "when": "false" } ] } diff --git a/src/extension.ts b/src/extension.ts index 3c8dd3b..4e4f1d8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -74,6 +74,20 @@ export function activate(context: vscode.ExtensionContext): void { } }, ), + // Public API for other extensions: the live server's WebSocket endpoint, + // starting the server if it is not already running. The port is normally + // ephemeral and is otherwise only visible in the status bar text and the + // clipboard snippet, neither of which another extension can read. + // Returns undefined if the server could not be started. + vscode.commands.registerCommand( + "compasThreejs.getLiveEndpoint", + async (): Promise => { + if (!(await ensureServer(false))) { + return undefined; + } + return server.endpoint; + }, + ), vscode.commands.registerCommand( "compasThreejs.copyConnectionSnippet", async () => {