first commit

This commit is contained in:
douboer@gmail.com
2026-03-03 13:23:14 +08:00
commit 3b7c1d558a
161 changed files with 28120 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
import type { IDisposable } from "../types";
/**
* Disposables
*
* https://blog.hediet.de/post/the_disposable_pattern_in_typescript
*
* https://github.com/xtermjs/xterm.js/blob/master/src/common/Lifecycle.ts
*/
export default class Disposable implements IDisposable {
// private store for states
#disposables: IDisposable[];
public isDisposed: boolean;
constructor() {
this.isDisposed = false;
this.#disposables = [];
}
/**
* Registers a disposable object
* @param d The disposable to register
*/
public register<T extends IDisposable>(d: T): void {
if (this.isDisposed) {
d?.dispose();
} else {
this.#disposables.push(d);
}
}
/**
* Disposes the object, triggering the `dispose` method on all registered disposables
*/
public dispose(): void {
if (this.isDisposed) return;
this.isDisposed = true;
this.#disposables.forEach((d) => d?.dispose());
}
}