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

1.0.0 • Public • Published

transit

Install

$ npm install --save transit
// using ES6 modules
import transit from 'transit'

// using  modules
var transit = require('transit')

Usage

import transit from 'transit'

const event = transit()

event.on('foo', (data) => {
  console.log(data) // abc
})
event.on('add', (data) => {
  console.log(data) // 1
})

event.emit('foo', 'abc')
event.emit('add', 1)

event.off('foo')
event.clear()

Typescript

import transit from 'transit'

interface Events {
  foo: string
  bar?: number
  add: symbol
}

const event = transit<Events>()
event.on('foo', (data) => {
  // (parameter) data: string
  console.log(data) // abc
})
event.on('bar', (data) => {
  // (parameter) data: number
  console.log(data) // 1
})

event.emit('foo', 'abc')
event.emit('bar')
event.emit('add', 1) // Argument of type 'number' is not assignable to parameter of type 'symbol'
event.emit('divide', 1) // Argument of type '"add1"' is not assignable to parameter of type 'keyof Events'.

API

  • on

    eventType: string | symbol

    handler: (data?: unknown) => void

  • emit

    eventType: string | symbol

    data?: unknown

  • off

    eventType: string | symbol

  • clear

Readme

Keywords

none

Package Sidebar

Install

npm i transit

Weekly Downloads

0

Version

1.0.0

License

ISC

Unpacked Size

5.36 kB

Total Files

6

Last publish

Collaborators

  • mz-dfhp