@dannyfranca/eventus
v0.1.3
Published
[DEPRECATED] Event Manager made with RxJS's Subjects, inspired by jQuery's event API. Repository moved to @dannyfranca/radarjs
Downloads
1
Maintainers
Readme
Getting Started
- Install
yarn add @dannyfranca/eventus
- Import and create a new Instance
import { Eventus } from '@dannyfranca/eventus'
const eventus = new Eventus()
Usage
Listen to Events
const state = {
count: 0,
lastNotificationType: ''
}
eventus.on('notify', () => state.count++)
// receive any number off values as arguments
eventus.on('notify', ({ type }, ...data) => {
state.lastNotificationType = type)
console.log(data)
}
// subscribe is an alias
eventus.subscribe('logout', () => {/*...*/})
// can use namespaces
eventus.on('notify.namespace1.namespace2', () => {/*...*/})
Unsubscribe from Events
// by event name
eventus.off('notify')
// unsubscribe is an alias
eventus.unsubscribe('logout')
// by namespace
eventus.off('.namespace1')
Trigger Events
// pass any data to an event trigger
eventus.trigger('notify', {
type: 'info',
message: 'Just an ordinary notification'
})
// pass any number of data
eventus.trigger('notify', notification, ...data)
// next is an alias
eventus.next('logout')
Native Events
Native events has reserved names starting with $. Until now, the only native event available is $error.
$error event
// listening to $error
eventus.on('$error', (error: Error) => {/*...*/})
Eventus check for an error handler. If you don't set your own, an ordinary Error will be throwed with a message.
// set your error handler
eventus.setErrorHandler((error: Error) => {/*...*/})
License
Copyright (c) Danny França mailto:[email protected]