2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-11-15 13:08:32 +05:30
|
|
|
type func = () => void;
|
|
|
|
type Disposable = { dispose: func; [key: string]: any } | func;
|
|
|
|
|
|
|
|
function disposeValue(value: Disposable): void {
|
2020-05-07 03:01:36 +05:30
|
|
|
if (typeof value === "function") {
|
2020-04-10 02:49:49 +05:30
|
|
|
value();
|
|
|
|
} else {
|
|
|
|
value.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 13:43:39 +05:30
|
|
|
function isDisposable(value: Disposable): boolean {
|
|
|
|
// todo: value can be undefined I think?
|
2020-10-09 20:31:54 +05:30
|
|
|
return value && (typeof value === "function" || typeof value.dispose === "function");
|
|
|
|
}
|
|
|
|
|
2020-04-10 02:49:49 +05:30
|
|
|
export class Disposables {
|
2021-11-15 13:43:39 +05:30
|
|
|
private _disposables: Disposable[] | null = [];
|
2020-04-10 02:49:49 +05:30
|
|
|
|
2021-11-15 13:43:39 +05:30
|
|
|
track(disposable: Disposable): Disposable {
|
2020-10-09 20:31:54 +05:30
|
|
|
if (!isDisposable(disposable)) {
|
|
|
|
throw new Error("Not a disposable");
|
|
|
|
}
|
2021-02-22 15:17:17 +05:30
|
|
|
if (this.isDisposed) {
|
|
|
|
console.warn("Disposables already disposed, disposing new value");
|
|
|
|
disposeValue(disposable);
|
|
|
|
return disposable;
|
|
|
|
}
|
2021-11-15 13:43:39 +05:30
|
|
|
this._disposables!.push(disposable);
|
2020-09-10 20:10:30 +05:30
|
|
|
return disposable;
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
|
|
|
|
2021-11-15 13:43:39 +05:30
|
|
|
untrack(disposable: Disposable) {
|
|
|
|
if (this.isDisposed) {
|
|
|
|
console.warn("Disposables already disposed, cannot untrack");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const idx = this._disposables!.indexOf(disposable);
|
2020-10-07 15:55:03 +05:30
|
|
|
if (idx >= 0) {
|
2021-11-15 13:43:39 +05:30
|
|
|
this._disposables!.splice(idx, 1);
|
2020-10-07 15:55:03 +05:30
|
|
|
}
|
2020-10-13 16:40:27 +05:30
|
|
|
return null;
|
2020-10-07 15:55:03 +05:30
|
|
|
}
|
|
|
|
|
2021-11-15 13:43:39 +05:30
|
|
|
dispose(): void {
|
2020-04-10 02:49:49 +05:30
|
|
|
if (this._disposables) {
|
|
|
|
for (const d of this._disposables) {
|
|
|
|
disposeValue(d);
|
|
|
|
}
|
|
|
|
this._disposables = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 13:43:39 +05:30
|
|
|
get isDisposed(): boolean {
|
2020-09-10 21:13:01 +05:30
|
|
|
return this._disposables === null;
|
|
|
|
}
|
|
|
|
|
2021-11-15 13:43:39 +05:30
|
|
|
disposeTracked(value: Disposable): null {
|
2020-09-10 21:13:01 +05:30
|
|
|
if (value === undefined || value === null || this.isDisposed) {
|
2020-05-06 02:49:02 +05:30
|
|
|
return null;
|
|
|
|
}
|
2021-11-15 13:43:39 +05:30
|
|
|
const idx = this._disposables!.indexOf(value);
|
2020-04-10 02:49:49 +05:30
|
|
|
if (idx !== -1) {
|
2021-11-15 13:43:39 +05:30
|
|
|
const [foundValue] = this._disposables!.splice(idx, 1);
|
2020-04-10 02:49:49 +05:30
|
|
|
disposeValue(foundValue);
|
2020-05-06 02:49:02 +05:30
|
|
|
} else {
|
|
|
|
console.warn("disposable not found, did it leak?", value);
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
2020-05-06 02:49:02 +05:30
|
|
|
return null;
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
|
|
|
}
|