# lifestyle

> Objects life cycle manager (e.g. for Angular)

Latest version **0.0.2** (published 2019-06-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install lifestyle
pnpm add lifestyle
yarn add lifestyle
bun add lifestyle
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2019-06-25 |
| First published | 2019-06-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 11 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | vivainio |
| Maintainers | vivainio |

## Links

- npm: https://www.npmjs.com/package/lifestyle
- npm.io page: https://npm.io/package/lifestyle

## Recent versions

- 0.0.2 (latest) — 2019-06-25
- 0.0.1 — 2019-06-25

## README

[![npm version](https://badge.fury.io/js/lifestyle.svg)](https://badge.fury.io/js/lifestyle)

# lifestyle

JS object life cycle management helper. Best used with Angular. 

## Installation

```
$ yarn add --dev lifestyle
```

## Usage

This is typically used in angular application to avoid messing up your application code with
explicit subscriptions and resource disposal calls. 

```ts

import {lunsub, ldisposer, lstop} from "lifestyle";

// ... my component code

ngOnInit() {
  // schedule .unsubscribe on passed object. For RxJS observables
  lunsub(this, mystream$.subscribe(val => { ...}));
  lunsub(this, otherstream$.subscribe(val => { ... }));

  // lunsub supports many args 
  lunsub(this, a.subscribe(...), b.subscribe(...));
  
  )
  // call your own disposal code
  ldisposer(this, () => { console.log('arbitrary disposer function' )})
  // mobx autorun() follows the same protocol.
  ldisposer(this, autorun( () => ... mobx autorun code ...);

}

ngOnDestroy() {
  // this cleans up everything. You forget it, you leak stuff.
  lstop(this);  
}

```

## How it works

It stores all pushed disposers to in a WeakMap associated with passed "this". Full
implementation is https://github.com/vivainio/lifestyle/blob/master/src/index.ts.

This is more convenient and harder to screw up than `takeUntil(this.destroy$)` pattern,
especially if you end up going to subscribe() anyway.

Size is ~ 500 bytes uncompressed, and there are no dependencies.

## License

MIT. Copyright (c) 2019 by Ville M. Vainio <vivainio@gmail.com>

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