Pārlūkot izejas kodu

Avoids needing completionId on params

Changes `sendCommandWithCompletion` to return a promise
main
Eric Amodio pirms 2 gadiem
vecāks
revīzija
8f437a12bd
6 mainītis faili ar 43 papildinājumiem un 30 dzēšanām
  1. +14
    -12
      src/webviews/apps/shared/appBase.ts
  2. +4
    -5
      src/webviews/apps/shared/appWithConfigBase.ts
  3. +1
    -1
      src/webviews/protocol.ts
  4. +10
    -4
      src/webviews/webviewBase.ts
  5. +9
    -4
      src/webviews/webviewViewBase.ts
  6. +5
    -4
      src/webviews/webviewWithConfigBase.ts

+ 14
- 12
src/webviews/apps/shared/appBase.ts Parādīt failu

@ -89,31 +89,33 @@ export abstract class App {
const id = nextIpcId();
this.log(`${this.appName}.sendCommand(${id}): name=${command.method}`);
return this.postMessage({ id: id, method: command.method, params: params });
this.postMessage({ id: id, method: command.method, params: params });
}
protected sendCommandWithCompletion<
protected async sendCommandWithCompletion<
TCommand extends IpcCommandType<any>,
TCompletion extends IpcNotificationType<{ completionId: string }>,
TCompletion extends IpcNotificationType<any>,
>(
command: TCommand,
params: IpcMessageParams<TCommand>,
completion: TCompletion,
callback: (params: IpcMessageParams<TCompletion>) => void,
): void {
): Promise<IpcMessageParams<TCompletion>> {
const id = nextIpcId();
this.log(`${this.appName}.sendCommandWithCompletion(${id}): name=${command.method}`);
const disposable = DOM.on(window, 'message', e => {
onIpc(completion, e.data as IpcMessage, params => {
if (params.completionId === id) {
disposable.dispose();
callback(params);
}
const promise = new Promise<IpcMessageParams<TCompletion>>(resolve => {
const disposable = DOM.on(window, 'message', (e: MessageEvent<IpcMessage>) => {
onIpc(completion, e.data, params => {
if (e.data.completionId === id) {
disposable.dispose();
resolve(params);
}
});
});
});
return this.postMessage({ id: id, method: command.method, params: params });
this.postMessage({ id: id, method: command.method, params: params, completionId: id });
return promise;
}
protected setState(state: State) {

+ 4
- 5
src/webviews/apps/shared/appWithConfigBase.ts Parādīt failu

@ -570,7 +570,7 @@ export abstract class AppWithConfig extends Ap
return;
}
this.sendCommandWithCompletion(
void this.sendCommandWithCompletion(
GenerateConfigurationPreviewCommandType,
{
key: el.dataset.settingPreview!,
@ -578,10 +578,9 @@ export abstract class AppWithConfig extends Ap
format: value,
},
DidGenerateConfigurationPreviewNotificationType,
params => {
el.innerText = params.preview ?? '';
},
);
).then(params => {
el.innerText = params.preview ?? '';
});
break;
}

+ 1
- 1
src/webviews/protocol.ts Parādīt failu

@ -4,6 +4,7 @@ export interface IpcMessage {
id: string;
method: string;
params?: unknown;
completionId?: string;
}
abstract class IpcMessageType<Params = void> {
@ -73,7 +74,6 @@ export const DidChangeConfigurationNotificationType = new IpcNotificationType
);
export interface DidGenerateConfigurationPreviewParams {
completionId: string;
preview: string;
}

+ 10
- 4
src/webviews/webviewBase.ts Parādīt failu

@ -216,7 +216,7 @@ export abstract class WebviewBase implements Disposable {
const html = content.replace(
/#{(head|body|endOfBody|placement|cspSource|cspNonce|root|webroot)}/g,
(_substring, token) => {
(_substring: string, token: string) => {
switch (token) {
case 'head':
return head ?? '';
@ -249,12 +249,18 @@ export abstract class WebviewBase implements Disposable {
return html;
}
protected notify<T extends IpcNotificationType<any>>(type: T, params: IpcMessageParams<T>): Thenable<boolean> {
return this.postMessage({ id: nextIpcId(), method: type.method, params: params });
protected notify<T extends IpcNotificationType<any>>(
type: T,
params: IpcMessageParams<T>,
completionId?: string,
): Thenable<boolean> {
return this.postMessage({ id: nextIpcId(), method: type.method, params: params, completionId: completionId });
}
@serialize()
@debug<WebviewBase<State>['postMessage']>({ args: { 0: m => `(id=${m.id}, method=${m.method})` } })
@debug<WebviewBase<State>['postMessage']>({
args: { 0: m => `(id=${m.id}, method=${m.method}${m.completionId ? `, completionId=${m.completionId}` : ''})` },
})
private postMessage(message: IpcMessage): Promise<boolean> {
if (this._panel == null) return Promise.resolve(false);

+ 9
- 4
src/webviews/webviewViewBase.ts Parādīt failu

@ -17,7 +17,6 @@ import { serialize } from '../system/decorators/serialize';
import type { TrackedUsageFeatures } from '../usageTracker';
import type { IpcMessage, IpcMessageParams, IpcNotificationType } from './protocol';
import { ExecuteCommandType, onIpc, WebviewReadyCommandType } from './protocol';
import type { WebviewBase } from './webviewBase';
const maxSmallIntegerV8 = 2 ** 30; // Max number that can be stored in V8's smis (small integers)
@ -243,12 +242,18 @@ export abstract class WebviewViewBase implements
return html;
}
protected notify<T extends IpcNotificationType<any>>(type: T, params: IpcMessageParams<T>): Thenable<boolean> {
return this.postMessage({ id: nextIpcId(), method: type.method, params: params });
protected notify<T extends IpcNotificationType<any>>(
type: T,
params: IpcMessageParams<T>,
completionId?: string,
): Thenable<boolean> {
return this.postMessage({ id: nextIpcId(), method: type.method, params: params, completionId: completionId });
}
@serialize()
@debug<WebviewBase<State>['postMessage']>({ args: { 0: m => `(id=${m.id}, method=${m.method})` } })
@debug<WebviewViewBase<State>['postMessage']>({
args: { 0: m => `(id=${m.id}, method=${m.method}${m.completionId ? `, completionId=${m.completionId}` : ''})` },
})
protected postMessage(message: IpcMessage) {
if (this._view == null) return Promise.resolve(false);

+ 5
- 4
src/webviews/webviewWithConfigBase.ts Parādīt failu

@ -172,10 +172,11 @@ export abstract class WebviewWithConfigBase extends WebviewBase {
preview = 'Invalid format';
}
await this.notify(DidGenerateConfigurationPreviewNotificationType, {
completionId: e.id,
preview: preview,
});
await this.notify(
DidGenerateConfigurationPreviewNotificationType,
{ preview: preview },
e.completionId,
);
}
}
});

Notiek ielāde…
Atcelt
Saglabāt