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)