@suinegmai/js-events
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

Tiny pubsub library in JS

Install

npm install @suinegmai/js-events

or

yarn add @suinegmai/js-events

Usage in JavaScript

import EventEmitter from '@suinegmai/js-events'

const ev = EventEmitter()

const onMount = (...args) => console.log('mount: ', ...args)
const onUpdate = (...args) => console.log('update: ', ...args)
const onUnmount = (...args) => console.log('unmount: ', ...args)

//Subscribe to 'mount' event, but only trigger the callback once.
ev.once('mount', onMount)

//Subscribe to 'update' event, the callback is trigger for every 'update' event
ev.on('update', onUpdate)

//Subscribe to 'unmount' event, but only trigger the callback once.
ev.once('unmount', onUnmount)


ev.emit('mount', 'First mount')
ev.emit('mount', 'Second mount')
ev.emit('update', { state: 1 })
ev.emit('update', { state: 2 })
ev.emit('unmount', 0)

//Unsubscribe onUpdate
ev.off('update', onUpdate)

//Console output
mount: First mount
update: { state: 1 }
update: { state: 2 }
unmount: 0

Usage in TypeScript

import EventEmitter from '@suinegmai/js-events'

type Event = {
  mount: string
  update: {
    state: number
  }
  unmount: number
}

const ev = EventEmitter<Event>()

const onMount = (...args) => console.log('mount: ', ...args)
const onUpdate = (...args) => console.log('update: ', ...args)
const onUnmount = (...args) => console.log('unmount: ', ...args)

//Subscribe to 'mount' event, but only trigger the callback once.
ev.once('mount', onMount)

//Subscribe to 'update' event, the callback is trigger for every 'update' event
ev.on('update', onUpdate)

//Subscribe to 'unmount' event, but only trigger the callback once.
ev.once('unmount', onUnmount)


ev.emit('mount', 'First mount')
ev.emit('mount', 'Second mount')
ev.emit('update', { state: 1 })
ev.emit('update', { state: 2 })
ev.emit('unmount', 0)

//Unsubscribe onUpdate
ev.off('update', onUpdate)

//Console output
mount: First mount
update: { state: 1 }
update: { state: 2 }
unmount: 0

Package Sidebar

Install

npm i @suinegmai/js-events

Weekly Downloads

1

Version

1.0.5

License

MIT

Unpacked Size

16.4 kB

Total Files

13

Last publish

Collaborators

  • lkmtri