Skip to content

Commit 9600da9

Browse files
authored
refactor: use ocache's built-in invalidate API (#85)
* refactor: use ocache's built-in invalidate API * update
1 parent f09aa27 commit 9600da9

5 files changed

Lines changed: 32 additions & 44 deletions

File tree

extensions/vscode/src/composables/workspace-context.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Uri } from 'vscode'
2-
import { deleteWorkspaceContextCache, getWorkspaceContext } from '#core/workspace'
2+
import { getWorkspaceContext } from '#core/workspace'
33
import { logger } from '#state'
44
import { SUPPORTED_DOCUMENT_PATTERN } from '#utils/constants'
55
import { PACKAGE_JSON_BASENAME } from 'npmx-language-core/constants'
@@ -10,8 +10,7 @@ import { window, workspace } from 'vscode'
1010
export function useWorkspaceContext() {
1111
useDisposable(workspace.onDidChangeWorkspaceFolders(({ removed }) => {
1212
removed.forEach((folder) => {
13-
deleteWorkspaceContextCache(folder)
14-
logger.info(`[workspace-context] delete workspace folder cache: ${folder.uri.path}`)
13+
getWorkspaceContext.invalidate(folder.uri)
1514
})
1615
}))
1716

@@ -23,7 +22,7 @@ export function useWorkspaceContext() {
2322
if (!ctx)
2423
return
2524

26-
ctx.invalidateDependencyInfo(uri)
25+
await ctx.invalidateDependencyInfo(uri)
2726
logger.info(`[workspace-context] invalidate dependencies cache: ${uri.path}`)
2827
if (reload) {
2928
const folderPath = ctx!.folder.uri.path

extensions/vscode/src/core/workspace.ts

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class WorkspaceContext {
5050
packageManager: PackageManager = 'npm'
5151
workspaceFileUri?: Uri
5252
#catalogs?: PromiseWithResolvers<CatalogsInfo | undefined>
53-
#invalidatedPaths = new Set<string>()
5453

5554
private constructor(folder: WorkspaceFolder) {
5655
this.folder = folder
@@ -74,7 +73,7 @@ class WorkspaceContext {
7473
this.workspaceFileUri = Uri.joinPath(this.folder.uri, workspaceFilename)
7574
this.#catalogs.resolve(
7675
await accessOk(this.workspaceFileUri)
77-
? (await this.loadWorkspaceCatalogInfo(this.workspaceFileUri))?.catalogs
76+
? (await this.loadWorkspaceFileInfo(this.workspaceFileUri))?.catalogs
7877
: undefined,
7978
)
8079
} else {
@@ -87,12 +86,6 @@ class WorkspaceContext {
8786
maxAge: 0,
8887
swr: false,
8988
staleMaxAge: 0,
90-
shouldInvalidateCache: (uri) => this.#invalidatedPaths.delete(uri.path),
91-
}
92-
93-
invalidateDependencyInfo(uri: Uri) {
94-
const path = uri.path
95-
this.#invalidatedPaths.add(path)
9689
}
9790

9891
async getCatalogs(): Promise<CatalogsInfo | undefined> {
@@ -154,7 +147,7 @@ class WorkspaceContext {
154147
}
155148
}, this.#cacheOptions)
156149

157-
loadWorkspaceCatalogInfo = defineCachedFunction<
150+
loadWorkspaceFileInfo = defineCachedFunction<
158151
WithDependencyInfo<WorkspaceCatalogInfo> | undefined,
159152
[Uri]
160153
>(async (uri) => {
@@ -178,37 +171,33 @@ class WorkspaceContext {
178171
dependencies: info.dependencies.map((dep) => this.#createResolvedDependencyInfo(dep)),
179172
}
180173
}, this.#cacheOptions)
181-
}
182174

183-
const invalidatedFolderPaths = new Set<string>()
175+
async invalidateDependencyInfo(uri: Uri) {
176+
if (isPackageManifest(uri.path))
177+
await this.loadPackageManifestInfo.invalidate(uri)
178+
else if (isWorkspaceFile(uri.path))
179+
await this.loadWorkspaceFileInfo.invalidate(uri)
180+
}
181+
}
184182

185-
const getWorkspaceContextByFolder = defineCachedFunction<
183+
export const getWorkspaceContext = defineCachedFunction<
186184
WorkspaceContext | undefined,
187-
[WorkspaceFolder]
188-
> (async (folder) => {
185+
[Uri]
186+
>(async (uri) => {
187+
const folder = workspace.getWorkspaceFolder(uri)
188+
if (!folder)
189+
return
190+
189191
logger.info(`[workspace-context] built ${folder.uri.path}`)
190192
return await WorkspaceContext.create(folder)
191193
}, {
192194
name: 'workspace-context',
193-
getKey: (folder) => folder.uri.path,
195+
getKey: (uri) => workspace.getWorkspaceFolder(uri)?.uri.path ?? '',
194196
swr: false,
195197
maxAge: 0,
196198
staleMaxAge: 0,
197-
shouldInvalidateCache: (folder) => invalidatedFolderPaths.delete(folder.uri.path),
198199
})
199200

200-
export function deleteWorkspaceContextCache(folder: WorkspaceFolder) {
201-
invalidatedFolderPaths.add(folder.uri.path)
202-
}
203-
204-
export async function getWorkspaceContext(uri: Uri) {
205-
const folder = workspace.getWorkspaceFolder(uri)
206-
if (!folder)
207-
return
208-
209-
return await getWorkspaceContextByFolder(folder)
210-
}
211-
212201
export async function getResolvedDependencies(uri: Uri): Promise<DependencyInfo[] | undefined> {
213202
const ctx = await getWorkspaceContext(uri)
214203
if (!ctx)
@@ -217,7 +206,7 @@ export async function getResolvedDependencies(uri: Uri): Promise<DependencyInfo[
217206
return (
218207
isPackageManifest(uri.path)
219208
? await ctx.loadPackageManifestInfo(uri)
220-
: await ctx.loadWorkspaceCatalogInfo(uri)
209+
: await ctx.loadWorkspaceFileInfo(uri)
221210
)?.dependencies
222211
}
223212

extensions/vscode/src/providers/definition/catalog.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export class CatalogDefinitionProvider implements DefinitionProvider {
1515
if (!ctx?.workspaceFileUri)
1616
return
1717

18-
const catalogInfo = await ctx.loadWorkspaceCatalogInfo(ctx.workspaceFileUri)
19-
if (!catalogInfo)
18+
const dependencies = (await ctx.loadWorkspaceFileInfo(ctx.workspaceFileUri))?.dependencies
19+
if (!dependencies)
2020
return
2121

22-
const target = catalogInfo.dependencies.find(
22+
const target = dependencies.find(
2323
(dep) =>
2424
dep.rawName === info.resolvedName
2525
&& dep.categoryName != null && info.categoryName != null

pnpm-lock.yaml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ catalogs:
2020
inline:
2121
fast-npm-meta: ^1.4.2
2222
jsonc-parser: ^3.3.1
23-
ocache: ^0.1.2
23+
ocache: ^0.1.4
2424
ofetch: ^2.0.0-alpha.3
2525
pathe: ^2.0.3
2626
perfect-debounce: ^2.1.0

0 commit comments

Comments
 (0)