npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tioniq/disposiq

v1.0.6

Published

Disposiq is a utility collection of Disposable (aka Dispose) pattern

Downloads

406

Readme

Disposiq

Coverage npm version npm bundle size license

Disposiq is a utility collection of Disposable (or Dispose) pattern.

This library is compatible with upcoming Explicit Resource Management API which is already implemented in Typescript 5.2

Are you lazy about cleaning up resources? Does your code look messy, and do you want to make it cleaner and easier to read? Are you tired of writing the same code for cleaning up resources over and over again? Have you wanted to use the Disposable pattern in your project, but you don't know how to start? Then this library is for you!

Stop talking, show me the code!

Installation

You can install the library using npm:

npm install @tioniq/disposiq

Function as a disposable

import {DisposableAction} from '@tioniq/disposiq'

// The action will be executed only once when the disposable is disposed
const disposable = new DisposableAction(() => {
  console.log('Resource cleaned up')
})
disposable.dispose() // Output: Resource cleaned up
disposable.dispose() // No output

Store-based disposable

import {DisposableStore, DisposableAction} from '@tioniq/disposiq'

const store = new DisposableStore()
const disposable1 = new DisposableAction(() => {
  console.log('Resource cleaned up 1')
})
// DisposableStore accepts functions as disposables
const disposable2 = () => {
  console.log('Resource cleaned up 2')
}
store.add(disposable1)
store.add(disposable2)
store.dispose() // Output: Resource cleaned up 1, Resource cleaned up 2

const disposable3 = () => {
  console.log('Resource cleaned up 3')
}

// After disposing of the store, all disposables added to the store will be disposed of immediately
store.add(disposable3) // Output: Resource cleaned up 3

Store-based disposable to dispose temporary resources

import {DisposableStore, DisposableAction} from '@tioniq/disposiq'

const store = new DisposableStore()
const disposable1 = new DisposableAction(() => {
  console.log('Resource cleaned up 1')
})
const disposable2 = new DisposableAction(() => {
  console.log('Resource cleaned up 2')
})
// You can add multiple disposables at once
store.add(disposable1, disposable2)

// Dispose of the current disposables in the store without disposing of the store itself
store.disposeCurrent() // Output: Resource cleaned up 1, Resource cleaned up 2

// The store is still active, but all contained disposables are disposed of and removed from the store
// You can now add new disposables to the store
store.add(() => {
  console.log('Resource cleaned up 3')
}) // No output

// Dispose the store completely
store.dispose() // Output: Resource cleaned up 3

Explicit Resource Management API support (aka 'using' keyword)

import {DisposableStore, DisposableAction} from '@tioniq/disposiq'

// When using the new 'using' keyword, the disposable will be disposed of automatically when it goes out of scope
{
  using disposable = new DisposableAction(() => {
    console.log('Resource cleaned up')
  })
}
// Output: Resource cleaned up

// It also works with other disposable objects from the library. For example, DisposableStore:
{
  using store = new DisposableStore()
  store.add(new DisposableAction(() => {
    console.log('Resource cleaned up')
  }))
} // Output: Resource cleaned up

If you cannot use 'using' keyword

There is a way to achieve the same result without use of the 'using' keyword. You can use the 'using' function instead.

import {Disposable, using} from '@tioniq/disposiq'

using(new Client(), async (client) => {
  await client.makeRequest() // Output: Request made
}) // Output: Resource cleaned up

class Client extends Disposable {
  constructor() {
    super()
    this.addDisposable(() => {
      console.log('Resource cleaned up')
    })
  }

  async makeRequest() {
    console.log('Request made')
  }
}

You can find a simple example here. Also, check out another project built with Disposiq: Eventiq. It's an implementation of the Observer pattern using Disposiq. It's an interesting project worth exploring!

Inspiration

This library is inspired by the

  • Dispose pattern and its principles
  • The usage of disposables in RxJava and ReactiveX
  • The C# IDisposable interface
  • The core concept used by major projects like Angular, React, Vue, etc., which utilize a return function or component method to clean up resources

Documentation

Interfaces & Types

| Interface | Short Description | |------------------------------|----------------------------------------------------------------| | IDisposable | Base interface for disposables | | IAsyncDisposable | Base interface for asynchronous disposables | | DisposeFunc | A function with no parameters | | DisposableLike | A function or disposable object | | AsyncDisposeFunc | An asynchronous function with no parameters | | IDisposablesContainer | A container for a collection of disposables | | DisposableAware | Represents a disposable that is aware of its state | | DisposableCompat | Represents a disposable compatible with the 'using' keyword | | DisposableAwareCompat | Combines DisposableAware and DisposableCompat | | AsyncDisposableAware | An asynchronous disposable that is aware of its state | | AsyncDisposableCompat | An asynchronous disposable compatible with the 'using' keyword | | AsyncDisposableAwareCompat | Combines AsyncDisposableAware and AsyncDisposableCompat |

Classes

| Class | Short Description | Aliases | |-----------------------------|---------------------------------------------------------------------------|-----------------------| | DisposableAction | A container for a function to be called on dispose | - | | AsyncDisposableAction | A container for an asynchronous function to be called on dispose | - | | DisposableStore | A container for disposables | CompositeDisposable | | DisposableContainer | A container for a disposable object | SerialDisposable | | BoolDisposable | A object that aware of its disposed state | BooleanDisposable | | SafeActionDisposable | A container for a function that is safely called on dispose | - | | SafeAsyncActionDisposable | A container for an asynchronous function that is safely called on dispose | - | | AbortDisposable | A wrapper for AbortController to make it disposable | - | | ObjectDisposedException | An exception thrown when an object is already disposed | - |

Functions

| Class | Short Description | Aliases | |---------------------------|------------------------------------------------------------------------------------------------------|--------------------| | disposeAll | Dispose of all disposables in the array safely, allowing array modification during disposal | disposeAllSafe | | disposeAllUnsafe | Dispose of all disposables in the array unsafely, array modification during disposal is dangerous | - | | createDisposable | Create a disposable object from a given parameter | toDisposable | | createDisposableCompat | Create a disposable object from a given parameter compatible with the 'using' keyword | toDisposableCompat | | disposableFromEvent | Create a disposable object from an event listener | on | | disposableFromEventOnce | Create a disposable object from an event listener that disposes after the first call | once | | isDisposable | Check if the object is a disposable object | - | | isDisposableLike | Check if the object is a disposable-like | - | | isDisposableCompat | Check if the object is a disposable object that is compatible with the 'using' keyword | - | | isAsyncDisposableCompat | Check if the object is an asynchronous disposable object that is compatible with the 'using' keyword | - | | isSystemDisposable | Check if the object is compatible with the system 'using' keyword | - | | isSystemAsyncDisposable | Check if the object is compatible with the system 'await using' keyword | - |

For more information, please check the type definitions file.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub. Remember to write tests for your changes.

License

This project is licensed under the MIT License.