diff --git a/extensions/vscode/src/composables/workspace-context.ts b/extensions/vscode/src/composables/workspace-context.ts index 01f5dc3..f95d44a 100644 --- a/extensions/vscode/src/composables/workspace-context.ts +++ b/extensions/vscode/src/composables/workspace-context.ts @@ -1,5 +1,5 @@ import type { Uri } from 'vscode' -import { deleteWorkspaceContextCache, getWorkspaceContext } from '#core/workspace' +import { getWorkspaceContext } from '#core/workspace' import { logger } from '#state' import { SUPPORTED_DOCUMENT_PATTERN } from '#utils/constants' import { PACKAGE_JSON_BASENAME } from 'npmx-language-core/constants' @@ -10,8 +10,7 @@ import { window, workspace } from 'vscode' export function useWorkspaceContext() { useDisposable(workspace.onDidChangeWorkspaceFolders(({ removed }) => { removed.forEach((folder) => { - deleteWorkspaceContextCache(folder) - logger.info(`[workspace-context] delete workspace folder cache: ${folder.uri.path}`) + getWorkspaceContext.invalidate(folder.uri) }) })) @@ -23,7 +22,7 @@ export function useWorkspaceContext() { if (!ctx) return - ctx.invalidateDependencyInfo(uri) + await ctx.invalidateDependencyInfo(uri) logger.info(`[workspace-context] invalidate dependencies cache: ${uri.path}`) if (reload) { const folderPath = ctx!.folder.uri.path diff --git a/extensions/vscode/src/core/workspace.ts b/extensions/vscode/src/core/workspace.ts index 9bb7254..4fbe347 100644 --- a/extensions/vscode/src/core/workspace.ts +++ b/extensions/vscode/src/core/workspace.ts @@ -50,7 +50,6 @@ class WorkspaceContext { packageManager: PackageManager = 'npm' workspaceFileUri?: Uri #catalogs?: PromiseWithResolvers - #invalidatedPaths = new Set() private constructor(folder: WorkspaceFolder) { this.folder = folder @@ -74,7 +73,7 @@ class WorkspaceContext { this.workspaceFileUri = Uri.joinPath(this.folder.uri, workspaceFilename) this.#catalogs.resolve( await accessOk(this.workspaceFileUri) - ? (await this.loadWorkspaceCatalogInfo(this.workspaceFileUri))?.catalogs + ? (await this.loadWorkspaceFileInfo(this.workspaceFileUri))?.catalogs : undefined, ) } else { @@ -87,12 +86,6 @@ class WorkspaceContext { maxAge: 0, swr: false, staleMaxAge: 0, - shouldInvalidateCache: (uri) => this.#invalidatedPaths.delete(uri.path), - } - - invalidateDependencyInfo(uri: Uri) { - const path = uri.path - this.#invalidatedPaths.add(path) } async getCatalogs(): Promise { @@ -154,7 +147,7 @@ class WorkspaceContext { } }, this.#cacheOptions) - loadWorkspaceCatalogInfo = defineCachedFunction< + loadWorkspaceFileInfo = defineCachedFunction< WithDependencyInfo | undefined, [Uri] >(async (uri) => { @@ -178,37 +171,33 @@ class WorkspaceContext { dependencies: info.dependencies.map((dep) => this.#createResolvedDependencyInfo(dep)), } }, this.#cacheOptions) -} -const invalidatedFolderPaths = new Set() + async invalidateDependencyInfo(uri: Uri) { + if (isPackageManifest(uri.path)) + await this.loadPackageManifestInfo.invalidate(uri) + else if (isWorkspaceFile(uri.path)) + await this.loadWorkspaceFileInfo.invalidate(uri) + } +} -const getWorkspaceContextByFolder = defineCachedFunction< +export const getWorkspaceContext = defineCachedFunction< WorkspaceContext | undefined, - [WorkspaceFolder] -> (async (folder) => { + [Uri] +>(async (uri) => { + const folder = workspace.getWorkspaceFolder(uri) + if (!folder) + return + logger.info(`[workspace-context] built ${folder.uri.path}`) return await WorkspaceContext.create(folder) }, { name: 'workspace-context', - getKey: (folder) => folder.uri.path, + getKey: (uri) => workspace.getWorkspaceFolder(uri)?.uri.path ?? '', swr: false, maxAge: 0, staleMaxAge: 0, - shouldInvalidateCache: (folder) => invalidatedFolderPaths.delete(folder.uri.path), }) -export function deleteWorkspaceContextCache(folder: WorkspaceFolder) { - invalidatedFolderPaths.add(folder.uri.path) -} - -export async function getWorkspaceContext(uri: Uri) { - const folder = workspace.getWorkspaceFolder(uri) - if (!folder) - return - - return await getWorkspaceContextByFolder(folder) -} - export async function getResolvedDependencies(uri: Uri): Promise { const ctx = await getWorkspaceContext(uri) if (!ctx) @@ -217,7 +206,7 @@ export async function getResolvedDependencies(uri: Uri): Promise dep.rawName === info.resolvedName && dep.categoryName != null && info.categoryName != null diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e280f52..1d64e87 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -44,8 +44,8 @@ catalogs: specifier: ^3.3.1 version: 3.3.1 ocache: - specifier: ^0.1.2 - version: 0.1.2 + specifier: ^0.1.4 + version: 0.1.4 ofetch: specifier: ^2.0.0-alpha.3 version: 2.0.0-alpha.3 @@ -141,7 +141,7 @@ importers: version: link:../../packages/language-core ocache: specifier: catalog:inline - version: 0.1.2 + version: 0.1.4 perfect-debounce: specifier: catalog:inline version: 2.1.0 @@ -162,7 +162,7 @@ importers: dependencies: ocache: specifier: catalog:inline - version: 0.1.2 + version: 0.1.4 ofetch: specifier: catalog:inline version: 2.0.0-alpha.3 @@ -1872,8 +1872,8 @@ packages: obug@2.1.1: resolution: {integrity: sha512-uTqF9MuPraAQ+IsnPf366RG4cP9RtUi7MLO1N3KEc+wb0a6yKpeL0lmk2IB1jY5KHPAlTc6T/JRdC/YqxHNwkQ==} - ocache@0.1.2: - resolution: {integrity: sha512-lI34wjM7cahEdrq2I5obbF7MEdE97vULf6vNj6ZCzwEadzyXO1w7QOl2qzzG4IL8cyO7wDtXPj9CqW/aG3mn7g==} + ocache@0.1.4: + resolution: {integrity: sha512-e7geNdWjxSnvsSgvLuPvgKgu7ubM10ZmTPOgpr7mz2BXYtvjMKTiLhjFi/gWU8chkuP6hNkZBsa9LzOusyaqkQ==} ofetch@2.0.0-alpha.3: resolution: {integrity: sha512-zpYTCs2byOuft65vI3z43Dd6iSdFbOZZLb9/d21aCpx2rGastVU9dOCv0lu4ykc1Ur1anAYjDi3SUvR0vq50JA==} @@ -4199,7 +4199,7 @@ snapshots: obug@2.1.1: {} - ocache@0.1.2: + ocache@0.1.4: dependencies: ohash: 2.0.11 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index c76eaab..32f7ca7 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -20,7 +20,7 @@ catalogs: inline: fast-npm-meta: ^1.4.2 jsonc-parser: ^3.3.1 - ocache: ^0.1.2 + ocache: ^0.1.4 ofetch: ^2.0.0-alpha.3 pathe: ^2.0.3 perfect-debounce: ^2.1.0