Skip to content

Commit

Permalink
feat(core): expose cache on generator
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 25, 2024
1 parent 10dbd4c commit 6990846
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
3 changes: 1 addition & 2 deletions packages/autocomplete/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ export function createAutocomplete(
}

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

Expand Down
25 changes: 13 additions & 12 deletions packages/core/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ export const symbols: ControlSymbols = {
}

class UnoGeneratorInternal<Theme extends object = object> {
public version = version
private _cache = new Map<string, StringifiedUtil<Theme>[] | null>()
public readonly version = version
public readonly events = createNanoEvents<{
config: (config: ResolvedConfig<Theme>) => void
}>()

public config: ResolvedConfig<Theme> = undefined!
public cache = new Map<string, StringifiedUtil<Theme>[] | null>()
public blocked = new Set<string>()
public parentOrders = new Map<string, number>()
public activatedRules = new Set<Rule<Theme>>()
public events = createNanoEvents<{
config: (config: ResolvedConfig<Theme>) => void
}>()

protected constructor(
public userConfig: UserConfig<Theme> = {},
Expand Down Expand Up @@ -52,7 +53,7 @@ class UnoGeneratorInternal<Theme extends object = object> {
this.blocked.clear()
this.parentOrders.clear()
this.activatedRules.clear()
this._cache.clear()
this.cache.clear()
this.config = await resolveConfig(userConfig, this.defaults)
this.events.emit('config', this.config)
}
Expand Down Expand Up @@ -123,24 +124,24 @@ class UnoGeneratorInternal<Theme extends object = object> {
const cacheKey = `${raw}${alias ? ` ${alias}` : ''}`

// use caches if possible
if (this._cache.has(cacheKey))
return this._cache.get(cacheKey)
if (this.cache.has(cacheKey))
return this.cache.get(cacheKey)

let current = raw
for (const p of this.config.preprocess)
current = p(raw)!

if (this.isBlocked(current)) {
this.blocked.add(raw)
this._cache.set(cacheKey, null)
this.cache.set(cacheKey, null)
return
}

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

if (variantResults.every(i => !i || this.isBlocked(i[1]))) {
this.blocked.add(raw)
this._cache.set(cacheKey, null)
this.cache.set(cacheKey, null)
return
}

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

const result = (await Promise.all(variantResults.map(i => handleVariantResult(i)))).flat().filter(x => !!x)
if (result?.length) {
this._cache.set(cacheKey, result)
this.cache.set(cacheKey, result)
return result
}

// set null cache for unmatched result
this._cache.set(cacheKey, null)
this.cache.set(cacheKey, null)
}

generate(
Expand Down

0 comments on commit 6990846

Please sign in to comment.