IDisposable
Simple IDisposable pattern for NodeJS
installation
npm install --save idisposable
Sources and References
This package - https://www.npmjs.com/package/idisposable
Source code and build tools for this package - https://github.com/SalvatorePreviti/idisposable
LICENSES
- This package is provided as open source software using Apache License.
API
Table of Contents
- IDisposable
- isPromiseLike
- isPromise
- dispose
- tryDispose
- using
- disposeAsync
- tryDisposeAsync
- usingAsync
- ignoreError
IDisposable
A base abstract disposable class
isDisposable
Returns true if the given object is a disposable object. An object is disposable if it has a dispose(...), destroy(...) or delete() function.
Parameters
instance
any The object instance to check
Returns boolean True if the given object is disposable, false if not.
isDisposed
Returns true if the given instance is undefined, is null, has a isDisposed property that returns true or a function isDisposed() that returns true
Parameters
instance
(IDisposable | null | undefined) The instance to check
Returns boolean True if the object was disposed.
throwIfDisposed
Throws an error if the given object is disposed, null or undefined.
Parameters
instance
any The instance to checkname
string? A name used for debugging purposes
Returns (void | never) undefined
onIgnoredError
Callback that is executed when tryDispose eats an error. Normally does nothing, can be set to something for logging.
Disposable types and functions
isPromiseLike
Returns true if the given instance is an object with a then function.
Parameters
instance
any The instance to check
Returns boolean True if the given object is a promise, false if not.
isPromise
Returns true if the given instance is an object with a then and a catch function.
Parameters
instance
any The instance to check
Returns boolean True if the given object is a promise, false if not.
dispose
Disposes the given objects. Accepts:
- Objects with a dispose() method.
- Objects with a destroy() method.
- Objects with a delete() method with no arguments.
- Objects with a close() method.
- functions that return disposables or iterable of disposables.
- iterables of disposables or functions that return disposables..
Parameters
instances
...Array<any> The objects to dispose
Returns void
tryDispose
Disposes the given objects, ignoring any error. Accepts:
- Objects with a dispose() method.
- Objects with a destroy() method.
- Objects with a delete() method with no arguments.
- Objects with a close() method.
- functions that return disposables or iterable of disposables.
- iterables of disposables or functions that return disposables..
Parameters
instances
...Array<any> The instances to dispose.
Returns void
using
Disposes the given object when after the functor get called.
Parameters
instance
any The instance to be disposed after the functor terminatesfunctor
Function The functor to execute.
Returns any The functor result
disposeAsync
Disposes the given objects, asynchronously. Accepts:
- Objects with a dispose() method.
- Objects with a destroy() method.
- Objects with a delete() method with no arguments.
- Objects with a close() method.
- Promises that resolves disposables.
- functions that return disposables or iterable of disposables or promises that resolves to disposables.
- iterables of disposables or functions that return disposables or promises that resolves to disposables.
Parameters
instances
...Array<any> The instances to dispose
Returns Promise<void> The promise to be awaited.
tryDisposeAsync
Disposes the given objects, asynchronously, ignoring any error. Accepts:
- Objects with a dispose() method.
- Objects with a destroy() method.
- Objects with a delete() method with no arguments.
- Objects with a close() method.
- Promises that resolves disposables.
- functions that return disposables or iterable of disposables or promises that resolves to disposables.
- iterables of disposables or functions that return disposables or promises that resolves to disposables.
Parameters
instances
...Array<any> The instance to dispose.
Returns Promise<void> The promise to be awaited.
usingAsync
Disposes the given object asynchronously when after the given functor completes.
Parameters
instance
any The instance to disposefunctor
Function The functor to wait
Returns Promise<any> The result
ignoreError
Ignores the given error. Called by tryDispose and tryDisposeAsync.
Parameters
error
any The error to ignore.
Returns void