1.0.0 • Published 3 years ago

@jojy/event-bus v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

A javascript eventBus

A quick example

npm i @jojy/event-bus -S

import eventBus from '@jojy/event-bus';

// bind
eventBus.on('back', e => console.log(`1: ${e}`))
eventBus.on('back', e => console.log(`2: ${e}`), 1)  // just callback once
eventBus.on('back.pageA', e => console.log(`3: ${e}`))  // with namespace

// emit
eventBus.emit('back', 'test')
// 1: test  2: test  3: test

eventBus.emit('back', 'test')
// 1: test  3: test

eventBus.emit('back.pageA', 'test')
// 3: test

// unbound
eventBus.off('back.pageA')  // only unbound back.pageA
eventBus.off('back')  // unbound all back

Method

  • on(key, function, times),subscribe message
  • emit(key, data),publish message
  • off(key),remove the subscription

Introduction

  • support namespace, use key.namespace as a key, just like Jquery on.
  • can set callback times, for example: eventBus.on(key, callback, 1), it will execute once only.