MCPcopy Create free account
hub / github.com/callstack/react-native-material-palette / getCacheKey

Function getCacheKey

src/createPalette.tsx:10–38  ·  view source on GitHub ↗
(
  source: ImageSourcePropType,
  options: PaletteOptions & { target?: PaletteTarget }
)

Source from the content-addressed store, hash-verified

8} from './types';
9
10export function getCacheKey(
11 source: ImageSourcePropType,
12 options: PaletteOptions & { target?: PaletteTarget }
13): string {
14 const uri = Image.resolveAssetSource(source)?.uri ?? '';
15
16 function serialize(obj: any): string {
17 if (obj == null) {
18 return '';
19 }
20
21 if (typeof obj !== 'object') {
22 return String(obj);
23 }
24
25 if (Array.isArray(obj)) {
26 return obj.map(serialize).join(',');
27 }
28
29 return Object.entries(obj)
30 .sort(([a], [b]) => a.localeCompare(b))
31 .map(([k, v]) => `${k}:${serialize(v)}`)
32 .join(',');
33 }
34
35 const optionsStr = serialize(options);
36
37 return [uri, optionsStr].filter(Boolean).join('|');
38}
39
40export const paletteCache = new Map<string, PaletteResult>();
41export const swatchCache = new Map<string, PaletteSwatch>();

Callers 3

usePaletteSwatchFunction · 0.90
createPaletteFunction · 0.85
createPaletteForTargetFunction · 0.85

Calls 1

serializeFunction · 0.85

Tested by

no test coverage detected