# @suinegmai/js-events

> Simple Javascript event emitter

Latest version **1.0.5** (published 2021-05-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @suinegmai/js-events
pnpm add @suinegmai/js-events
yarn add @suinegmai/js-events
bun add @suinegmai/js-events
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2021-05-29 |
| First published | 2019-07-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 16 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | lkmtri |
| Maintainers | lkmtri |
| Keywords | pubsub, js, ts, typescript, javascript, event-emitter |

## Links

- npm: https://www.npmjs.com/package/@suinegmai/js-events
- Homepage: https://github.com/lkmtri/js-events
- Issues: https://github.com/lkmtri/js-events/issues
- npm.io page: https://npm.io/package/@suinegmai/js-events

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.5 (latest) — 2021-05-29
- 1.0.4 — 2021-05-29
- 1.0.3 — 2021-05-29
- 1.0.2 — 2021-05-29
- 1.0.1 — 2021-05-29
- 1.0.0 — 2019-07-31

## README

## Tiny pubsub library in JS
### Install
```
npm install @suinegmai/js-events

or

yarn add @suinegmai/js-events
```
### Usage in JavaScript

```typescript
import EventEmitter from '@suinegmai/js-events'

const ev = EventEmitter()

const onMount = (...args) => console.log('mount: ', ...args)
const onUpdate = (...args) => console.log('update: ', ...args)
const onUnmount = (...args) => console.log('unmount: ', ...args)

//Subscribe to 'mount' event, but only trigger the callback once.
ev.once('mount', onMount)

//Subscribe to 'update' event, the callback is trigger for every 'update' event
ev.on('update', onUpdate)

//Subscribe to 'unmount' event, but only trigger the callback once.
ev.once('unmount', onUnmount)


ev.emit('mount', 'First mount')
ev.emit('mount', 'Second mount')
ev.emit('update', { state: 1 })
ev.emit('update', { state: 2 })
ev.emit('unmount', 0)

//Unsubscribe onUpdate
ev.off('update', onUpdate)

//Console output
mount: First mount
update: { state: 1 }
update: { state: 2 }
unmount: 0
```

### Usage in TypeScript
```typescript
import EventEmitter from '@suinegmai/js-events'

type Event = {
  mount: string
  update: {
    state: number
  }
  unmount: number
}

const ev = EventEmitter<Event>()

const onMount = (...args) => console.log('mount: ', ...args)
const onUpdate = (...args) => console.log('update: ', ...args)
const onUnmount = (...args) => console.log('unmount: ', ...args)

//Subscribe to 'mount' event, but only trigger the callback once.
ev.once('mount', onMount)

//Subscribe to 'update' event, the callback is trigger for every 'update' event
ev.on('update', onUpdate)

//Subscribe to 'unmount' event, but only trigger the callback once.
ev.once('unmount', onUnmount)


ev.emit('mount', 'First mount')
ev.emit('mount', 'Second mount')
ev.emit('update', { state: 1 })
ev.emit('update', { state: 2 })
ev.emit('unmount', 0)

//Unsubscribe onUpdate
ev.off('update', onUpdate)

//Console output
mount: First mount
update: { state: 1 }
update: { state: 2 }
unmount: 0
```

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