Browse Source

Consolidates into common `updatePendingContext`

main
Eric Amodio 1 year ago
parent
commit
f338f4bb5f
3 changed files with 43 additions and 42 deletions
  1. +4
    -16
      src/plus/webviews/timeline/timelineWebview.ts
  2. +5
    -26
      src/webviews/commitDetails/commitDetailsWebview.ts
  3. +34
    -0
      src/webviews/webviewController.ts

+ 4
- 16
src/plus/webviews/timeline/timelineWebview.ts View File

@ -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<Context>): boolean {
let changed = false;
for (const [key, value] of Object.entries(context)) {
const current = (this._context as unknown as Record<string, unknown>)[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<string, unknown>)[key] = value;
changed = true;
const [changed, pending] = updatePendingContext(this._context, this._pendingContext, context);
if (changed) {
this._pendingContext = pending;
}
return changed;

+ 5
- 26
src/webviews/commitDetails/commitDetailsWebview.ts View File

@ -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
return state;
}
@debug({ args: false })
private async updateRichState(current: Context, cancellation: CancellationToken): Promise<void> {
const { commit } = current;
if (commit == null) return;
@ -640,32 +642,9 @@ export class CommitDetailsWebviewProvider implements WebviewProvider
}
private updatePendingContext(context: Partial<Context>, force: boolean = false): boolean {
let changed = false;
for (const [key, value] of Object.entries(context)) {
const current = (this._context as unknown as Record<string, unknown>)[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<string, unknown>)[key] = value;
changed = true;
const [changed, pending] = updatePendingContext(this._context, this._pendingContext, context, force);
if (changed) {
this._pendingContext = pending;
}
return changed;

+ 34
- 0
src/webviews/webviewController.ts View File

@ -568,3 +568,37 @@ export function setContextKeys(
void setContext(`${contextKeyPrefix}:inputFocus`, inputFocus);
}
}
export function updatePendingContext<Context extends object>(
current: Context,
pending: Partial<Context> | undefined,
update: Partial<Context>,
force: boolean = false,
): [changed: boolean, pending: Partial<Context> | undefined] {
let changed = false;
for (const [key, value] of Object.entries(update)) {
const currentValue = (current as unknown as Record<string, unknown>)[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<string, unknown>)[key] = value;
changed = true;
}
return [changed, pending];
}

Loading…
Cancel
Save