clean up methods from same caller when subscribing (#18264)

This commit is contained in:
Hai Cao 2024-10-18 08:52:16 -07:00 коммит произвёл GitHub
Родитель 2a94630b34
Коммит cb70e75d16
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 29 добавлений и 21 удалений

Просмотреть файл

@ -22,7 +22,7 @@ export class WebviewRpc<Reducers> {
};
} = {};
private _methodSubscriptions: {
[method: string]: ((params: unknown) => void)[];
[method: string]: Record<string, (params: unknown) => void>;
} = {};
private static _instance: WebviewRpc<any>;
public static getInstance<Reducers>(
@ -51,8 +51,8 @@ export class WebviewRpc<Reducers> {
if (message.type === "notification") {
const { method, params } = message;
if (this._methodSubscriptions[method]) {
this._methodSubscriptions[method].forEach((callback) =>
callback(params),
Object.values(this._methodSubscriptions[method]).forEach(
(cb) => cb(params),
);
}
}
@ -86,11 +86,15 @@ export class WebviewRpc<Reducers> {
void this.call("action", { type: method, payload });
}
public subscribe(method: string, callback: (params: unknown) => void) {
public subscribe(
callerId: string,
method: string,
callback: (params: unknown) => void,
) {
if (!this._methodSubscriptions[method]) {
this._methodSubscriptions[method] = [];
this._methodSubscriptions[method] = {};
}
this._methodSubscriptions[method].push(callback);
this._methodSubscriptions[method][callerId] = callback;
}
public sendActionEvent(event: WebviewTelemetryActionEvent) {

Просмотреть файл

@ -121,22 +121,26 @@ export function VscodeWebviewProvider<State, Reducers>({
void getLocalization();
}, []);
extensionRpc.subscribe("onDidChangeTheme", (params) => {
const kind = params as ColorThemeKind;
switch (kind) {
case ColorThemeKind.Dark:
setTheme(webDarkTheme);
break;
case ColorThemeKind.HighContrast:
setTheme(teamsHighContrastTheme);
break;
default:
setTheme(webLightTheme);
break;
}
});
extensionRpc.subscribe(
"vscodeWebviewProvider",
"onDidChangeTheme",
(params) => {
const kind = params as ColorThemeKind;
switch (kind) {
case ColorThemeKind.Dark:
setTheme(webDarkTheme);
break;
case ColorThemeKind.HighContrast:
setTheme(teamsHighContrastTheme);
break;
default:
setTheme(webLightTheme);
break;
}
},
);
extensionRpc.subscribe("updateState", (params) => {
extensionRpc.subscribe("vscodeWebviewProvider", "updateState", (params) => {
setState(params as State);
});