2020-04-09 23:19:49 +02:00
|
|
|
function disposeValue(value) {
|
|
|
|
if (typeof d === "function") {
|
|
|
|
value();
|
|
|
|
} else {
|
|
|
|
value.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Disposables {
|
|
|
|
constructor() {
|
|
|
|
this._disposables = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
track(disposable) {
|
|
|
|
this._disposables.push(disposable);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
if (this._disposables) {
|
|
|
|
for (const d of this._disposables) {
|
|
|
|
disposeValue(d);
|
|
|
|
}
|
|
|
|
this._disposables = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disposeTracked(value) {
|
2020-05-05 23:19:02 +02:00
|
|
|
if (value === undefined || value === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-04-09 23:19:49 +02:00
|
|
|
const idx = this._disposables.indexOf(value);
|
|
|
|
if (idx !== -1) {
|
|
|
|
const [foundValue] = this._disposables.splice(idx, 1);
|
|
|
|
disposeValue(foundValue);
|
2020-05-05 23:19:02 +02:00
|
|
|
} else {
|
|
|
|
console.warn("disposable not found, did it leak?", value);
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
2020-05-05 23:19:02 +02:00
|
|
|
return null;
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
|
|
|
}
|