# @hazae41/disposer

> Helpers for Disposable

Latest version **2.0.20** (published 2024-05-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @hazae41/disposer
pnpm add @hazae41/disposer
yarn add @hazae41/disposer
bun add @hazae41/disposer
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.20 |
| Published | 2024-05-29 |
| First published | 2024-02-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | hazae41 |
| Maintainers | hazae41 |
| Keywords | cleaner, clean, cleanable, promise, async, race, events, side-effects, typescript, esmodules, tested, unit-tested |

## Links

- npm: https://www.npmjs.com/package/@hazae41/disposer
- Repository: https://github.com/hazae41/disposer
- Issues: https://github.com/hazae41/disposer/issues
- npm.io page: https://npm.io/package/@hazae41/disposer

## Recent versions

- 2.0.20 (latest) — 2024-05-29
- 2.0.18 — 2024-05-29
- 2.0.17 — 2024-05-29
- 2.0.15 — 2024-05-29
- 2.0.14 — 2024-05-29
- 2.0.12 — 2024-05-29
- 2.0.11 — 2024-02-08
- 2.0.10 — 2024-02-08

## README

# Disposer

A wrapper for an object with a dispose function

```bash
npm i @hazae41/disposer
```

[**Node Package 📦**](https://www.npmjs.com/package/@hazae41/disposer)

## Features

### Current features
- 100% TypeScript and ESM
- No external dependencies
- Create a disposable object from any object

## Usage

### Create a disposable object (Disposable or AsyncDisposable) from any object

```tsx
using d = new Disposer(something, () => { ... })
```

```tsx
await using d = new AsyncDisposer(something, async () => { ... })
```

You can get the inner object with `.inner` or `.get()` and the dispose function with `.dispose`

## Example

This can be useful for waiting for multiple listeners, and remove all listeners when one of them is triggered

```tsx
import { Future } from "@hazae41/future"
import { Disposer } from "@hazae41/disposer"

function waitA(): Disposer<Promise<"a">> {
  const future = new Future<"a">()
  const onevent = () => future.resolve("a")

  this.addEventListener("a", onevent)
  const off = () => this.removeEventListener("a", onevent)
  
  return new Disposer(future.promise, off)
}

function waitB(): Disposer<Promise<"b">> {
  const future = new Future<"b">()
  const onevent = () => future.resolve()

  this.addEventListener("b", onevent)
  const off = () => this.removeEventListener("b", onevent)
  
  return new Disposer(future.promise, off)
}

async function wait() {
  using a = waitA()
  using b = waitB()

  const x: "a" | "b" = await Promise.race([a.get(), b.get()])

  /**
   * All listeners will be removed
   */
}
```

---
_Source: https://npm.io/package/@hazae41/disposer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
