From 90aa979bb75a795fd8c33d38aee263ea655270d0 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Wed, 21 Jul 2021 10:11:53 -0700 Subject: [PATCH] notebooks: renderer messaging feedback --- src/vs/vscode.d.ts | 8 +++++--- .../api/browser/mainThreadNotebookRenderers.ts | 2 +- src/vs/workbench/api/common/extHost.protocol.ts | 2 +- .../api/common/extHostNotebookRenderers.ts | 13 +++++++------ .../browser/notebookRendererMessagingServiceImpl.ts | 7 ++++++- .../common/notebookRendererMessagingService.ts | 2 +- 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index 3068ae60c11..18c06565eb9 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -11571,11 +11571,13 @@ declare module 'vscode' { /** * Sends a message to the renderer. - * @param editor Editor to target with the message * @param message Message to send - * @returns a boolean indicating whether the message was successfully delivered + * @param editor Editor to target with the message. If not provided, the + * message is sent to all renderers. + * @returns a boolean indicating whether the message was successfully + * delivered to any renderer. */ - postMessage(editor: NotebookEditor, message: any): Thenable; + postMessage(message: any, editor?: NotebookEditor): Thenable; } /** diff --git a/src/vs/workbench/api/browser/mainThreadNotebookRenderers.ts b/src/vs/workbench/api/browser/mainThreadNotebookRenderers.ts index 6ebc512ddbe..83a3578155e 100644 --- a/src/vs/workbench/api/browser/mainThreadNotebookRenderers.ts +++ b/src/vs/workbench/api/browser/mainThreadNotebookRenderers.ts @@ -23,7 +23,7 @@ export class MainThreadNotebookRenderers extends Disposable implements MainThrea })); } - $postMessage(editorId: string, rendererId: string, message: unknown): Promise { + $postMessage(editorId: string | undefined, rendererId: string, message: unknown): Promise { return this.messaging.receiveMessage(editorId, rendererId, message); } } diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index 8814c9286bf..dcc74a8d949 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -936,7 +936,7 @@ export interface MainThreadNotebookKernelsShape extends IDisposable { } export interface MainThreadNotebookRenderersShape extends IDisposable { - $postMessage(editorId: string, rendererId: string, message: unknown): Promise; + $postMessage(editorId: string | undefined, rendererId: string, message: unknown): Promise; } export interface MainThreadInteractiveShape extends IDisposable { diff --git a/src/vs/workbench/api/common/extHostNotebookRenderers.ts b/src/vs/workbench/api/common/extHostNotebookRenderers.ts index bef5da1273a..2b18b0d9f26 100644 --- a/src/vs/workbench/api/common/extHostNotebookRenderers.ts +++ b/src/vs/workbench/api/common/extHostNotebookRenderers.ts @@ -44,14 +44,15 @@ export class ExtHostNotebookRenderers implements ExtHostNotebookRenderersShape { return this.getOrCreateEmitterFor(rendererId).event(wrappedListener, thisArg, disposables); }, - postMessage: (editorOrAlias, message) => { - const editor = notebookEditorVisible ? editorOrAlias : notebookEditorAliases.get(editorOrAlias); - const extHostEditor = editor && ExtHostNotebookEditor.apiEditorsToExtHost.get(editor); - if (!extHostEditor) { - throw new Error(`The first argument to postMessage() must be a NotebookEditor`); + postMessage: (message, editorOrAlias) => { + if (ExtHostNotebookEditor.apiEditorsToExtHost.has(message)) { // back compat for swapped args + [message, editorOrAlias] = [editorOrAlias, message]; } - return this.proxy.$postMessage(extHostEditor.id, rendererId, message); + + const editor = notebookEditorVisible ? editorOrAlias : notebookEditorAliases.get(editorOrAlias!); + const extHostEditor = editor && ExtHostNotebookEditor.apiEditorsToExtHost.get(editor); + return this.proxy.$postMessage(extHostEditor?.id, rendererId, message); }, }; diff --git a/src/vs/workbench/contrib/notebook/browser/notebookRendererMessagingServiceImpl.ts b/src/vs/workbench/contrib/notebook/browser/notebookRendererMessagingServiceImpl.ts index eb7fe07898d..e94a907f93a 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookRendererMessagingServiceImpl.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookRendererMessagingServiceImpl.ts @@ -23,7 +23,12 @@ export class NotebookRendererMessagingService implements INotebookRendererMessag constructor(@IExtensionService private readonly extensionService: IExtensionService) { } /** @inheritdoc */ - public receiveMessage(editorId: string, rendererId: string, message: unknown): Promise { + public receiveMessage(editorId: string | undefined, rendererId: string, message: unknown): Promise { + if (editorId === undefined) { + const sends = [...this.scopedMessaging.values()].map(e => e.receiveMessageHandler?.(rendererId, message)); + return Promise.all(sends).then(s => s.some(s => !!s)); + } + return this.scopedMessaging.get(editorId)?.receiveMessageHandler?.(rendererId, message) ?? Promise.resolve(false); } diff --git a/src/vs/workbench/contrib/notebook/common/notebookRendererMessagingService.ts b/src/vs/workbench/contrib/notebook/common/notebookRendererMessagingService.ts index f5701025e74..4d5f5dc04f7 100644 --- a/src/vs/workbench/contrib/notebook/common/notebookRendererMessagingService.ts +++ b/src/vs/workbench/contrib/notebook/common/notebookRendererMessagingService.ts @@ -29,7 +29,7 @@ export interface INotebookRendererMessagingService { /** * Called when the main thread gets a message for a renderer. */ - receiveMessage(editorId: string, rendererId: string, message: unknown): Promise; + receiveMessage(editorId: string | undefined, rendererId: string, message: unknown): Promise; } export interface IScopedRendererMessaging extends IDisposable {