@guanghechen/event-bus
TypeScript icon, indicating that this package has built-in type declarations

6.0.0-alpha.1 • Public • Published

@guanghechen/event-bus


A simple event bus.

Install

npm install --save @guanghechen/event-bus
  • yarn

    yarn add @guanghechen/event-bus

Usage

  • Basic

    import type { IEvent, IEventHandler } from '@guanghechen/event-bus'
    import { EventBus } from '@guanghechen/event-bus'
    
    enum EventTypes {
      INIT = 'INIT',
      EXIT = 'EXIT',
    }
    
    const eventBus = new EventBus<EventTypes>()
    
    const handle: IEventHandler<EventTypes> = (evt: IEvent<EventTypes>) => {
      console.log('evt:', evt)
    }
    
    // Listen for specific event
    eventBus.on(EventTypes.INIT, handle)
    
    // Listen for specific event, and only need to be called once
    eventBus.once(EventTypes.INIT, handle)
    
    // Listen for all events
    eventBus.subscribe(handle, true)
    
    // Remove listener
    eventBus.removeListener(EventTypes.INIT, handle)
    
    // Remove subscriber
    eventBus.unsubscribe(handle)

Readme

Keywords

Package Sidebar

Install

npm i @guanghechen/event-bus

Weekly Downloads

2

Version

6.0.0-alpha.1

License

MIT

Unpacked Size

42.2 kB

Total Files

8

Last publish

Collaborators

  • lemonclown