Browse Source

Fixes leaked disposable

main
Eric Amodio 2 years ago
parent
commit
dfa5c23644
1 changed files with 7 additions and 2 deletions
  1. +7
    -2
      src/system/promise.ts

+ 7
- 2
src/system/promise.ts View File

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

Loading…
Cancel
Save