Skip to content

Commit

Permalink
safely call timeout.unref() in Node.js environments only (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
sqs authored Jul 31, 2024
1 parent 47ada61 commit f82692a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
4 changes: 2 additions & 2 deletions client/vscode-lib/src/util/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Cache<T> {

this.cache.set(key, { value })
const timeout = setTimeout(() => this.cache.delete(key), this.ttlMs)
timeout.unref()
if (typeof timeout !== 'number' && 'unref' in timeout) timeout.unref()

return value
}
Expand All @@ -33,7 +33,7 @@ export async function bestEffort<T>(
let id: ReturnType<typeof setTimeout> | undefined
const timeout = new Promise<T>((resolve, _) => {
id = setTimeout(resolve, opts.delay, opts.defaultValue)
id.unref()
if (typeof id !== 'number' && 'unref' in id) id.unref()
})
try {
return await Promise.race([promise, timeout])
Expand Down
5 changes: 2 additions & 3 deletions client/vscode-lib/src/util/errorWaiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ export function createErrorWaiter(delay: number, errorCountThreshold: number): E
if (!timeoutHandle) {
return
}
if ('unref' in timeoutHandle) {
if (typeof timeoutHandle !== 'number' && 'unref' in timeoutHandle) {
timeoutHandle.unref()
} else if (typeof timeoutHandle === 'number') {
clearTimeout(timeoutHandle)
}
clearTimeout(timeoutHandle)
},
}
}
2 changes: 1 addition & 1 deletion provider/devdocs/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Cache<T> {

this.cache.set(key, { value })
const timeout = setTimeout(() => this.cache.delete(key), this.timeoutMS)
timeout.unref()
if (typeof timeout !== 'number' && 'unref' in timeout) timeout.unref()

return value
}
Expand Down

0 comments on commit f82692a

Please sign in to comment.