Disposable Namespace
Package: @esfx/disposable
Indicates an object that has resources that can be explicitly disposed.
Functions
create(dispose)
Creates a Disposable
wrapper around a callback used to dispose of a resource.
NOTE: This is not spec-compliant and will not be standardized. It is preferred to use a DisposableStack
or to implement Disposable.dispose
yourself instead.
Declaration
function create(dispose: () => void): Disposable;
Parameters
- dispose
- () => void
Returns
hasInstance(value)
Determines whether a value is Disposable.
NOTE: This is not spec-compliant and will not be standardized.
Declaration
function hasInstance(value: unknown): value is Disposable;
Parameters
- value
- unknown
Returns
scope()
Emulate using
using for..of
.
NOTE: This is not spec-compliant and will not be standardized.
Declaration
function scope(): Generator<DisposableScope, void, undefined>;
Returns
Examples
// with `using` (proposed)
{
...
using x = expr, y = expr;
...
}
// with `Disposable.scope()`:
for (const { using, fail } of Disposable.scope()) {
try {
...
const x = using(expr), y = using(expr);
...
}
catch (e) {
fail(e);
}
}
usingEach(iterable)
Yields each disposable in the iterable, disposing it when the generator resumes.
This emulates for (using x of expr)
.
NOTE: This is not spec-compliant and will not be standardized.
Declaration
function usingEach(iterable: Iterable<Disposable | null | undefined>): Generator<Disposable | null | undefined, void, unknown>;
Parameters
- iterable
- Iterable<Disposable | null | undefined>
Returns
Examples
// with `using` (proposed)
for (using x of expr) {
...
}
// with `Disposable.usingEach()`:
for (const x of Disposable.usingEach(expr)) {
...
}
Variables
dispose
A well-known symbol used to define an explicit resource disposal method on an object.
NOTE: Uses Symbol.dispose
if present.
Declaration
dispose: DisposeSymbol