@codeleap/styles 5.8.2 → 5.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -15
- package/package.json.bak +8 -14
- package/src/classes/Cacher.ts +169 -0
- package/src/classes/StaleControl.ts +125 -0
- package/src/classes/StyleCache.ts +116 -0
- package/src/classes/StylePersistor.ts +62 -0
- package/src/{lib → classes}/StyleRegistry.ts +7 -12
- package/src/classes/index.ts +2 -0
- package/src/classes/tests/Cache.spec.ts +371 -0
- package/src/classes/tests/StaleControl.spec.ts +175 -0
- package/src/classes/tests/StyleCache.spec.ts +452 -0
- package/src/classes/tests/StylePersistor.spec.ts +231 -0
- package/src/{lib/constants.ts → constants.ts} +1 -0
- package/src/hooks/index.ts +4 -0
- package/src/hooks/tests/useCompositionStyles.spec.ts +107 -0
- package/src/hooks/tests/useStyleObserver.spec.ts +89 -0
- package/src/hooks/useCompositionStyles.ts +33 -0
- package/src/hooks/useNestedStylesByKey.ts +13 -0
- package/src/hooks/useStyleObserver.ts +19 -0
- package/src/hooks/useTheme.ts +16 -0
- package/src/index.ts +9 -5
- package/src/lib/createStyles.ts +10 -1
- package/src/lib/createTheme.ts +22 -13
- package/src/lib/index.ts +1 -10
- package/src/lib/tests/createStyles.spec.ts +151 -0
- package/src/tests/colors/baseColors.ts +166 -0
- package/src/tests/colors/darkMode.ts +232 -0
- package/src/tests/colors/lightMode.ts +285 -0
- package/src/tests/measures.ts +31 -0
- package/src/tests/theme.ts +58 -0
- package/src/theme/generateColorScheme.ts +53 -0
- package/src/theme/index.ts +3 -0
- package/src/theme/tests/generateColorScheme.spec.ts +118 -0
- package/src/theme/tests/themeStore.spec.ts +698 -0
- package/src/theme/tests/validateTheme.spec.ts +173 -0
- package/src/{lib → theme}/themeStore.ts +68 -3
- package/src/{lib → theme}/validateTheme.ts +13 -0
- package/src/tools/colors.ts +83 -39
- package/src/tools/deepClone.ts +10 -0
- package/src/tools/deepmerge.ts +10 -0
- package/src/{lib → tools}/hashKey.ts +7 -0
- package/src/tools/index.ts +6 -1
- package/src/tools/minifier.ts +38 -0
- package/src/tools/tests/colors.spec.ts +233 -0
- package/src/tools/tests/deepClone.spec.ts +102 -0
- package/src/tools/tests/deepmerge.spec.ts +155 -0
- package/src/tools/tests/hashKey.spec.ts +69 -0
- package/src/tools/tests/minifier.spec.ts +173 -0
- package/src/types/store.ts +2 -2
- package/src/types/style.ts +3 -3
- package/src/types/theme.ts +4 -4
- package/src/{lib/utils.ts → utils.ts} +3 -3
- package/src/{lib → variants}/borderCreator.ts +2 -2
- package/src/{lib → variants}/createAppVariants.ts +1 -1
- package/src/{lib → variants}/dynamicVariants.ts +1 -1
- package/src/variants/index.ts +6 -0
- package/src/{lib → variants}/spacing.ts +37 -24
- package/src/variants/tests/borderCreator.spec.ts +180 -0
- package/src/variants/tests/dynamicVariants.spec.ts +194 -0
- package/src/variants/tests/spacing.spec.ts +177 -0
- package/src/lib/Cacher.ts +0 -112
- package/src/lib/StaleControl.ts +0 -73
- package/src/lib/StyleCache.ts +0 -81
- package/src/lib/StylePersistor.ts +0 -28
- package/src/lib/hooks.ts +0 -64
- package/src/lib/minifier.ts +0 -20
- /package/src/{lib → tools}/multiplierProperty.ts +0 -0
- /package/src/{lib → variants}/defaultVariants.ts +0 -0
- /package/src/{lib → variants}/mediaQuery.ts +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeleap/styles",
|
|
3
|
-
"version": "5.8.
|
|
3
|
+
"version": "5.8.4",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -9,28 +9,22 @@
|
|
|
9
9
|
"directory": "packages/styles"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@codeleap/config": "5.8.
|
|
12
|
+
"@codeleap/config": "5.8.4",
|
|
13
13
|
"ts-node-dev": "^1.1.8"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
|
-
"build": "echo 'No build needed'"
|
|
17
|
-
"lint": "eslint -c .eslintrc.js --fix \"./src/**/*.{ts,tsx,js,jsx}\"",
|
|
18
|
-
"test": "./node_modules/.bin/mocha --require ts-node/register src/tests/**/*.spec.ts",
|
|
19
|
-
"run-sc": "tsnd --transpile-only",
|
|
20
|
-
"test:watch": "./node_modules/.bin/mocha --require ts-node/register ./src/tests/**/*.spec.ts -w --watch-files ./src/tests/**/*.spec.ts",
|
|
21
|
-
"createStyleSheets": "bun run-sc src/scripts/createStyleSheets.ts"
|
|
16
|
+
"build": "echo 'No build needed'"
|
|
22
17
|
},
|
|
23
18
|
"peerDependencies": {
|
|
24
19
|
"react": "18.2.0",
|
|
25
20
|
"typescript": "5.0.4"
|
|
26
21
|
},
|
|
27
22
|
"dependencies": {
|
|
28
|
-
"@fastify/deepmerge": "1.
|
|
29
|
-
"@nanostores/
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"rfdc": "^1.4.1"
|
|
23
|
+
"@fastify/deepmerge": "3.1.0",
|
|
24
|
+
"@nanostores/react": "1.0.0",
|
|
25
|
+
"js-sha256": "0.11.1",
|
|
26
|
+
"lz-string": "1.5.0",
|
|
27
|
+
"nanostores": "1.0.1",
|
|
28
|
+
"rfdc": "1.4.1"
|
|
35
29
|
}
|
|
36
30
|
}
|
package/package.json.bak
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeleap/styles",
|
|
3
|
-
"version": "5.8.
|
|
3
|
+
"version": "5.8.4",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -13,24 +13,18 @@
|
|
|
13
13
|
"ts-node-dev": "^1.1.8"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
|
-
"build": "echo 'No build needed'"
|
|
17
|
-
"lint": "eslint -c .eslintrc.js --fix \"./src/**/*.{ts,tsx,js,jsx}\"",
|
|
18
|
-
"test": "./node_modules/.bin/mocha --require ts-node/register src/tests/**/*.spec.ts",
|
|
19
|
-
"run-sc": "tsnd --transpile-only",
|
|
20
|
-
"test:watch": "./node_modules/.bin/mocha --require ts-node/register ./src/tests/**/*.spec.ts -w --watch-files ./src/tests/**/*.spec.ts",
|
|
21
|
-
"createStyleSheets": "bun run-sc src/scripts/createStyleSheets.ts"
|
|
16
|
+
"build": "echo 'No build needed'"
|
|
22
17
|
},
|
|
23
18
|
"peerDependencies": {
|
|
24
19
|
"react": "18.2.0",
|
|
25
20
|
"typescript": "5.0.4"
|
|
26
21
|
},
|
|
27
22
|
"dependencies": {
|
|
28
|
-
"@fastify/deepmerge": "1.
|
|
29
|
-
"@nanostores/
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"rfdc": "^1.4.1"
|
|
23
|
+
"@fastify/deepmerge": "3.1.0",
|
|
24
|
+
"@nanostores/react": "1.0.0",
|
|
25
|
+
"js-sha256": "0.11.1",
|
|
26
|
+
"lz-string": "1.5.0",
|
|
27
|
+
"nanostores": "1.0.1",
|
|
28
|
+
"rfdc": "1.4.1"
|
|
35
29
|
}
|
|
36
30
|
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { CacheType } from '../types/cache'
|
|
2
|
+
import { StateStorage } from '../types/store'
|
|
3
|
+
import { StyleConstants } from '../constants'
|
|
4
|
+
import { hashKey } from '../tools'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Generates a stale time date by adding 7 days to the current date
|
|
8
|
+
* @returns {Date} The future date representing the stale time
|
|
9
|
+
*/
|
|
10
|
+
function generateStaleTime() {
|
|
11
|
+
const time = 7
|
|
12
|
+
|
|
13
|
+
let currentTime = new Date()
|
|
14
|
+
|
|
15
|
+
currentTime.setDate(currentTime.getDate() + time)
|
|
16
|
+
|
|
17
|
+
return currentTime
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Generic cache implementation with persistence and stale time management
|
|
22
|
+
* @template T - The type of values stored in the cache
|
|
23
|
+
*/
|
|
24
|
+
export class Cache<T extends any = any> {
|
|
25
|
+
/** In-memory cache storage */
|
|
26
|
+
cache: Record<string, T> = {}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Gets the persistence key for cache data
|
|
30
|
+
* @returns {string} The storage key for cache data
|
|
31
|
+
*/
|
|
32
|
+
get persistKeyCache() {
|
|
33
|
+
return `@styles.caches.${this.registryName}.cache`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Gets the persistence key for stale time
|
|
38
|
+
* @returns {string} The storage key for stale time
|
|
39
|
+
*/
|
|
40
|
+
get persistKeyStaleTime() {
|
|
41
|
+
return `@styles.caches.${this.registryName}.staleTime`
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new Cache instance
|
|
46
|
+
* @param {CacheType} registryName - The name/type of the cache registry
|
|
47
|
+
* @param {StateStorage} [storage=null] - The storage implementation for persistence
|
|
48
|
+
* @param {boolean} [persistCache=!!storage] - Whether to persist cache to storage
|
|
49
|
+
*/
|
|
50
|
+
constructor(
|
|
51
|
+
public registryName: CacheType,
|
|
52
|
+
private storage: StateStorage = null,
|
|
53
|
+
public persistCache: boolean = !!storage,
|
|
54
|
+
) {
|
|
55
|
+
if (!persistCache || !StyleConstants.STORE_CACHE_ENABLED) return
|
|
56
|
+
|
|
57
|
+
const { persistedCache, persistedStaleTime } = this.loadStorage()
|
|
58
|
+
|
|
59
|
+
const currentTime = new Date()
|
|
60
|
+
|
|
61
|
+
const isStaled = currentTime > persistedStaleTime
|
|
62
|
+
|
|
63
|
+
if (isStaled) {
|
|
64
|
+
this.clearStorage()
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (StyleConstants.LOG) {
|
|
69
|
+
console.log('Cache [constructor] ->', { persistedCache, persistedStaleTime, registryName })
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
this.setCache(persistedCache)
|
|
73
|
+
this.storeStaleTime(persistedStaleTime)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Generates a cache key and retrieves the cached value
|
|
78
|
+
* @param {string} cacheBaseKey - The base key for cache generation
|
|
79
|
+
* @param {Array<any> | any} data - The data to use for key generation
|
|
80
|
+
* @returns {{ key: string, value: T | null }} Object containing the cache key and value
|
|
81
|
+
*/
|
|
82
|
+
keyFor(cacheBaseKey: string, data: Array<any> | any) {
|
|
83
|
+
const values = [cacheBaseKey, data]
|
|
84
|
+
|
|
85
|
+
const cacheKey = hashKey(values)
|
|
86
|
+
const cachedValue = this.cache[cacheKey] ?? null
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
key: cacheKey,
|
|
90
|
+
value: cachedValue,
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Stores a value in the cache and optionally persists it
|
|
96
|
+
* @param {string} key - The cache key
|
|
97
|
+
* @param {T} cache - The value to cache
|
|
98
|
+
* @returns {T} The cached value
|
|
99
|
+
*/
|
|
100
|
+
cacheFor(key: string, cache: T) {
|
|
101
|
+
this.cache[key] = cache
|
|
102
|
+
if (this.persistCache) this.storeCache()
|
|
103
|
+
return cache
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Replaces the entire cache with new data
|
|
108
|
+
* @param {Record<string, T>} cache - The new cache data
|
|
109
|
+
*/
|
|
110
|
+
setCache(cache: Record<string, T>) {
|
|
111
|
+
this.cache = cache ?? {}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Clears both in-memory and persistent cache
|
|
116
|
+
*/
|
|
117
|
+
clear() {
|
|
118
|
+
this.cache = {}
|
|
119
|
+
this.clearStorage()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Loads cache data and stale time from persistent storage
|
|
124
|
+
* @returns {{ persistedStaleTime: Date, persistedCache: any }} Loaded data from storage
|
|
125
|
+
*/
|
|
126
|
+
loadStorage() {
|
|
127
|
+
if (!this.persistCache) return { persistedStaleTime: generateStaleTime(), persistedCache: {} }
|
|
128
|
+
|
|
129
|
+
const persistedStaleTime = this.storage.getItem(this.persistKeyStaleTime)
|
|
130
|
+
const persistedCache = this.storage.getItem(this.persistKeyCache)
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
persistedStaleTime: !persistedStaleTime ? generateStaleTime() : new Date(persistedStaleTime),
|
|
134
|
+
persistedCache,
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Clears cache data from persistent storage
|
|
140
|
+
*/
|
|
141
|
+
clearStorage() {
|
|
142
|
+
if (!this.persistCache) return
|
|
143
|
+
|
|
144
|
+
this.storage.removeItem(this.persistKeyStaleTime)
|
|
145
|
+
this.storage.removeItem(this.persistKeyCache)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Stores cache data to persistent storage
|
|
150
|
+
* @param {Record<string, T>} [cache=null] - Cache data to store (uses current cache if not provided)
|
|
151
|
+
*/
|
|
152
|
+
storeCache(cache: Record<string, T> = null) {
|
|
153
|
+
if (!this.persistCache) return
|
|
154
|
+
|
|
155
|
+
const value = cache ?? this.cache
|
|
156
|
+
this.storage.setItem(this.persistKeyCache, value)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Stores stale time to persistent storage
|
|
161
|
+
* @param {Date} staleTime - The stale time to store
|
|
162
|
+
*/
|
|
163
|
+
storeStaleTime(staleTime: Date) {
|
|
164
|
+
if (!this.persistCache) return
|
|
165
|
+
|
|
166
|
+
const value = staleTime.toISOString()
|
|
167
|
+
this.storage.setItem(this.persistKeyStaleTime, value)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @UNUSED_IMPLEMENTATION - Class available for future use
|
|
3
|
+
* Stale data controller with temporal expiration system
|
|
4
|
+
*
|
|
5
|
+
* @description This implementation is complete but not currently being used.
|
|
6
|
+
* Developed for potential future need of time-based cache invalidation.
|
|
7
|
+
*/
|
|
8
|
+
export class StaleControl {
|
|
9
|
+
private wiperId: Timer | null = null
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @UNUSED_IMPLEMENTATION - Constructor available for future use
|
|
13
|
+
* @param staleTime - Expiration time in minutes (default: 60 minutes)
|
|
14
|
+
* @param staleTimeIdentifier - Separator to identify timestamp in value (default: '//:')
|
|
15
|
+
* @param wiperInterval - Expired data check interval in milliseconds (default: 30 minutes)
|
|
16
|
+
*/
|
|
17
|
+
constructor(
|
|
18
|
+
private staleTime: number = 60, // minutes
|
|
19
|
+
private staleTimeIdentifier: string = '//:',
|
|
20
|
+
private wiperInterval: number = 30 * 60 * 1000, // 30 minutes
|
|
21
|
+
) {}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @UNUSED_IMPLEMENTATION - Method available for future use
|
|
25
|
+
* Checks if a value is expired based on embedded timestamp
|
|
26
|
+
* @param value - Value with embedded timestamp
|
|
27
|
+
* @returns boolean indicating if value is expired
|
|
28
|
+
*/
|
|
29
|
+
isStaled(value: string): boolean {
|
|
30
|
+
const { staleTime } = this.extractStaleTime(value)
|
|
31
|
+
|
|
32
|
+
const currentTime = new Date()
|
|
33
|
+
|
|
34
|
+
const isStaled = currentTime > staleTime
|
|
35
|
+
|
|
36
|
+
return isStaled
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @UNUSED_IMPLEMENTATION - Method available for future use
|
|
41
|
+
* Inserts expiration timestamp into a value
|
|
42
|
+
* @param value - Original value without timestamp
|
|
43
|
+
* @returns Value with expiration timestamp embedded
|
|
44
|
+
*/
|
|
45
|
+
insertStaleTime(value: string): string {
|
|
46
|
+
const currentTime = new Date()
|
|
47
|
+
currentTime.setMinutes(currentTime.getMinutes() + this.staleTime)
|
|
48
|
+
|
|
49
|
+
const staleTime = currentTime.toISOString()
|
|
50
|
+
const valueWithStaleTime = `${value}${this.staleTimeIdentifier}${staleTime}`
|
|
51
|
+
|
|
52
|
+
return valueWithStaleTime
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @UNUSED_IMPLEMENTATION - Method available for future use
|
|
57
|
+
* Refreshes the expiration timestamp of a value
|
|
58
|
+
* @param value - Value with existing timestamp
|
|
59
|
+
* @returns Value with refreshed expiration timestamp
|
|
60
|
+
*/
|
|
61
|
+
refreshStaleTime(value: string): string {
|
|
62
|
+
const { value: extractedValue } = this.extractStaleTime(value)
|
|
63
|
+
const refreshedValue = this.insertStaleTime(extractedValue)
|
|
64
|
+
|
|
65
|
+
return refreshedValue
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @UNUSED_IMPLEMENTATION - Method available for future use
|
|
70
|
+
* Extracts expiration timestamp and original value
|
|
71
|
+
* @param value - Value with embedded timestamp
|
|
72
|
+
* @returns Object containing expiration date and original value
|
|
73
|
+
*/
|
|
74
|
+
extractStaleTime(value: string): { staleTime: Date; value: string } {
|
|
75
|
+
const [extractedValue, _staleTime] = value?.split(this.staleTimeIdentifier) || []
|
|
76
|
+
const staleTime = new Date(_staleTime || 0)
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
staleTime,
|
|
80
|
+
value: extractedValue || value,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @UNUSED_IMPLEMENTATION - Method available for future use
|
|
86
|
+
* Wipes expired cache; verifies isStaled values and removes them
|
|
87
|
+
* @throws Error indicating method is not implemented
|
|
88
|
+
*/
|
|
89
|
+
cacheWiper(): void {
|
|
90
|
+
throw new Error('Cache Wiper not implemented - Requires storage integration for future use')
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @UNUSED_IMPLEMENTATION - Method available for future use
|
|
95
|
+
* Registers periodic cache cleaning interval
|
|
96
|
+
*/
|
|
97
|
+
registerCacheWiper(): void {
|
|
98
|
+
if (this.wiperId !== null) {
|
|
99
|
+
this.unregisterCacheWiper()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
this.wiperId = setInterval(() => {
|
|
103
|
+
this.cacheWiper()
|
|
104
|
+
}, this.wiperInterval)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @UNUSED_IMPLEMENTATION - Method available for future use
|
|
109
|
+
* Unregisters periodic cache cleaning interval
|
|
110
|
+
*/
|
|
111
|
+
unregisterCacheWiper(): void {
|
|
112
|
+
if (this.wiperId) {
|
|
113
|
+
clearInterval(this.wiperId)
|
|
114
|
+
this.wiperId = null
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @UNUSED_IMPLEMENTATION - Method available for future use
|
|
120
|
+
* Cleanup method to clear intervals when instance is no longer needed
|
|
121
|
+
*/
|
|
122
|
+
destroy(): void {
|
|
123
|
+
this.unregisterCacheWiper()
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { Cache } from './Cacher'
|
|
2
|
+
import { hashKey, minifier } from '../tools'
|
|
3
|
+
import { StyleConstants } from '../constants'
|
|
4
|
+
import { CacheType } from '../types/cache'
|
|
5
|
+
import { StateStorage } from '../types/store'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* StyleCache manages multiple cache instances for different style-related data.
|
|
9
|
+
* Provides centralized caching functionality with compression and key generation.
|
|
10
|
+
*/
|
|
11
|
+
export class StyleCache {
|
|
12
|
+
/** Base key used for cache key generation */
|
|
13
|
+
baseKey: string
|
|
14
|
+
|
|
15
|
+
/** Cache for style data */
|
|
16
|
+
styles = new Cache('styles')
|
|
17
|
+
|
|
18
|
+
/** Cache for style compositions */
|
|
19
|
+
compositions = new Cache('compositions')
|
|
20
|
+
|
|
21
|
+
/** Cache for responsive styles */
|
|
22
|
+
responsive = new Cache('responsive')
|
|
23
|
+
|
|
24
|
+
/** Cache for style variants with persistent storage */
|
|
25
|
+
variants: Cache
|
|
26
|
+
|
|
27
|
+
/** Cache for common styles with persistent storage */
|
|
28
|
+
common: Cache
|
|
29
|
+
|
|
30
|
+
/** Cache for component styles with persistent storage */
|
|
31
|
+
components: Cache
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Creates a new StyleCache instance
|
|
35
|
+
* @param storage - State storage instance for persistent caches
|
|
36
|
+
*/
|
|
37
|
+
constructor(storage: StateStorage) {
|
|
38
|
+
this.variants = new Cache('variants', storage)
|
|
39
|
+
this.common = new Cache('common', storage)
|
|
40
|
+
this.components = new Cache('components', storage)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Registers and generates a base key for cache operations
|
|
45
|
+
* @param keys - Array of values to include in base key generation
|
|
46
|
+
* @returns The generated base key hash
|
|
47
|
+
*/
|
|
48
|
+
registerBaseKey(keys: Array<any>): string {
|
|
49
|
+
keys.push(StyleConstants.STORES_PERSIST_VERSION)
|
|
50
|
+
|
|
51
|
+
const baseKey = hashKey(keys)
|
|
52
|
+
|
|
53
|
+
this.baseKey = baseKey
|
|
54
|
+
|
|
55
|
+
return baseKey
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Clears all cache instances
|
|
60
|
+
*/
|
|
61
|
+
wipeCache(): void {
|
|
62
|
+
this.components.clear()
|
|
63
|
+
this.responsive.clear()
|
|
64
|
+
this.compositions.clear()
|
|
65
|
+
this.variants.clear()
|
|
66
|
+
this.common.clear()
|
|
67
|
+
this.styles.clear()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Generates a cache key and retrieves cached value if exists
|
|
72
|
+
* @param type - The cache type to use
|
|
73
|
+
* @param keyData - Data to use for key generation (array or object)
|
|
74
|
+
* @returns Object containing the generated key and cached value (if any)
|
|
75
|
+
*/
|
|
76
|
+
keyFor(type: CacheType, keyData: Array<any> | any): { key: string; value: any } {
|
|
77
|
+
const cache = this[type]
|
|
78
|
+
|
|
79
|
+
const withFunctionsHash = Object.values(keyData).map((value) => {
|
|
80
|
+
if (typeof value === 'function') {
|
|
81
|
+
return value.toString()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return value
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
const values = [this.baseKey, ...withFunctionsHash]
|
|
88
|
+
|
|
89
|
+
const cacheKey = hashKey(values)
|
|
90
|
+
const cachedValue = minifier.decompress(cache.cache[cacheKey] ?? null)
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
key: cacheKey,
|
|
94
|
+
value: cachedValue,
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Stores a value in the specified cache with compression
|
|
100
|
+
* @param type - The cache type to use
|
|
101
|
+
* @param key - Cache key
|
|
102
|
+
* @param value - Value to cache
|
|
103
|
+
* @returns The cached value (compressed if caching is enabled)
|
|
104
|
+
*/
|
|
105
|
+
cacheFor(type: CacheType, key: string, value: any): any {
|
|
106
|
+
if (!StyleConstants.CACHE_ENABLED) {
|
|
107
|
+
return value
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const cache = this[type]
|
|
111
|
+
|
|
112
|
+
const minifiedValue = minifier.compress(value)
|
|
113
|
+
|
|
114
|
+
return cache.cacheFor(key, minifiedValue)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { StateStorage } from '../types/store'
|
|
2
|
+
import { minifier } from '../tools'
|
|
3
|
+
import { StyleConstants } from '../constants'
|
|
4
|
+
|
|
5
|
+
export type StoragePersistor = {
|
|
6
|
+
set: (key: string, value: any) => void
|
|
7
|
+
get: (key: string) => any
|
|
8
|
+
del: (key: string) => void
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class StylePersistor implements StateStorage {
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new StylePersistor instance with compression capabilities
|
|
14
|
+
* @param {StoragePersistor} storage - The underlying storage implementation
|
|
15
|
+
*/
|
|
16
|
+
constructor(
|
|
17
|
+
private storage: StoragePersistor
|
|
18
|
+
) { }
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Stores a value in the underlying storage with compression
|
|
22
|
+
*
|
|
23
|
+
* @param {string} name - Storage key identifier
|
|
24
|
+
* @param {string} value - Value to store (will be compressed)
|
|
25
|
+
* @returns {void}
|
|
26
|
+
* @throws {Error} If the underlying storage fails to set the value
|
|
27
|
+
*/
|
|
28
|
+
setItem(name: string, value: string): void {
|
|
29
|
+
const minifiedValue = minifier.compress(value)
|
|
30
|
+
|
|
31
|
+
return this.storage.set(name, minifiedValue)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Retrieves and decompresses a value from storage
|
|
36
|
+
* Returns null if key doesn't exist, value is null/undefined, or decompression fails
|
|
37
|
+
*
|
|
38
|
+
* @param {string} name - Storage key identifier
|
|
39
|
+
* @returns {string | null} Decompressed value or null if not found/invalid
|
|
40
|
+
* @throws {Error} If the underlying storage fails to get the value
|
|
41
|
+
*/
|
|
42
|
+
getItem(name: string): string | null {
|
|
43
|
+
const persistedValue = this.storage.get(name)
|
|
44
|
+
|
|
45
|
+
if (StyleConstants.LOG) {
|
|
46
|
+
console.log('StylePersistor [getItem] => ' + name, persistedValue)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return minifier.decompress(persistedValue ?? null)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Removes an item from storage
|
|
54
|
+
*
|
|
55
|
+
* @param {string} name - Storage key identifier
|
|
56
|
+
* @returns {void}
|
|
57
|
+
* @throws {Error} If the underlying storage fails to delete the value
|
|
58
|
+
*/
|
|
59
|
+
removeItem(name: string): void {
|
|
60
|
+
return this.storage.del(name)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
/* eslint-disable dot-notation */
|
|
2
|
-
import { AnyRecord, AnyStyledComponent, ICSS, ITheme, StyleAggregator, StyleProp, VariantStyleSheet } from '../types'
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import { defaultVariants } from '
|
|
7
|
-
import {
|
|
8
|
-
import { capitalize, ignoredStyleKeys, isSpacingKey } from './utils'
|
|
2
|
+
import type { AnyRecord, AnyStyledComponent, ICSS, ITheme, StyleAggregator, StyleProp, VariantStyleSheet } from '../types'
|
|
3
|
+
import type { StateStorage } from '../types/store'
|
|
4
|
+
import type { MultiplierFunction } from '../variants'
|
|
5
|
+
import { themeStore } from '../theme'
|
|
6
|
+
import { defaultVariants, dynamicVariants } from '../variants'
|
|
7
|
+
import { capitalize, ignoredStyleKeys, isSpacingKey } from '../utils'
|
|
9
8
|
import { StyleCache } from './StyleCache'
|
|
10
|
-
import { minifier } from '
|
|
11
|
-
import rfdc from 'rfdc'
|
|
12
|
-
import { StateStorage } from '../types/store'
|
|
13
|
-
|
|
14
|
-
const deepClone = rfdc()
|
|
9
|
+
import { minifier, deepClone, deepmerge } from '../tools'
|
|
15
10
|
|
|
16
11
|
export class CodeleapStyleRegistry {
|
|
17
12
|
stylesheets: Record<string, VariantStyleSheet> = {}
|