idisposable
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

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

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

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 check
  • name 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 terminates
  • functor 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 dispose
  • functor 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

Readme

Keywords

Package Sidebar

Install

npm i idisposable

Weekly Downloads

0

Version

0.2.0

License

Apache-2.0

Unpacked Size

40.2 kB

Total Files

5

Last publish

Collaborators

  • salvatorepreviti