diff --git a/src/plus/webviews/timeline/timelineWebview.ts b/src/plus/webviews/timeline/timelineWebview.ts index 1fcdf6f..2bfc7a9 100644 --- a/src/plus/webviews/timeline/timelineWebview.ts +++ b/src/plus/webviews/timeline/timelineWebview.ts @@ -20,6 +20,7 @@ import { hasVisibleTextEditor, isTextEditor } from '../../../system/utils'; import type { IpcMessage } from '../../../webviews/protocol'; import { onIpc } from '../../../webviews/protocol'; import type { WebviewController, WebviewProvider } from '../../../webviews/webviewController'; +import { updatePendingContext } from '../../../webviews/webviewController'; import type { SubscriptionChangeEvent } from '../../subscription/subscriptionService'; import type { Commit, Period, State } from './protocol'; import { DidChangeNotificationType, OpenDataPointCommandType, UpdatePeriodCommandType } from './protocol'; @@ -360,22 +361,9 @@ export class TimelineWebviewProvider implements WebviewProvider { } private updatePendingContext(context: Partial): boolean { - let changed = false; - for (const [key, value] of Object.entries(context)) { - const current = (this._context as unknown as Record)[key]; - if ( - current === value || - ((current instanceof Uri || value instanceof Uri) && (current as any)?.toString() === value?.toString()) - ) { - continue; - } - - if (this._pendingContext == null) { - this._pendingContext = {}; - } - - (this._pendingContext as Record)[key] = value; - changed = true; + const [changed, pending] = updatePendingContext(this._context, this._pendingContext, context); + if (changed) { + this._pendingContext = pending; } return changed; diff --git a/src/webviews/commitDetails/commitDetailsWebview.ts b/src/webviews/commitDetails/commitDetailsWebview.ts index 1a15480..255324b 100644 --- a/src/webviews/commitDetails/commitDetailsWebview.ts +++ b/src/webviews/commitDetails/commitDetailsWebview.ts @@ -46,6 +46,7 @@ import type { LinesChangeEvent } from '../../trackers/lineTracker'; import type { IpcMessage } from '../protocol'; import { onIpc } from '../protocol'; import type { WebviewController, WebviewProvider } from '../webviewController'; +import { updatePendingContext } from '../webviewController'; import type { CommitDetails, FileActionParams, Preferences, State } from './protocol'; import { AutolinkSettingsCommandType, @@ -441,6 +442,7 @@ export class CommitDetailsWebviewProvider implements WebviewProvider { const { commit } = current; if (commit == null) return; @@ -640,32 +642,9 @@ export class CommitDetailsWebviewProvider implements WebviewProvider, force: boolean = false): boolean { - let changed = false; - for (const [key, value] of Object.entries(context)) { - const current = (this._context as unknown as Record)[key]; - if ( - !force && - (current instanceof Uri || value instanceof Uri) && - (current as any)?.toString() === value?.toString() - ) { - continue; - } - - if (!force && current === value) { - if ( - (value !== undefined || key in this._context) && - (this._pendingContext == null || !(key in this._pendingContext)) - ) { - continue; - } - } - - if (this._pendingContext == null) { - this._pendingContext = {}; - } - - (this._pendingContext as Record)[key] = value; - changed = true; + const [changed, pending] = updatePendingContext(this._context, this._pendingContext, context, force); + if (changed) { + this._pendingContext = pending; } return changed; diff --git a/src/webviews/webviewController.ts b/src/webviews/webviewController.ts index a5fd834..4d1bd4c 100644 --- a/src/webviews/webviewController.ts +++ b/src/webviews/webviewController.ts @@ -568,3 +568,37 @@ export function setContextKeys( void setContext(`${contextKeyPrefix}:inputFocus`, inputFocus); } } + +export function updatePendingContext( + current: Context, + pending: Partial | undefined, + update: Partial, + force: boolean = false, +): [changed: boolean, pending: Partial | undefined] { + let changed = false; + for (const [key, value] of Object.entries(update)) { + const currentValue = (current as unknown as Record)[key]; + if ( + !force && + (currentValue instanceof Uri || value instanceof Uri) && + (currentValue as any)?.toString() === value?.toString() + ) { + continue; + } + + if (!force && currentValue === value) { + if ((value !== undefined || key in current) && (pending == null || !(key in pending))) { + continue; + } + } + + if (pending == null) { + pending = {}; + } + + (pending as Record)[key] = value; + changed = true; + } + + return [changed, pending]; +}