Skip to content

Commit

Permalink
Merge pull request #1623 from gluestack/fix/inject-sx-memoize
Browse files Browse the repository at this point in the history
fix: memoize inject sx
  • Loading branch information
surajahmed authored Jan 12, 2024
2 parents 659a535 + 323a2c6 commit 7950b4e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/styled/react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gluestack-style/react",
"description": "A universal & performant styling library for React Native, Next.js & React",
"version": "1.0.37",
"version": "1.0.39-alpha.0",
"keywords": [
"React Native",
"Next.js",
Expand Down
18 changes: 17 additions & 1 deletion packages/styled/react/src/styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ import {
import { updateOrderUnResolvedMap } from './updateOrderUnResolvedMap';
import { resolveComponentTheme } from './createConfig';

// Create a caching object
let sxMemoizationCache: any = {};

const styledSystemProps = { ...CSSPropertiesMap };

function isSubset(subset: any, set: any) {
Expand Down Expand Up @@ -917,10 +920,21 @@ export function verboseStyled<P, Variants, ComCon>(
inlineStyleMap?: any,
ignoreKeys: Set<any> = new Set()
) {
const sxHash = stableHash(sx);

const memoizationKey = sxHash + type;
// Check if the result is already in the cache
if (sxMemoizationCache[memoizationKey]) {
return sxMemoizationCache[memoizationKey];
}

const inlineSxTheme = {
baseStyle: sx,
};

// if (Platform.OS === '')
// console.log(sxHash, GluestackStyleSheet.getStyleMap(), 'hash here');

resolvePlatformTheme(inlineSxTheme, Platform.OS);
const sxStyledResolved = styledToStyledResolved(
// @ts-ignore
Expand Down Expand Up @@ -958,7 +972,7 @@ export function verboseStyled<P, Variants, ComCon>(
);
}

const sxHash = stableHash(sx);
// const sxHash = stableHash(sx);

const orderedSXResolved =
styledResolvedToOrderedSXResolved(sxStyledResolved);
Expand All @@ -980,6 +994,8 @@ export function verboseStyled<P, Variants, ComCon>(
CONFIG
);

sxMemoizationCache[memoizationKey] = orderedSXResolved;

return orderedSXResolved;
}

Expand Down

0 comments on commit 7950b4e

Please sign in to comment.