Skip to content

Commit e849dba

Browse files
committed
discard cache after 1s for empty result (probably errored)
1 parent 26cc345 commit e849dba

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

src/apiCommands.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { sendCommand } from './sendCommand'
66
export default () => {
77
/** @unique */
88
const cacheableCommands: Set<(typeof passthroughExposedApiCommands)[number]> = new Set(['getNodePath', 'getSpanOfEnclosingComment', 'getNodeAtPosition'])
9-
const operationsCache = new Map<string, { key: string; data }>()
9+
const operationsCache = new Map<string, { key: string; data; time?: number }>()
1010
const sharedRequest = async (type: TriggerCharacterCommand, { offset, relativeOffset = 0, document, position }: RequestOptions) => {
1111
if (position && offset) throw new Error('Only position or offset parameter can be provided')
1212
if (document && !offset && !position) throw new Error('When custom document is provided, offset or position must be provided')
@@ -18,10 +18,11 @@ export default () => {
1818
const requestOffset = offset ?? document.offsetAt(position!)
1919
const requestPos = position ?? document.positionAt(offset!)
2020
const getData = async () => sendCommand(type, { document: document!, position: requestPos })
21+
const CACHE_UNDEFINED_TIMEOUT = 1000
2122
if (cacheableCommands.has(type as any)) {
2223
const cacheEntry = operationsCache.get(type)
2324
const operationKey = `${document.uri.toString()}:${document.version}:${requestOffset}`
24-
if (cacheEntry?.key === operationKey) {
25+
if (cacheEntry?.key === operationKey && cacheEntry?.time && Date.now() - cacheEntry.time < CACHE_UNDEFINED_TIMEOUT) {
2526
return cacheEntry.data
2627
}
2728

@@ -31,7 +32,7 @@ export default () => {
3132
// at the same time:
3233
// extension 2 completion provider requests API data at the same document and position
3334
// and so on
34-
operationsCache.set(type, { key: operationKey, data })
35+
operationsCache.set(type, { key: operationKey, data, time: data === undefined ? Date.now() : undefined })
3536
if (type === 'getNodePath') {
3637
operationsCache.set('getNodeAtPosition', { key: operationKey, data: data.then((path: any) => path?.[path.length - 1]) })
3738
}

0 commit comments

Comments
 (0)