Fix cleanup after getting message from vscode

This commit is contained in:
Asher 2020-11-12 11:16:21 -06:00
parent 79478eb89f
commit 72931edcf0
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
1 changed files with 13 additions and 8 deletions

View File

@ -123,8 +123,19 @@ export class VscodeProvider {
fn: (message: ipc.VscodeMessage) => message is T, fn: (message: ipc.VscodeMessage) => message is T,
): Promise<T> { ): Promise<T> {
return new Promise((resolve, _reject) => { return new Promise((resolve, _reject) => {
const reject = (error: Error) => { const cleanup = () => {
proc.off("error", reject)
proc.off("exit", onExit)
clearTimeout(timeout) clearTimeout(timeout)
}
const timeout = setTimeout(() => {
cleanup()
_reject(new Error("timed out"))
}, this.timeoutInterval)
const reject = (error: Error) => {
cleanup()
_reject(error) _reject(error)
} }
@ -132,16 +143,10 @@ export class VscodeProvider {
reject(new Error(`VS Code exited unexpectedly with code ${code}`)) reject(new Error(`VS Code exited unexpectedly with code ${code}`))
} }
const timeout = setTimeout(() => {
reject(new Error("timed out"))
}, this.timeoutInterval)
proc.on("message", (message: ipc.VscodeMessage) => { proc.on("message", (message: ipc.VscodeMessage) => {
logger.debug("got message from vscode", field("message", message)) logger.debug("got message from vscode", field("message", message))
proc.off("error", reject)
proc.off("exit", onExit)
if (fn(message)) { if (fn(message)) {
clearTimeout(timeout) cleanup()
resolve(message) resolve(message)
} }
}) })