-
-
Notifications
You must be signed in to change notification settings - Fork 13
refactor: migrate to ocache #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import type { ModuleReplacement } from 'module-replacements' | ||
| import { CACHE_MAX_AGE_ONE_DAY, NPMX_DEV_API } from '#constants' | ||
| import { logger } from '#state' | ||
| import { encodePackageName } from '#utils/package' | ||
| import { defineCachedFunction } from 'ocache' | ||
| import { ofetch } from 'ofetch' | ||
|
|
||
| export const getReplacement = defineCachedFunction<ModuleReplacement | null, [string]>(async (name) => { | ||
| logger.info(`[replacement] fetching for ${name}`) | ||
| const encodedName = encodePackageName(name) | ||
|
|
||
| const result = await ofetch<ModuleReplacement | undefined>(`${NPMX_DEV_API}/replacements/${encodedName}`, { | ||
| ignoreResponseError: true, | ||
| }) ?? null | ||
| logger.info(`[replacement] fetched for ${name}`) | ||
|
|
||
| return result | ||
| }, { | ||
| name: 'replacement', | ||
| getKey: (name) => name, | ||
| maxAge: CACHE_MAX_AGE_ONE_DAY, | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| import type { CatalogsInfo, PackageManager, ResolvedDependencyInfo } from '#types/context' | ||
| import type { DependencyInfo, PackageManifestInfo, WorkspaceCatalogInfo } from '#types/extractor' | ||
| import type { MemoizeOptions } from '#utils/memoize' | ||
| import type { CacheOptions } from 'ocache' | ||
| import type { WorkspaceFolder } from 'vscode' | ||
| import { getPackageInfo } from '#api/package' | ||
| import { logger } from '#state' | ||
| import { getPackageInfo } from '#utils/api/package' | ||
| import { isOffsetInRange } from '#utils/ast' | ||
| import { resolveDependencySpec } from '#utils/dependency' | ||
| import { getDocumentText, isPackageManifestPath, isWorkspaceFilePath } from '#utils/file' | ||
| import { memoize } from '#utils/memoize' | ||
| import { resolveExactVersion } from '#utils/package' | ||
| import { detectPackageManager, workspaceFileMapping } from '#utils/package-manager' | ||
| import { lazyInit } from '#utils/shared' | ||
| import { defineCachedFunction } from 'ocache' | ||
| import { Uri, workspace } from 'vscode' | ||
| import { accessOk } from 'vscode-find-up' | ||
| import { getExtractor } from './extractors' | ||
|
|
@@ -23,6 +23,7 @@ class WorkspaceContext { | |
| folder: WorkspaceFolder | ||
| packageManager: PackageManager = 'npm' | ||
| #catalogs?: PromiseWithResolvers<CatalogsInfo | undefined> | ||
| #invalidatedPaths = new Set<string>() | ||
|
|
||
| private constructor(folder: WorkspaceFolder) { | ||
| this.folder = folder | ||
|
|
@@ -53,11 +54,17 @@ class WorkspaceContext { | |
| } | ||
| } | ||
|
|
||
| #memoizeOptions: MemoizeOptions<Uri> = { | ||
| #cacheOptions: CacheOptions<any, [Uri]> = { | ||
| getKey: (uri) => uri.path, | ||
| ttl: false, | ||
| maxSize: Number.POSITIVE_INFINITY, | ||
| fallbackToCachedOnError: false, | ||
| maxAge: 0, | ||
| swr: false, | ||
| staleMaxAge: 0, | ||
| shouldInvalidateCache: (uri) => this.#invalidatedPaths.delete(uri.path), | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| invalidateDependencyInfo(uri: Uri) { | ||
| const path = uri.path | ||
| this.#invalidatedPaths.add(path) | ||
| } | ||
|
Comment on lines
+57
to
68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Invalidate manifest caches when the workspace catalogue changes.
Possible fix class WorkspaceContext {
folder: WorkspaceFolder
packageManager: PackageManager = 'npm'
`#catalogs`?: PromiseWithResolvers<CatalogsInfo | undefined>
`#invalidatedPaths` = new Set<string>()
+ `#manifestCacheVersion` = 0
...
invalidateDependencyInfo(uri: Uri) {
const path = uri.path
this.#invalidatedPaths.add(path)
+ if (isWorkspaceFilePath(path))
+ this.#manifestCacheVersion++
}
...
loadPackageManifestInfo = defineCachedFunction<
WithResolvedDependencyInfo<PackageManifestInfo> | undefined,
[Uri]
>(async (uri) => {
const path = uri.path
if (!isPackageManifestPath(path))
return
...
return {
...info,
dependencies: info.dependencies.map((dep) => this.#createResolvedDependencyInfo(dep, catalogs)),
}
- }, this.#cacheOptions)
+ }, {
+ ...this.#cacheOptions,
+ getKey: (uri) => `${this.#manifestCacheVersion}:${uri.path}`,
+ })Also applies to: 97-123 |
||
|
|
||
| #createResolvedDependencyInfo(dependency: DependencyInfo, catalogs?: CatalogsInfo): ResolvedDependencyInfo { | ||
|
|
@@ -87,9 +94,9 @@ class WorkspaceContext { | |
| } | ||
| } | ||
|
|
||
| loadPackageManifestInfo = memoize< | ||
| Uri, | ||
| Promise<WithResolvedDependencyInfo<PackageManifestInfo> | undefined> | ||
| loadPackageManifestInfo = defineCachedFunction< | ||
| WithResolvedDependencyInfo<PackageManifestInfo> | undefined, | ||
| [Uri] | ||
| >(async (uri) => { | ||
| const path = uri.path | ||
| if (!isPackageManifestPath(path)) | ||
|
|
@@ -113,11 +120,11 @@ class WorkspaceContext { | |
| ...info, | ||
| dependencies: info.dependencies.map((dep) => this.#createResolvedDependencyInfo(dep, catalogs)), | ||
| } | ||
| }, this.#memoizeOptions) | ||
| }, this.#cacheOptions) | ||
|
|
||
| loadWorkspaceCatalogInfo = memoize< | ||
| Uri, | ||
| Promise<WithResolvedDependencyInfo<WorkspaceCatalogInfo> | undefined> | ||
| loadWorkspaceCatalogInfo = defineCachedFunction< | ||
| WithResolvedDependencyInfo<WorkspaceCatalogInfo> | undefined, | ||
| [Uri] | ||
| >(async (uri) => { | ||
| const path = uri.path | ||
| if (!isWorkspaceFilePath(path)) | ||
|
|
@@ -138,20 +145,28 @@ class WorkspaceContext { | |
| ...info, | ||
| dependencies: info.dependencies.map((dep) => this.#createResolvedDependencyInfo(dep)), | ||
| } | ||
| }, this.#memoizeOptions) | ||
| }, this.#cacheOptions) | ||
| } | ||
|
|
||
| const getWorkspaceContextByFolder = memoize<WorkspaceFolder, Promise<WorkspaceContext | undefined>>(async (folder) => { | ||
| const invalidatedFolderPaths = new Set<string>() | ||
|
|
||
| const getWorkspaceContextByFolder = defineCachedFunction< | ||
| WorkspaceContext | undefined, | ||
| [WorkspaceFolder] | ||
| > (async (folder) => { | ||
| logger.info(`[workspace-context] built ${folder.uri.path}`) | ||
| return await WorkspaceContext.create(folder) | ||
| }, { | ||
| name: 'workspace-context', | ||
| getKey: (folder) => folder.uri.path, | ||
| ttl: false, | ||
| fallbackToCachedOnError: false, | ||
| swr: false, | ||
| maxAge: 0, | ||
| staleMaxAge: 0, | ||
| shouldInvalidateCache: (folder) => invalidatedFolderPaths.delete(folder.uri.path), | ||
| }) | ||
|
|
||
| export function deleteWorkspaceContextCache(folder: WorkspaceFolder) { | ||
| getWorkspaceContextByFolder.delete(folder) | ||
| invalidatedFolderPaths.add(folder.uri.path) | ||
| } | ||
|
|
||
| export async function getWorkspaceContext(uri: Uri) { | ||
|
|
||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.