瀏覽代碼

Fixes serialization issue w/ commit details

Ensures updateRichState uses the correct current context
main
Eric Amodio 2 年之前
父節點
當前提交
53b0c416d9
共有 4 個檔案被更改,包括 80 行新增40 行删除
  1. +26
    -0
      src/git/models/pullRequest.ts
  2. +14
    -4
      src/system/serialize.ts
  3. +21
    -22
      src/webviews/apps/commitDetails/commitDetails.ts
  4. +19
    -14
      src/webviews/commitDetails/commitDetailsWebviewView.ts

+ 26
- 0
src/git/models/pullRequest.ts 查看文件

@ -24,6 +24,32 @@ export interface PullRequestShape extends IssueOrPullRequest {
readonly mergedDate?: Date;
}
export function toPullRequestShape(value: PullRequest): PullRequestShape {
const shape: PullRequestShape = {
type: value.type,
provider: {
id: value.provider.id,
name: value.provider.name,
domain: value.provider.domain,
icon: value.provider.icon,
},
id: value.id,
title: value.title,
url: value.url,
date: value.date,
closedDate: value.closedDate,
closed: value.closed,
author: {
name: value.author.name,
avatarUrl: value.author.avatarUrl,
url: value.author.url,
},
state: value.state,
mergedDate: value.mergedDate,
};
return shape;
}
export class PullRequest implements PullRequestShape {
static is(pr: any): pr is PullRequest {
return pr instanceof PullRequest;

+ 14
- 4
src/system/serialize.ts 查看文件

@ -9,9 +9,19 @@ export type Serialized = T extends Function
: T;
export function serialize<T extends object>(obj: T): Serialized<T> {
function replacer(this: any, key: string, value: unknown) {
const original = this[key];
return original instanceof Date ? original.getTime() : value;
try {
function replacer(this: any, key: string, value: unknown) {
if (value instanceof Date) return value.getTime();
if (value instanceof Map || value instanceof Set) return [...value.entries()];
if (value instanceof Function || value instanceof Error) return undefined;
if (value instanceof RegExp) return value.toString();
const original = this[key];
return original instanceof Date ? original.getTime() : value;
}
return JSON.parse(JSON.stringify(obj, replacer)) as Serialized<T>;
} catch (ex) {
debugger;
throw ex;
}
return JSON.parse(JSON.stringify(obj, replacer)) as Serialized<T>;
}

+ 21
- 22
src/webviews/apps/commitDetails/commitDetails.ts 查看文件

@ -6,7 +6,6 @@ import type { CommitActionsParams, State } from '../../commitDetails/protocol';
import {
AutolinkSettingsCommandType,
CommitActionsCommandType,
DidChangeRichStateNotificationType,
DidChangeStateNotificationType,
FileActionsCommandType,
OpenFileCommandType,
@ -95,27 +94,27 @@ export class CommitDetailsApp extends App> {
protected override onMessageReceived(e: MessageEvent) {
const msg = e.data as IpcMessage;
switch (msg.method) {
case DidChangeRichStateNotificationType.method:
onIpc(DidChangeRichStateNotificationType, msg, params => {
if (this.state.selected == null) return;
assertsSerialized<typeof params>(params);
const newState = { ...this.state };
if (params.formattedMessage != null) {
newState.selected!.message = params.formattedMessage;
}
// if (params.pullRequest != null) {
newState.pullRequest = params.pullRequest;
// }
// if (params.formattedMessage != null) {
newState.autolinkedIssues = params.autolinkedIssues;
// }
this.state = newState;
this.renderRichContent();
});
break;
// case DidChangeRichStateNotificationType.method:
// onIpc(DidChangeRichStateNotificationType, msg, params => {
// if (this.state.selected == null) return;
// assertsSerialized<typeof params>(params);
// const newState = { ...this.state };
// if (params.formattedMessage != null) {
// newState.selected!.message = params.formattedMessage;
// }
// // if (params.pullRequest != null) {
// newState.pullRequest = params.pullRequest;
// // }
// // if (params.formattedMessage != null) {
// newState.autolinkedIssues = params.autolinkedIssues;
// // }
// this.state = newState;
// this.renderRichContent();
// });
// break;
case DidChangeStateNotificationType.method:
onIpc(DidChangeStateNotificationType, msg, params => {
assertsSerialized<typeof params.state>(params.state);

+ 19
- 14
src/webviews/commitDetails/commitDetailsWebviewView.ts 查看文件

@ -10,6 +10,7 @@ import type { GitFileChange } from '../../git/models/file';
import { GitFile } from '../../git/models/file';
import type { IssueOrPullRequest } from '../../git/models/issue';
import type { PullRequest } from '../../git/models/pullRequest';
import { toPullRequestShape } from '../../git/models/pullRequest';
import type { GitRevisionReference } from '../../git/models/reference';
import { Logger } from '../../logger';
import type { ShowCommitInGraphCommandArgs } from '../../plus/webviews/graph/graphWebview';
@ -306,7 +307,7 @@ export class CommitDetailsWebviewView extends WebviewViewBase
const cancellation = this._cancellationTokenSource.token;
setTimeout(() => {
if (cancellation.isCancellationRequested) return;
void this.updateRichState(cancellation);
void this.updateRichState(current, cancellation);
}, 100);
}
}
@ -320,15 +321,14 @@ export class CommitDetailsWebviewView extends WebviewViewBase
preferences: current.preferences,
selected: details,
autolinkedIssues: current.autolinkedIssues,
pullRequest: current.pullRequest,
pullRequest: current.pullRequest != null ? toPullRequestShape(current.pullRequest) : undefined,
dateFormat: dateFormat,
});
return state;
}
private async updateRichState(cancellation: CancellationToken): Promise<void> {
const commit = this._context.commit;
private async updateRichState(current: Context, cancellation: CancellationToken): Promise<void> {
const commit = current.commit;
if (commit == null) return;
const remotes = await this.container.git.getRemotesWithProviders(commit.repoPath, { sort: true });
@ -508,18 +508,23 @@ export class CommitDetailsWebviewView extends WebviewViewBase
const sw = new Stopwatch(scope);
Logger.warn(scope, `notifyDidChangeState(${this._counter}) starting...`);
const success = await this.notify(DidChangeStateNotificationType, {
state: await this.getState(context),
});
Logger.warn(scope, `(${this._counter}) starting...`);
try {
const success = await this.notify(DidChangeStateNotificationType, {
state: await this.getState(context),
});
if (success) {
this._context = context;
this._pendingContext = undefined;
}
} catch (ex) {
Logger.error(scope, ex);
debugger;
}
sw.stop();
Logger.warn(scope, `notifyDidChangeState(${this._counter}) ended after ${sw.elapsed()}ms`);
if (success) {
this._context = context;
this._pendingContext = undefined;
}
Logger.warn(scope, `(${this._counter}) completed after ${sw.elapsed()} ms`);
});
}

Loading…
取消
儲存