0.1.1 • Published 9 months ago

@knicola/minibus v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@knicola/minibus

A strongly typed event emitter.

Install

Using npm:

$ npm install @knicola/minibus

Using yarn:

$ yarn add @knicola/minibus

Usage

import { minibus, event } from '@knicola/minibus'

// define event schema
interface IUserCreated {
    name: string
    email: string
}

// register events
const Events = {
    UserCreated: event<IUserCreated>('user:created'),
} as const

// create event bus
const bus = minibus()

// subscribe an event handler
bus.subscribe(Events.UserCreated, (data) => {
    console.log(data)
})

// dispatch a new event
bus.dispatch(Events.UserCreated, {
    name: 'Peter Pan',
    email: 'pete@neverland.com',
})

License

This project is open-sourced software licensed under the MIT license.