Skip to content

Commit

Permalink
refactor: cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-He95 committed Sep 21, 2023
1 parent 411cb93 commit 912b089
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/vscode/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function activate(ext: ExtensionContext) {
}
// now if the root is an array, then it is an empty array

const cacheMap = new Set()
const cacheMap = new Set<string>()
const contextCache = new Map()
const watchConfigMap = ['uno.config.js', 'uno.config.ts', 'unocss.config.js', 'unocss.config.ts']
const useWatcherUnoConfig = (configUrl: string) => {
Expand All @@ -89,15 +89,19 @@ export async function activate(ext: ExtensionContext) {
watcher.dispose()
}))

return watcher
return () => watcher.dispose()
}

const hasCache = (url: string) => {
return [...cacheMap].find(cache => url.startsWith(cache))
}

const registerUnocss = async () => {
const url = window.activeTextEditor?.document.uri.fsPath
if (!url)
return

if (cacheMap.has(url))
if (hasCache(url))
return

const defaultExclude = exclude && exclude.length ? exclude : /[\/](node_modules|dist|\.temp|\.cache|\.vscode)[\/]/
Expand All @@ -112,9 +116,8 @@ export async function activate(ext: ExtensionContext) {
if (!configUrl || cacheMap.has(configUrl))
return

cacheMap.add(url)
cacheMap.add(configUrl)
const cwd = path.dirname(configUrl)
cacheMap.add(cwd)

const contextLoader = await registerRoot(ext, status, cwd)
const reload = async () => {
Expand All @@ -127,11 +130,13 @@ export async function activate(ext: ExtensionContext) {
contextLoader.unload(configDir)
log.appendLine('✅ unloaded.')
}
const dispose = useWatcherUnoConfig(configUrl)

contextCache.set(configUrl, {
reload,
unload,
dispose,
})
useWatcherUnoConfig(configUrl)
ext.subscriptions.push(
commands.registerCommand('unocss.reload', reload),
)
Expand Down

0 comments on commit 912b089

Please sign in to comment.