# react-birch-event-emitter

> Simple Event Emitter

Latest version **2.0.7** (published 2021-05-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-birch-event-emitter
pnpm add react-birch-event-emitter
yarn add react-birch-event-emitter
bun add react-birch-event-emitter
```

## 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.0.7 |
| Published | 2021-05-22 |
| First published | 2021-05-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Tinia Labs |
| Maintainers | guycreate |
| Keywords | right, click, custom, context, menu, react, react-birch, tinia |

## Links

- npm: https://www.npmjs.com/package/react-birch-event-emitter
- Repository: https://github.com/tinialabs/react-birch
- Homepage: https://github.com/tinialabs/react-birch#readme
- Issues: https://github.com/tinialabs/react-birch/issues
- npm.io page: https://npm.io/package/react-birch-event-emitter

## 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

- 2.0.7 (latest) — 2021-05-22
- 2.0.6 — 2021-05-21
- 2.0.4 — 2021-05-21
- 2.0.3 — 2021-05-19
- 2.0.2 — 2021-05-19
- 2.0.1 — 2021-05-16
- 2.0.0 — 2021-05-16

## README

# react-birch-event-emitter - A Simple Event Library

Features include:

 - Unlike `on`, `off`, `addListener`, `removeListener` etc. `EventEmitter` event subscriptions return a `Disposable` object with `dispose()` method to unsubscribe from event.
 - Above feature allows you to group event subscriptions using `DisposablesComposite` and dispose them all with one call, i.e `DisposableComposite#dispose()`
 - Listeners voice matters!! You can now dispatch an event using `EventEmitter#dispatchWithReturn` where each listener's return value can be accessed from returned array.

 The Last feature can come in handy like in example below:

```javascript

// Here some listeners are subscribed to `onWillCommit` event, but we can allow listeners to disagree if they deem so
commitChange(change) {
    // ...

    // here we'll notify all `will-commit` listeners, but if one of them returns false, we'll abort the `commit` operation
    if (this.eventEmitter.dispatchWithReturn('will-commit', this.currentValue).some((canCommit) => canCommit === false)) {
        return
    }

    // Proceed with commit opertaion

    // ...
}
```

## Usage

```bash
npm i eventEmitter
```

```typescript
// Textbuffer.ts (I highly recommend TypeScript as you can get a huge performance boost by using `enum`s instead of strings as event identifiers)

import { EventEmitter } from 'eventEmitter'
// ...

enum TextBufferEvent {
    WillSave,
    WillPaste,
}

class TextBuffer implements IDisposable {
    private eventEmitter = new EventEmitter<TextBufferEvent>()
    // ...

    public onWillPaste(callback: (textFragment: TextFragment, pos: Position) => void): IDiposable {
        return this.eventEmitter.on(TextBufferEvent.Will, callback)
    }

    public onWillSave(callback: () => void): IDiposable {
        return this.eventEmitter.on(TextBufferEvent.WillSave, callback)
    }

    // ...

    private _pasteTextFragment(textFragment: TextFragment, pos: Position): boolean {
        // ...
        this.eventEmitter.dispatch(TextBufferEvent.WillPaste, textFragment, pos) // dispatch with args
        // ...
    }

    private _trySave(): boolean {
        // ...
        this.eventEmitter.dispatch(TextBufferEvent.WillSave) // dispatch without args
        // ...
    }
}

```

Full API can be found in `index.d.ts`

## License

MIT

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