Skip to content

Commit

Permalink
perf: cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Daydreamer-riri committed Dec 2, 2023
1 parent 7a6a375 commit 0fef33a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/commands/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TextEditor, TextEditorEdit } from 'vscode'
import { Range, commands } from 'vscode'
import jsonListener from '../core/listener'
import { clearCache } from '../core/fetcher'

export interface ReplaceItem {
item: string
Expand Down Expand Up @@ -38,8 +39,10 @@ export const replaceVersion = commands.registerTextEditorCommand(
export const reload = commands.registerTextEditorCommand(
'packages.retry',
(editor: TextEditor) => {
if (editor)
if (editor) {
clearCache()
jsonListener(editor)
}
},
)

Expand Down
11 changes: 10 additions & 1 deletion src/core/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import type Item from './Item'
import type Dependency from './Dependency'

// const queue = new PQueue({ concurrency: 10 })
let cache: [Dependency[], Map<string, Dependency[]>] | null = null

export function clearCache() {
cache = null
}

export async function fetchPackageVersions(
dependencies: Item[],
): Promise<[Dependency[], Map<string, Dependency[]>]> {
if (cache)
return cache
statusBarItem.setText('👀 Fetching npm')

const responsesMap: Map<string, Dependency[]> = new Map()
Expand Down Expand Up @@ -70,7 +77,9 @@ export async function fetchPackageVersions(
},
)

return [responses, responsesMap]
cache = [responses, responsesMap]

return cache
}

function fetchPackageData(
Expand Down

0 comments on commit 0fef33a

Please sign in to comment.