# @stoplight/lifecycle

> Event and disposable helpers.

Latest version **2.3.3** (published 2022-03-04) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @stoplight/lifecycle
pnpm add @stoplight/lifecycle
yarn add @stoplight/lifecycle
bun add @stoplight/lifecycle
```

## 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 | 2.3.3 |
| Published | 2022-03-04 |
| First published | 2018-12-13 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8.3.0 |
| Dependencies | 2 |
| Unpacked size | 37 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Stoplight |
| Maintainers | stoplight-devops |

## Links

- npm: https://www.npmjs.com/package/@stoplight/lifecycle
- Repository: https://github.com/stoplightio/lifecycle
- Homepage: https://github.com/stoplightio/lifecycle#readme
- Issues: https://github.com/stoplightio/lifecycle/issues
- npm.io page: https://npm.io/package/@stoplight/lifecycle

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^2.3.1
- [wolfy87-eventemitter](https://npm.io/package/wolfy87-eventemitter.md) ~5.2.8

## Recent versions

- 2.3.3 (latest) — 2022-03-04
- 2.3.2 — 2020-09-01
- 2.3.1 — 2020-08-27
- 2.3.0 — 2020-08-13
- 2.2.2 — 2020-07-24
- 2.2.1 — 2020-02-10
- 2.2.0 — 2019-12-01
- 2.1.4 — 2019-11-22
- 2.1.3 — 2019-09-27
- 2.1.2 — 2019-09-25
- 2.1.1 — 2019-09-23
- 2.1.0 — 2019-09-23
- 2.0.1 — 2019-04-26
- 2.0.0 — 2019-04-25
- 1.1.0 — 2019-04-25
- … 1 more at https://npm.io/package/@stoplight/lifecycle/versions

## README

# @stoplight/lifecycle

[![Maintainability](https://api.codeclimate.com/v1/badges/79ae2a385f589a16e38a/maintainability)](https://codeclimate.com/github/stoplightio/lifecycle/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/79ae2a385f589a16e38a/test_coverage)](https://codeclimate.com/github/stoplightio/lifecycle/test_coverage)

Event and disposable helpers.

- Explore the interfaces: [TSDoc](https://stoplightio.github.io/lifecycle)
- View the changelog: [Releases](https://github.com/stoplightio/lifecycle/releases)

### Features

- Disposable helpers.
- Event emitter helpers.

### Installation

Supported in modern browsers and node.

```bash
# latest stable
yarn add @stoplight/lifecycle
```

### Usage

#### Disposables

A standardized way for things to declare how they cleanup. Simple example below:

```ts
import {
  DisposableCollection,
  EventEmitter,
  IDisposable
} from "@stoplight/lifecycle";

export class Editor implements IDisposable {
  // EventEmitter itself is an IDisposable
  public readonly valueEmitter = new EventEmitter<string, "didUpdate">();

  private readonly disposables: DisposableCollection = new DisposableCollection();

  constructor() {
    this.disposables.push(this.valueEmitter);
  }

  // Implement IDisposable
  dispose() {
    this.disposables.dispose();
  }
}
```

#### Emitter

A simple example editor that allows users to subscribe to value update events.

```ts
import { EventEmitter, IDisposable } from "@stoplight/lifecycle";

class Editor implements IDisposable {
  private _value = "";

  // create an emitter instance for this editor, defining the possible events and event object value
  private valueEmitter = new EventEmitter<string, "willUpdate" | "didUpdate">();

  get value() {
    return this._value;
  }

  set value(val: string) {
    this.valueEmitter.emit("willUpdate", this._value);
    this._value = val;
    this.valueEmitter.emit("didUpdate", this._value);
  }

  get onWillUpdateValue() {
    return this.valueEmitter.on("willUpdate");
  }

  get onDidUpdateValue() {
    return this.valueEmitter.on("didUpdate");
  }

  dispose() {
    // best practice to cleanup the emitter
    this.valueEmitter.dispose();
  }
}

const editor = new Editor();

const willUpdateListener = editor.onWillUpdateValue(val => {
  console.log("next value: ", val);
});

const didUpdateListener = editor.onDidUpdateValue(val => {
  console.log("new value: ", val);
});

// stop listening to willUpdate event
willUpdateListener.dispose();
```

### Contributing

1. Clone repo.
2. Create / checkout `feature/{name}`, `chore/{name}`, or `fix/{name}` branch.
3. Install deps: `yarn`.
4. Make your changes.
5. Run tests: `yarn test.prod`.
6. Stage relevant files to git.
7. Commit: `yarn commit`. _NOTE: Commits that don't follow the [conventional](https://github.com/marionebl/commitlint/tree/master/%40commitlint/config-conventional) format will be rejected. `yarn commit` creates this format for you, or you can put it together manually and then do a regular `git commit`._
8. Push: `git push`.
9. Open PR targeting the `next` branch.

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