Estacion
![dependabot enabled](https://flat.badgen.net/dependabot/dependabot/dependabot-core/?icon=dependabot)
Your search for the event bus implementation is over.
Estacion is made on top of native node EventEmitter
class.
It can be used in the browser via events
module which is automatically included by the bundlers like webpack
and browserify
.
Instalation
npm install estacion
Example
const { EventBus } = require('estacion')
const bus = new EventBus()
const usersChannel = bus.channel('users')
const gamesChannel = bus.channel('games')
const userAdded = usersChannel.topic('user_added')
const userRemoved = usersChannel.topic('user_removed')
const gameStart = gamesChannel.topic('game_start')
const gameEnd = gamesChannel.topic('game_end')
const listener = event => {
console.log(event.channel)
console.log(event.topic)
console.log(event.payload)
}
usersChannel.addListener(listener)
userAdded.addListener(listener)
bus.mainChannel().addListener(listener)
bus
.mainChannel()
.topic('game_end')
.addListener(listener)
const customPayload = {}
usersChannel.emit(customPayload)
userAdded.emit({ name: 'Sam', lastName: 'Fisher' })
skip to mainChannel
explanation
example on runkit
Setup is really simple:
- EventBus holds channels.
- Channels hold topics.
- Add listeners to channels or topics.
EventBus
EventBus
is used for creating and removing channels. You can create
any number of channels.
EventBus
is not a singleton, you can create any number of EventBus
instances.
const eventBus = new EventBus()
const channel = eventBus.channel('nbc')
const eventBus.removeChannel('nbc')
Channel
Channel
can create and remove topics
. You can add listeners directly to the channel
or you can add listeners to the particular topic
.
When you add a listener to the channel directly, you will be notified for all events that are dispatched via topics
for that channel, and also when the channel
itself emits ( event.topic
will be set to '*'
).
const eventBus = new EventBus()
const channel = eventBus.channel('nbc')
const economyTopic = channel.topic('economy')
const sportsTopic = channel.topic('sports')
const listener = event => {
console.log(event.channel)
console.log(event.topic)
console.log(event.payload)
}
channel.addListener(listener)
economyTopic.addListener(event => {
console.log(event.channel)
console.log(event.topic)
console.log(event.payload)
})
channel.removeAllListeners()
channel.removeListener(listener)
Topic
Topic
is the smallest building block of the event bus system. You can only create topics via channels.
const eventBus = new EventBus()
const channel = eventBus.channel('nbc')
const ecologyTopic = channel.topic('ecology')
const listener = event => {
console.log(event.channel)
console.log(event.topic)
console.log(event.payload.title)
console.log(event.payload.content)
}
ecologyTopic.addListener(listener)
ecologyTopic.emit({ title: 'Climate change is real.', content: 'Lorem ipsum' })
channel.removeTopic('ecology')
ecologyTopic.removeListener(listener)
Subscribe
Channel
and Topic
classes both inherit from the Broadcaster
class, which wraps the native EventEmitter
and exposes some of its methods.
You can subscribe to the channel
or topic
via these methods:
const listener = event => {}
channelOrTopic.addListener(listener)
channelOrTopic.on(listener)
channelOrTopic.prependListener(listener)
channelOrTopic.once(listener)
channelOrTopic.prependOnceListener(listener)
Listener function accepts one parameterEventPayload
.
type Listener = (event: EventPayload) => void
interface EventPayload {
channel: string
topic: string
readonly payload?: any | undefined
}
Unsubscribe
Every one of the subscribe function variations returns a function to unsubscribe from the channel
or topic
const listener = event => {}
const unsubscribe = channelOrTopic.addListener(listener)
unsubscribe()
Or you can use one of the methods on channel
or topic
const listener = event => {}
channelOrTopic.removeListener(listener)
channelOrTopic.off(listener)
Remove all listeners from the channel
or topic
:
channelOrTopic.removeAllListeners()
Also by destroying channel
or topic
, all listeners will be automatically unsubscribed.
When destroying channel
all topics in that channel will also be destroyed.
const eventBus = new EventBus()
const channel = eventBus.channel('nbc')
const ecologyTopic = channel.topic('ecology')
channel.addListener(event => {})
ecologyTopic.addListener(event => {})
channel.destroy()
ecologyTopic.destory()
EventBus main channel
There is a special channel on the EventBus
that is created automatically with every EventBus
instance. This special channel is used for listening to all other channels via just one subscribe call.
const eventBus = new EventBus()
const channelOne = eventBus.channel('one')
const channelTwo = eventBus.channel('two')
channelOne.emit({ name: 'Sam' })
channelTwo.emit({ name: 'Jack' })
const mainChannel = eventBus().mainChannel()
mainChannel.addListener(event => {
console.log(event.channel)
console.log(event.topic)
console.log(event.payload)
})
You can also subscribe to the particular topics
that are emitted
from any channel. In the next example, you will subscribe to the 'economy' topic on any channel and nothing else.
const eventBus = new EventBus()
const channelOne = eventBus.channel('cnn')
const channelTwo = eventBus.channel('nbc')
channelOne.topic('economy').emit()
channelTwo.topic('economy').emit()
const mainChannel = eventBus().mainChannel()
mainChannel.topic('economy').addListener(event => {
console.log(event.channel)
console.log(event.topic)
})
API docs
Estacion
is written in TypeScript, auto generated API docs are available.
Author
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Acknowledgments