TypeFox / vscode-messenger

RPC messaging library for the VS Code extension platform
MIT License
33 stars 3 forks source link

Unexpected behavior for `WebviewMessageParticipant` interface #5

Closed msujew closed 2 years ago

msujew commented 2 years ago

Currently, using the WebviewMessageParticipant instead leads into a non-obvious pitfall:

https://github.com/TypeFox/vscode-messenger/blob/f8746a5717931a900309729d356dc239194354a5/packages/vscode-messenger-common/src/messages.ts#L26-L32

As both webviewId and webviewType are optional, I believed them both to be truly optional. However, at least one of them is needed. This fact is not reflected in the type definition. Instead, the type should probably look something like this:

export type WebviewMessageParticipant = WebviewIdMessageParticipant | WebviewTypeMessageParticipant;

export interface WebviewIdMessageParticipant {
    type: 'webview'
    /** Identifier of a specific webview instance. */
    webviewId: string
}

export interface WebviewTypeMessageParticipant {
    type: 'webview'
    /** Webview panel type or webview view type. */
    webviewType: string
}
dhuebner commented 2 years ago

@msujew Yes, you are right it also what the docu comment says:

A webview must be identified either with an ID (webviewId) or a type (webviewType).

I like your proposal!

dhuebner commented 2 years ago

Changed as proposed. Also added isWebviewIdMessageParticipant to check the WebviewMessageParticipant type Thanks @msujew !