# rxjs-obs-unsubscribe

> Typescript decorator for auto unsubscribe when component is destroyed.

Latest version **1.0.6** (published 2020-10-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install rxjs-obs-unsubscribe
pnpm add rxjs-obs-unsubscribe
yarn add rxjs-obs-unsubscribe
bun add rxjs-obs-unsubscribe
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2020-10-29 |
| First published | 2020-10-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Dragan Dejanovic |
| Maintainers | dejooo13 |
| Keywords | angular, react, rxjs, observables, unsubscribe, typescript, decorators, javascript |

## Links

- npm: https://www.npmjs.com/package/rxjs-obs-unsubscribe
- Repository: https://github.com/mangicapra/rxjs-obs-unsubscribe
- Homepage: https://github.com/mangicapra/rxjs-obs-unsubscribe#readme
- Issues: https://github.com/mangicapra/rxjs-obs-unsubscribe/issues
- npm.io page: https://npm.io/package/rxjs-obs-unsubscribe

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.6 (latest) — 2020-10-29
- 1.0.5 — 2020-10-29
- 1.0.4 — 2020-10-28
- 1.0.3 — 2020-10-28
- 1.0.2 — 2020-10-28
- 1.0.1 — 2020-10-28
- 1.0.0 — 2020-10-28

## README

### Auto unsubscribe observables

#### Class decorator that will unsubscribe all observables inside component

## Installation

`npm i rxjs-obs-unsubscribe --save`

## Usage

First inside your `tsconfig.json` enable: `"emitDecoratorMetadata"` and `"experimentalDecorators"`

```js
import { ObsUnsubscribe } from 'rxjs-obs-unsubscribe';

@ObsUnsubscribe()
@Component({
  selector: 'app-products',
  templateUrl: './products.component.html',
  styleUrls: ['./products.component.css']
})
export class ProductsComponent implements OnInit, OnDestroy {
  products$: Subscription; // it supports single Subscription
  sub$: Subscription[]; // or array of Subscriptions

  constructor( private productService: ProductService ) {}

  ngOnInit() {
    his.products$ = this.productService.getProducts().subscribe(res => ... );
    this.sub$ = [
        this.productService.obsOne().subscribe(res => ... ),
        this.productService.obsTwo().subscribe(res => ... ),
        ...
    ];
  }

  // Method(event) that is specified as decorator option must be present
  // otherwise it will throw error, it can stay empty
  ngOnDestroy(): void {}
  
```

Optionaly pass `event` to decorator so unsubscribe can happen inside it:
`@ObsUnsubscribe({event: 'componentWillUnmount'})`

### Options

| Option      | Description                                            | Default Value |
| ----------- | ------------------------------------------------------ | ------------- |
| `event`     | a name of event callback to execute on                 | `ngOnDestroy` |

Note: `event` can be set to any method or lifecycle hook e.g. `componentWillUnmount` for react.

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