0.1.3 • Published 4 years ago

@dannyfranca/eventus v0.1.3

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

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

MIT License

Copyright (c) Danny França contato@dannyfranca.com