Convert Disposables to typescript

This commit is contained in:
RMidhunSuresh 2021-11-15 13:43:39 +05:30
parent dd74ed1957
commit 7270918b65

View file

@ -25,14 +25,15 @@ function disposeValue(value: Disposable): void {
} }
} }
function isDisposable(value) { function isDisposable(value: Disposable): boolean {
// todo: value can be undefined I think?
return value && (typeof value === "function" || typeof value.dispose === "function"); return value && (typeof value === "function" || typeof value.dispose === "function");
} }
export class Disposables { export class Disposables {
private readonly _disposables = []; private _disposables: Disposable[] | null = [];
track(disposable) { track(disposable: Disposable): Disposable {
if (!isDisposable(disposable)) { if (!isDisposable(disposable)) {
throw new Error("Not a disposable"); throw new Error("Not a disposable");
} }
@ -41,19 +42,23 @@ export class Disposables {
disposeValue(disposable); disposeValue(disposable);
return disposable; return disposable;
} }
this._disposables.push(disposable); this._disposables!.push(disposable);
return disposable; return disposable;
} }
untrack(disposable) { untrack(disposable: Disposable) {
const idx = this._disposables.indexOf(disposable); if (this.isDisposed) {
console.warn("Disposables already disposed, cannot untrack");
return;
}
const idx = this._disposables!.indexOf(disposable);
if (idx >= 0) { if (idx >= 0) {
this._disposables.splice(idx, 1); this._disposables!.splice(idx, 1);
} }
return null; return null;
} }
dispose() { dispose(): void {
if (this._disposables) { if (this._disposables) {
for (const d of this._disposables) { for (const d of this._disposables) {
disposeValue(d); disposeValue(d);
@ -62,17 +67,17 @@ export class Disposables {
} }
} }
get isDisposed() { get isDisposed(): boolean {
return this._disposables === null; return this._disposables === null;
} }
disposeTracked(value) { disposeTracked(value: Disposable): null {
if (value === undefined || value === null || this.isDisposed) { if (value === undefined || value === null || this.isDisposed) {
return null; return null;
} }
const idx = this._disposables.indexOf(value); const idx = this._disposables!.indexOf(value);
if (idx !== -1) { if (idx !== -1) {
const [foundValue] = this._disposables.splice(idx, 1); const [foundValue] = this._disposables!.splice(idx, 1);
disposeValue(foundValue); disposeValue(foundValue);
} else { } else {
console.warn("disposable not found, did it leak?", value); console.warn("disposable not found, did it leak?", value);