mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 13:00:58 +00:00
Prevent using disposed of timers (#213271)
* Prevent using disposed of timers If a timer has been disposed of, warn and don't schedule the callback. This is needed as otherwise the timer very likely ends up being leaked * Throw errors instead
This commit is contained in:
parent
21df88c9b4
commit
79d8e9809e
|
@ -880,6 +880,7 @@ export class ResourceQueue implements IDisposable {
|
|||
|
||||
export class TimeoutTimer implements IDisposable {
|
||||
private _token: any;
|
||||
private _isDisposed = false;
|
||||
|
||||
constructor();
|
||||
constructor(runner: () => void, timeout: number);
|
||||
|
@ -893,6 +894,7 @@ export class TimeoutTimer implements IDisposable {
|
|||
|
||||
dispose(): void {
|
||||
this.cancel();
|
||||
this._isDisposed = true;
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
|
@ -903,6 +905,10 @@ export class TimeoutTimer implements IDisposable {
|
|||
}
|
||||
|
||||
cancelAndSet(runner: () => void, timeout: number): void {
|
||||
if (this._isDisposed) {
|
||||
throw new BugIndicatingError(`Calling 'cancelAndSet' on a disposed TimeoutTimer`);
|
||||
}
|
||||
|
||||
this.cancel();
|
||||
this._token = setTimeout(() => {
|
||||
this._token = -1;
|
||||
|
@ -911,6 +917,10 @@ export class TimeoutTimer implements IDisposable {
|
|||
}
|
||||
|
||||
setIfNotSet(runner: () => void, timeout: number): void {
|
||||
if (this._isDisposed) {
|
||||
throw new BugIndicatingError(`Calling 'setIfNotSet' on a disposed TimeoutTimer`);
|
||||
}
|
||||
|
||||
if (this._token !== -1) {
|
||||
// timer is already set
|
||||
return;
|
||||
|
@ -925,6 +935,7 @@ export class TimeoutTimer implements IDisposable {
|
|||
export class IntervalTimer implements IDisposable {
|
||||
|
||||
private disposable: IDisposable | undefined = undefined;
|
||||
private isDisposed = false;
|
||||
|
||||
cancel(): void {
|
||||
this.disposable?.dispose();
|
||||
|
@ -932,6 +943,10 @@ export class IntervalTimer implements IDisposable {
|
|||
}
|
||||
|
||||
cancelAndSet(runner: () => void, interval: number, context = globalThis): void {
|
||||
if (this.isDisposed) {
|
||||
throw new BugIndicatingError(`Calling 'cancelAndSet' on a disposed IntervalTimer`);
|
||||
}
|
||||
|
||||
this.cancel();
|
||||
const handle = context.setInterval(() => {
|
||||
runner();
|
||||
|
@ -945,6 +960,7 @@ export class IntervalTimer implements IDisposable {
|
|||
|
||||
dispose(): void {
|
||||
this.cancel();
|
||||
this.isDisposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue