Skip to content

Commit cb842c8

Browse files
committed
refactor(theme): reorder ThemeResolverCache methods for clarity
- Moved `_generateCacheKey` and `_evict` methods below the main logic in `ThemeResolverCache` for better readability and organization - Removed duplicate declaration of `_instance` property - Kept functionality unchanged, improving code structure and maintainability
1 parent 2ea128e commit cb842c8

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,4 +887,4 @@ All notable changes to this project will be documented in this file.
887887
- This changelog covers the most recent development history available
888888
- The project focuses on building React UIs with type-safe fluency without JSX syntax
889889
- Recent development has emphasized Emotion integration, type safety improvements, and enhanced flexbox support
890-
- For a complete history, view all commits on GitHub: [View all commits](https://github.com/l7aromeo/meonode-ui/commits)
890+
- For a complete history, view all commits on GitHub: [View all commits](https://github.com/l7aromeo/meonode-ui/commits)

src/util/theme.util.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ import { getValueByPath } from '@src/helper/common.helper.js'
77
* Cache manager for theme resolution operations.
88
*/
99
class ThemeResolverCache {
10+
private static _instance: ThemeResolverCache | null = null
1011
private readonly CACHE_SIZE_LIMIT = 500
1112
private readonly CACHE_EVICTION_BATCH_SIZE = 50
12-
13-
private static _instance: ThemeResolverCache | null = null
14-
1513
private readonly _resolutionCache = new Map<string, Record<string, unknown>>()
1614
private readonly _pathLookupCache = new Map<string, Record<string, unknown> | string | null>()
1715
private readonly _themeRegex = /theme\.([a-zA-Z0-9_.-]+)/g
@@ -23,14 +21,6 @@ class ThemeResolverCache {
2321
return ThemeResolverCache._instance
2422
}
2523

26-
/**
27-
* Generate a stable cache key from object and theme, including the theme mode.
28-
*/
29-
private _generateCacheKey(obj: Record<string, any>, theme: Theme): string {
30-
// Including theme.mode is critical for cache correctness.
31-
return `${ObjHelper.stringify(obj)}_${theme.mode}_${ObjHelper.stringify(theme.system)}`
32-
}
33-
3424
getResolution<O extends Record<string, unknown>>(obj: O, theme: Theme) {
3525
const key = this._generateCacheKey(obj, theme)
3626
const result = this._resolutionCache.get(key) as O
@@ -69,18 +59,6 @@ class ThemeResolverCache {
6959
}
7060
}
7161

72-
private _evict(cache: Map<string, unknown>) {
73-
const keys = cache.keys()
74-
for (let i = 0; i < this.CACHE_EVICTION_BATCH_SIZE; i++) {
75-
const key = keys.next().value
76-
if (key) {
77-
cache.delete(key)
78-
} else {
79-
break
80-
}
81-
}
82-
}
83-
8462
getThemeRegex(): RegExp {
8563
this._themeRegex.lastIndex = 0
8664
return this._themeRegex
@@ -94,25 +72,39 @@ class ThemeResolverCache {
9472
this._resolutionCache.clear()
9573
this._pathLookupCache.clear()
9674
}
75+
76+
/**
77+
* Generate a stable cache key from object and theme, including the theme mode.
78+
*/
79+
private _generateCacheKey(obj: Record<string, any>, theme: Theme): string {
80+
// Including theme.mode is critical for cache correctness.
81+
return `${ObjHelper.stringify(obj)}_${theme.mode}_${ObjHelper.stringify(theme.system)}`
82+
}
83+
84+
private _evict(cache: Map<string, unknown>) {
85+
const keys = cache.keys()
86+
for (let i = 0; i < this.CACHE_EVICTION_BATCH_SIZE; i++) {
87+
const key = keys.next().value
88+
if (key) {
89+
cache.delete(key)
90+
} else {
91+
break
92+
}
93+
}
94+
}
9795
}
9896

99-
/**
100-
* Parsed flex shorthand components for CSS flex property
101-
* @interface FlexComponents
102-
* @property grow - The flex-grow value (how much the item should grow)
103-
* @property shrink - The flex-shrink value (how much the item should shrink)
104-
* @property basis - The flex-basis value (initial main size before free space is distributed)
105-
*/
10697
interface FlexComponents {
10798
grow: number
10899
shrink: number
109100
basis: string | number
110101
}
111-
export class ThemeUtil {
112-
private constructor() {}
113102

103+
export class ThemeUtil {
114104
private static themeCache = ThemeResolverCache.getInstance()
115105

106+
private constructor() {}
107+
116108
/**
117109
* Parses a CSS flex shorthand property into its individual components.
118110
*

0 commit comments

Comments
 (0)