@locmod/event-aggregator
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

event-aggregator

This is very simple event aggregator

Installation

npm install --save @locmod/event-aggregator

Usage

Can be used directly

import { events } from '@locmod/event-aggregator'

or can be used as a class

const events = new EventAggregator()

Subscribe to event

useEffect(() => {
  const unsubscribe = events.subscribe('test event', console.log)

  return () => {
    unsubscribe()
  }
}, [])

or just

events.subscribe('test event', console.log)

Dispatch an event

events.subscribe('test event', { value: 10 }) // console.log will be called with { value: 10 }

Unsubscribe

events.unsubscribe('test event', console.log)

Get event

Get Event by name

events.getEvent('test event')
events.getEvent('unexistant event') // if event not found, it will be created and returned

Once

Subscribe to Event and unsubscribe after call

events.once('once in a lifetime', console.log)

Readme

Keywords

Package Sidebar

Install

npm i @locmod/event-aggregator

Weekly Downloads

50

Version

1.0.3

License

MIT

Unpacked Size

11.4 kB

Total Files

9

Last publish

Collaborators

  • on47sky
  • clean_bread
  • irondsd
  • grammka