Skip to content

Commit

Permalink
fix: memoize inject sx
Browse files Browse the repository at this point in the history
  • Loading branch information
Suraj authored and Suraj committed Jan 12, 2024
1 parent 554f5fa commit a5eb341
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/styled/react/src/styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ import {
import { updateOrderUnResolvedMap } from './updateOrderUnResolvedMap';
import { resolveComponentTheme } from './createConfig';

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

// Define a function to generate a unique key for the function arguments
const generateCacheKey = (args: any) => stableHash(args);

const styledSystemProps = { ...CSSPropertiesMap };

function isSubset(subset: any, set: any) {
Expand Down Expand Up @@ -917,10 +923,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 +975,7 @@ export function verboseStyled<P, Variants, ComCon>(
);
}

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

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

sxMemoizationCache[memoizationKey] = orderedSXResolved;

return orderedSXResolved;
}

Expand Down

0 comments on commit a5eb341

Please sign in to comment.