Skip to content

Commit 6990846

Browse files
committed
feat(core): expose cache on generator
1 parent 10dbd4c commit 6990846

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

packages/autocomplete/src/create.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ export function createAutocomplete(
174174
}
175175

176176
async function suggestUnoCache(input: string) {
177-
// @ts-expect-error private
178-
const keys = Array.from(uno._cache.entries())
177+
const keys = Array.from(uno.cache.entries())
179178
return keys.filter(i => i[1] && i[0].startsWith(input)).map(i => i[0])
180179
}
181180

packages/core/src/generator.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ export const symbols: ControlSymbols = {
1515
}
1616

1717
class UnoGeneratorInternal<Theme extends object = object> {
18-
public version = version
19-
private _cache = new Map<string, StringifiedUtil<Theme>[] | null>()
18+
public readonly version = version
19+
public readonly events = createNanoEvents<{
20+
config: (config: ResolvedConfig<Theme>) => void
21+
}>()
22+
2023
public config: ResolvedConfig<Theme> = undefined!
24+
public cache = new Map<string, StringifiedUtil<Theme>[] | null>()
2125
public blocked = new Set<string>()
2226
public parentOrders = new Map<string, number>()
2327
public activatedRules = new Set<Rule<Theme>>()
24-
public events = createNanoEvents<{
25-
config: (config: ResolvedConfig<Theme>) => void
26-
}>()
2728

2829
protected constructor(
2930
public userConfig: UserConfig<Theme> = {},
@@ -52,7 +53,7 @@ class UnoGeneratorInternal<Theme extends object = object> {
5253
this.blocked.clear()
5354
this.parentOrders.clear()
5455
this.activatedRules.clear()
55-
this._cache.clear()
56+
this.cache.clear()
5657
this.config = await resolveConfig(userConfig, this.defaults)
5758
this.events.emit('config', this.config)
5859
}
@@ -123,24 +124,24 @@ class UnoGeneratorInternal<Theme extends object = object> {
123124
const cacheKey = `${raw}${alias ? ` ${alias}` : ''}`
124125

125126
// use caches if possible
126-
if (this._cache.has(cacheKey))
127-
return this._cache.get(cacheKey)
127+
if (this.cache.has(cacheKey))
128+
return this.cache.get(cacheKey)
128129

129130
let current = raw
130131
for (const p of this.config.preprocess)
131132
current = p(raw)!
132133

133134
if (this.isBlocked(current)) {
134135
this.blocked.add(raw)
135-
this._cache.set(cacheKey, null)
136+
this.cache.set(cacheKey, null)
136137
return
137138
}
138139

139140
const variantResults = await this.matchVariants(raw, current)
140141

141142
if (variantResults.every(i => !i || this.isBlocked(i[1]))) {
142143
this.blocked.add(raw)
143-
this._cache.set(cacheKey, null)
144+
this.cache.set(cacheKey, null)
144145
return
145146
}
146147

@@ -162,12 +163,12 @@ class UnoGeneratorInternal<Theme extends object = object> {
162163

163164
const result = (await Promise.all(variantResults.map(i => handleVariantResult(i)))).flat().filter(x => !!x)
164165
if (result?.length) {
165-
this._cache.set(cacheKey, result)
166+
this.cache.set(cacheKey, result)
166167
return result
167168
}
168169

169170
// set null cache for unmatched result
170-
this._cache.set(cacheKey, null)
171+
this.cache.set(cacheKey, null)
171172
}
172173

173174
generate(

0 commit comments

Comments
 (0)