Skip to content

Commit

Permalink
notebooks: renderer messaging feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 committed Jul 21, 2021
1 parent 889fc9e commit 90aa979
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src/vs/vscode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>;
postMessage(message: any, editor?: NotebookEditor): Thenable<boolean>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class MainThreadNotebookRenderers extends Disposable implements MainThrea
}));
}

$postMessage(editorId: string, rendererId: string, message: unknown): Promise<boolean> {
$postMessage(editorId: string | undefined, rendererId: string, message: unknown): Promise<boolean> {
return this.messaging.receiveMessage(editorId, rendererId, message);
}
}
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ export interface MainThreadNotebookKernelsShape extends IDisposable {
}

export interface MainThreadNotebookRenderersShape extends IDisposable {
$postMessage(editorId: string, rendererId: string, message: unknown): Promise<boolean>;
$postMessage(editorId: string | undefined, rendererId: string, message: unknown): Promise<boolean>;
}

export interface MainThreadInteractiveShape extends IDisposable {
Expand Down
13 changes: 7 additions & 6 deletions src/vs/workbench/api/common/extHostNotebookRenderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
public receiveMessage(editorId: string | undefined, rendererId: string, message: unknown): Promise<boolean> {
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>;
receiveMessage(editorId: string | undefined, rendererId: string, message: unknown): Promise<boolean>;
}

export interface IScopedRendererMessaging extends IDisposable {
Expand Down

0 comments on commit 90aa979

Please sign in to comment.