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

1.1.1 • Public • Published

smitter

smitter is a small 250 byte event emitter.

Installation

npm i smitter

Usage

import { smitter } from 'smitter'

// create an instance
let emitter = smitter()

// subscribe to an event called 'hi'
let off = emitter.on('hi', x => {
  console.log(`hi ${x}!`)
})

// fire an event called 'hi' and pass data to the handler
emitter.emit('hi', 'mom') // 'hi mom!' logged to console

// remove subscription
off()

emitter.emit('hi', 'dad') // nothing

Typescript

If you're using Typescript, you can strictly type your events and handlers.

let emitter = smitter<{
  hi: { name: string }
}>()

// strictly typed payloads
emitter.on('hi', ({ name }) => {})

// [E] Argument of type 'string' is not assignable to parameter of type { name: string }
emitter.emit('hi', 'mom')

// OK
emitter.emit('hi', { name: 'mom' })

License

MIT License © Self Aware

Dependencies (0)

    Dev Dependencies (16)

    Package Sidebar

    Install

    npm i smitter

    Weekly Downloads

    109

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    4.31 kB

    Total Files

    6

    Last publish

    Collaborators

    • wagz