@dannyfranca/eventus
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

dannyfranca/eventus

[DEPRECATED] Event Manager made with RxJS's Subjects, inspired by jQuery's event API. Repository moved to @dannyfranca/radarjs

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 mailto:contato@dannyfranca.com

Package Sidebar

Install

npm i @dannyfranca/eventus

Weekly Downloads

1

Version

0.1.3

License

MIT

Unpacked Size

530 kB

Total Files

54

Last publish

Collaborators

  • dannyfranca