@hypernym/emitter

2.0.1 • Public • Published

Emitter

A super simple and lightweight event emitter.

Repository | Package | Releases | Discussions

npm i @hypernym/emitter

Features

  • TypeScript friendly
  • Fully tree-shakeable
  • No dependencies

Usage

JS

import { createEmitter } from '@hypernym/emitter'

const emitter = createEmitter()

emitter.on('event-id', (e) => console.log(e.x, e.y))

emitter.emit('event-id', { x: 0, y: 0 })

TS

import { createEmitter, type Emitter } from '@hypernym/emitter'

type Events = {
  'event-id': { x: number; y: number }
  // ...
}

const emitter: Emitter<Events> = createEmitter<Events>()

emitter.on('event-id', (e) => console.log(e.x, e.y))

emitter.emit('event-id', { x: 0, y: 0 })

API

.on()

Registers a specific event.

emitter.on(id: string, callback: (event: any) => void)

.emit()

Emits a specific event.

emitter.emit(id: string, event: any)

.events

Main events map.

Stores all registered events.

emitter.events

.has()

Checks if a specific event by id exists in the map.

emitter.events.has(id: string)

.get()

Gets a specific event by id from the map.

emitter.events.get(id: string)

.delete()

Deletes a specific event by id from the map.

emitter.events.delete(id: string)

.clear()

Removes all events from the map.

emitter.events.clear()

Community

Feel free to use the official discussions for any additional questions.

License

Developed in 🇭🇷 Croatia

Released under the MIT license.

© Hypernym Studio

Package Sidebar

Install

npm i @hypernym/emitter

Weekly Downloads

0

Version

2.0.1

License

MIT

Unpacked Size

6.04 kB

Total Files

5

Last publish

Collaborators

  • ivodolenc
  • hypernym