@dandi/common
v1.0.0-alpha.77
Published
`@dandi/common` provides common types and utilities for the rest of the `@dandi` system.
Downloads
439
Readme
@dandi/common
@dandi/common
provides common types and utilities for the rest of the
@dandi
system.
@dandi/common
does not have any dependencies on NodeJS, and therefore
can be used on classes shared with projects targeted for the web.
Disposable
The Disposable
interface allows implementing classes to define
behavior for cleaning up resources like IO streams, database
connections, as well as Observable and other event subscriptions.
When used with @dandi/core
, Disposable
instances are
automatically disposed by Dandi at the end of their lifecycle.
class MyService implements Disposable {
constructor(private dbClient: DbClient) {}
public dispose(reason: string): void {
this.dbClient.release()
}
}
Disposable Utilities
Disposable
is also a static class provides several utility functions:
isDisposable(obj) - Returns
true
if the object implementsdispose()
; otherwise,false
.makeDisposable(obj, disposeFn) - Modifies the specified object to add the provided {@see DisposeFn} as the
Disposable.dispose
implementation. If the object already has a function member nameddispose
, it is wrapped and called before the new function.use(obj, fn) - Invokes the specified function in a
try
/catch
, statement, thenfinally
disposes the object. Returns the value returned byfn
, or rethrows the error thrown by it.useAsync(obj, fn) - Same as
use
, butfn
is invoked usingawait
.remapDisposed(target, reason) - Overwrites members of the target such that functions and property/field accessors throw an
AlreadyDisposedError
, a read-onlydisposed
property is set with the valuetrue
, and the target is frozen (usingObject.freeze
) to prevent further modification.