Skip to content

Commit b804086

Browse files
update performance for theme api
1 parent a51c6f3 commit b804086

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

packages/cli/src/components/codeTools.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createStore, markRaw, ref } from "reactivity-store";
22
import stringWidth from "string-width";
33

4-
import { buildTheme } from "./color";
4+
import { buildTheme, isThemeEqual } from "./color";
55

66
import type { CodeViewProps } from "./CodeView";
77
import type { ResolvedDiffViewColorTheme, DiffViewColorTheme } from "./color";
@@ -62,7 +62,12 @@ export const createCodeConfigStore = <T = any>(props: CodeViewProps<T>, fileId:
6262

6363
const themeColors = ref(buildTheme(props.codeViewThemeColors));
6464

65-
const setThemeColors = (_themeColors?: DiffViewColorTheme) => (themeColors.value = buildTheme(_themeColors));
65+
const setThemeColors = (_themeColors?: DiffViewColorTheme) => {
66+
const next = buildTheme(_themeColors);
67+
if (!isThemeEqual(themeColors.value, next)) {
68+
themeColors.value = next;
69+
}
70+
};
6671

6772
return {
6873
id,

packages/cli/src/components/color.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,12 @@ export const buildTheme = (overrides?: DiffViewColorTheme): ResolvedDiffViewColo
6767
}
6868
return result;
6969
};
70+
71+
export const isThemeEqual = (a: ResolvedDiffViewColorTheme, b: ResolvedDiffViewColorTheme): boolean => {
72+
if (a === b) return true;
73+
const keys = Object.keys(a) as (keyof ResolvedDiffViewColorTheme)[];
74+
for (const key of keys) {
75+
if (a[key].light !== b[key].light || a[key].dark !== b[key].dark) return false;
76+
}
77+
return true;
78+
};

packages/cli/src/components/tools.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { createStore, markRaw, ref, configureEnv } from "reactivity-store";
22
import stringWidth from "string-width";
33

4-
import { buildTheme } from "./color";
4+
import { buildTheme, isThemeEqual } from "./color";
55

6-
import type { DiffModeEnum, DiffViewProps } from "./DiffView";
76
import type { ResolvedDiffViewColorTheme, DiffViewColorTheme } from "./color";
7+
import type { DiffModeEnum, DiffViewProps } from "./DiffView";
88
import type { DiffLine } from "@git-diff-view/core";
99
import type { DOMElement } from "ink";
1010
import type { Ref, UseSelectorWithStore } from "reactivity-store";
@@ -85,7 +85,12 @@ export const createDiffConfigStore = <T = any>(props: DiffViewProps<T>, diffFile
8585

8686
const themeColors = ref(buildTheme(props.diffViewThemeColors));
8787

88-
const setThemeColors = (_themeColors?: DiffViewColorTheme) => (themeColors.value = buildTheme(_themeColors));
88+
const setThemeColors = (_themeColors?: DiffViewColorTheme) => {
89+
const next = buildTheme(_themeColors);
90+
if (!isThemeEqual(themeColors.value, next)) {
91+
themeColors.value = next;
92+
}
93+
};
8994

9095
return {
9196
id,

0 commit comments

Comments
 (0)