瀏覽代碼

Fixes leaked disposable

main
Eric Amodio 2 年之前
父節點
當前提交
dfa5c23644
共有 1 個檔案被更改,包括 7 行新增2 行删除
  1. +7
    -2
      src/system/promise.ts

+ 7
- 2
src/system/promise.ts 查看文件

@ -1,5 +1,5 @@
'use strict';
import { CancellationToken } from 'vscode';
import { CancellationToken, Disposable } from 'vscode';
import { map } from './iterable';
export type PromiseOrValue<T> = Promise<T> | T;
@ -29,6 +29,8 @@ export function cancellable(
return new Promise((resolve, reject) => {
let fulfilled = false;
let timer: ReturnType<typeof setTimeout> | undefined;
let disposable: Disposable | undefined;
if (typeof timeoutOrToken === 'number') {
timer = setTimeout(() => {
if (typeof options.onDidCancel === 'function') {
@ -38,7 +40,8 @@ export function cancellable(
}
}, timeoutOrToken);
} else {
timeoutOrToken.onCancellationRequested(() => {
disposable = timeoutOrToken.onCancellationRequested(() => {
disposable?.dispose();
if (fulfilled) return;
if (typeof options.onDidCancel === 'function') {
@ -55,6 +58,7 @@ export function cancellable(
if (timer != null) {
clearTimeout(timer);
}
disposable?.dispose();
resolve(promise);
},
ex => {
@ -62,6 +66,7 @@ export function cancellable(
if (timer != null) {
clearTimeout(timer);
}
disposable?.dispose();
reject(ex);
},
);

Loading…
取消
儲存