@tsdotnet/disposable
v2.1.2
Published
A disposable base class and minimal set of interfaces to properly implement an object disposal pattern.
Maintainers
Readme
Base classes for objects that own resources: cleanup runs once, and use after disposal throws.
Install
npm install @tsdotnet/disposableUsage
import { DisposableBase } from '@tsdotnet/disposable';
class Connection extends DisposableBase {
constructor (private readonly socket: WebSocket) { super(); }
send (data: string): void {
this.assertIsAlive(); // throws once disposed
this.socket.send(data);
}
protected override _onDispose (): void {
this.socket.close(); // runs once, however often dispose() is called
}
}Async cleanup: extend AsyncDisposableBase. Several at once: dispose(a, b, c).
Use it from
ESM (import { DisposableBase } from '@tsdotnet/disposable';) or CommonJS (const { DisposableBase } = require('@tsdotnet/disposable');).
<script src="https://cdn.jsdelivr.net/npm/@tsdotnet/disposable@2/dist/umd/disposable.min.js"></script>
<script>
const { DisposableBase } = tsdotnet.disposable;
</script>