You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

@jeppech/typed-emitter

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jeppech/typed-emitter

Typed event emitter

1.1.0
latest
Source
npm
Version published
Weekly downloads
2
-84.62%
Maintainers
1
Weekly downloads
 
Created
Source

Typed event emitter

This is a fairly simple class, for emitting/subscriping to typed events

Installation

Use your favourite package manager.

pnpm add @jeppech/typed-emitter

Usage

import { TypedEmitter } from '@jeppech/typed-emitter'

// Start by declaring an interface, describing your events and data types

interface EventData {
  id: number
  user: string
  message: string
}

interface MyEvents {
  'data': EventData
  'connected': undefined
  'disconnected': undefined
}

const emitter = TypedEmitter<MyEvents>()

// Note you get hints, when using the methods of the emitter.
emitter.on('data', (data) => {
  // data is EventData-type
})

emitter.emit('data', {
  'id': 1,
  'user': 'jeppech',
  'message': 'Hello!'
})

Extending class


class MyClass extends TypedEmitter<MyEvents> {
  constructor() {
    super()

    this.on('data', (data) => {
      // data is EventData-type
    })
  }
}

Keywords

typescript

FAQs

Package last updated on 25 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts