kiwi-event
TypeScript icon, indicating that this package has built-in type declarations

0.0.6 • Public • Published

Kiwi Event

A typescript event bus library.

Installation

Install through npm:

npm install --save kiwi-event

Install through yarn:

yarn add kiwi-event

Usage

import KiwiEvent from 'kiwi-event'

const $bus = KiwiEvent()

// Register multiple listeners for an event
const listener1 = $bus.on('hello world', () => {
  console.log('hello world 1')
})
const listener2 = $bus.on('hello world', () => {
  console.log('hello world 2')
})

$bus.emit('hello world')
// hello world 1
// hello world 2

// remove a listener
$bus.off(listener1) 
$bus.emit('hello world')
// hello world 2

// remove all listener for an event
$bus.off('hello world')

// remove one or multiple events and listeners
$bus.off(listener1)
$bus.off('hello world')
$bus.off([listener1, listener2])
$bus.off(['hello world', listener1, listener2])

// clear all events and listeners
$bus.clear()

methods

on(event, handler)

  • 'event': an event name.
  • 'handler': a callback function.

Register for an event, the return value is a unique key, you could use the off method to cancel the listener


emit(event, ...args)

  • 'event': an event name.
  • '...args': parameters passed to handler.

Emit an event


off(event)

  • 'event': an event name or the return value of the on method, or an array of them

Remove event listeners

Contribution

Your contributions and suggestions are heartily welcome.

License

MIT

Package Sidebar

Install

npm i kiwi-event

Weekly Downloads

0

Version

0.0.6

License

MIT

Unpacked Size

23.4 kB

Total Files

14

Last publish

Collaborators

  • iamiota