Skip to content

Commit 092e234

Browse files
authored
Fix theme proxy generation performance issue (#971)
* logging and prevent excessive theme changes * callback dependency * add await * Move if one layer up * remove `removeItem` * clear up useEffect * Revert "clear up useEffect" This reverts commit f5eddec. * Revert "remove `removeItem`" This reverts commit 11ab4b3. * Revert "Move if one layer up" This reverts commit cdc7e60. * Revert "add await" This reverts commit 55fd0a0. * Revert "callback dependency" This reverts commit 1ade218. * Revert "logging and prevent excessive theme changes" This reverts commit f10d982. * Remove zod parsing in creating values * Revert "Remove zod parsing in creating values" This reverts commit d6c0001. * Only use zod for validation, remove from proxy creation steps
1 parent 34c5fe7 commit 092e234

File tree

6 files changed

+30
-15
lines changed

6 files changed

+30
-15
lines changed

packages/theme/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"color": "^4.2.3",
4444
"deepmerge-ts": "^7.1.3",
4545
"react-native-typography": "^1.4.1",
46+
"lodash.isobject": "^3.0.2",
4647
"zod": "^3.23.8"
4748
},
4849
"eslintIgnore": [

packages/theme/src/createTheme.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import type {
66
ColorPalettes,
77
} from "./types";
88
import {
9-
isTextStyleObject,
109
validateBreakpoints,
1110
validatePalettes,
1211
validateTheme,
1312
} from "./validators";
13+
import { isTextStyleObject } from "./utils";
1414

1515
/**
1616
* Custom deepmerge function to skip merging of typography/text style objects.

packages/theme/src/createThemeValuesProxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ThemeValues, Breakpoints } from "./types";
22
import { Platform, TextStyle } from "react-native";
3-
import { asThemeValuesObject } from "./validators";
3+
import { asThemeValuesObject } from "./utils";
44

55
interface CreateThemeValuesProxyInput {
66
value: ThemeValues | undefined;

packages/theme/src/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { isObject } from "lodash";
2+
import type { ThemeValues } from "./types";
3+
4+
export function isTextStyleObject(value: any): boolean {
5+
return Object.keys(value).some((key) =>
6+
[
7+
"fontFamily",
8+
"fontWeight",
9+
"fontSize",
10+
"fontStyle",
11+
"lineHeight",
12+
"letterSpacing",
13+
].includes(key)
14+
);
15+
}
16+
17+
export function asThemeValuesObject(value: any): ThemeValues | null {
18+
// Any object that isn't a text style object is considered a ThemeValues object
19+
return !isTextStyleObject(value) && isObject(value)
20+
? (value as ThemeValues)
21+
: null;
22+
}

packages/theme/src/validators.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,3 @@ export function validateTheme(theme: Theme) {
101101
throw new Error("Invalid theme object: " + result.error.message);
102102
}
103103
}
104-
105-
export function isTextStyleObject(value: any): boolean {
106-
return TextStyleSchema.safeParse(value).success;
107-
}
108-
109-
export function asThemeValuesObject(value: any): ThemeValues | null {
110-
// Text style matches the shape of ThemeValues, but we don't want to treat it as a ThemeValues
111-
if (isTextStyleObject(value)) {
112-
return null;
113-
}
114-
115-
return ThemeValuesSchema.safeParse(value).success ? value : null;
116-
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10446,6 +10446,11 @@ lodash.isnumber@^3.0.3:
1044610446
resolved "https://registry.yarnpkg.com/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz#3ce76810c5928d03352301ac287317f11c0b1ffc"
1044710447
integrity sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==
1044810448

10449+
lodash.isobject@^3.0.2:
10450+
version "3.0.2"
10451+
resolved "https://registry.yarnpkg.com/lodash.isobject/-/lodash.isobject-3.0.2.tgz#3c8fb8d5b5bf4bf90ae06e14f2a530a4ed935e1d"
10452+
integrity sha512-3/Qptq2vr7WeJbB4KHUSKlq8Pl7ASXi3UG6CMbBm8WRtXi8+GHm7mKaU3urfpSEzWe2wCIChs6/sdocUsTKJiA==
10453+
1044910454
lodash.memoize@^4.1.2:
1045010455
version "4.1.2"
1045110456
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"

0 commit comments

Comments
 (0)