New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

event-emitters

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

event-emitters

event emitters

latest
Source
npmnpm
Version
1.0.6
Version published
Weekly downloads
15
-28.57%
Maintainers
1
Weekly downloads
 
Created
Source

event-emitters

build status Known Vulnerabilities Renovate

Typesafe event emitters.

EventEmitter<T>

Similar to node's EventEmitter but:

  • Only emits a single event of type T.
  • Throw an exception when a client registers the same listener more than once.
  • Throw an exception when a client tries to remove a listener that is not listening.
  • Support async iteration.
const emitter = new EventEmitter<number>()
const listener = (n: number): void => {
  console.log(n)
}

emitter.subscribe(listener)
emitter.emit(42)

try {
  emitter.subscribe(listener)
} catch (e) {
  console.log('same listener')
}

emitter.unsubscribe(listener)
try {
  emitter.unsubscribe(() => {})
} catch (e) {
  console.log('listener not found')
}

The above will log:

42
same listener
listener not found
const emitter = new EventEmitter<number>()

async function logEmitterValues(emitter: EventEmitter<number>) {
  for await (const val of emitter) {
    console.log(val)
  }
}

logEmitterValues(emitter)

emitter.emit(42)
emitter.emit(43)

The above will log:

42
43
const emitter = new EventEmitter<number>()
const listener = emitter.subscribe((val: number) => {
  console.log(val)
})
emitter.unsubscribe(listener)

subscribe returns the callback passed to it which can be useful for unsubscribing later.

EventEmitterWithCurrent<T>

Provides the same API as EventEmitter but:

  • Is initialized with the current message.
  • Emits the current message to each listener as soon as it subscribes.
const emitter = new EventEmitterWithCurrent<number>(42)
emitter.subscribe((n: number): void => {
  console.log(n)
})
emitter.emit(43)

The above will log:

42
43

EventEmitterWithOptionalCurrent<T>

Provides the same API as EventEmitterWithCurrent but:

  • Can optionally be initialized with the current message.
  • Emits the current message to each listener as soon as it subscribes only when the current message is available.
const emitter = new EventEmitterWithCurrent<number>()
emitter.subscribe((n: number): void => {
  console.log(n)
})
emitter.emit(43)

The above will log:

43

QueueingEventEmitter<T>

Provides the same API as EventEmitter but:

  • Queues messages when there are no subscribers.
  • Delivers queued message to the first subscriber, then drains the queue.
const emitter = new EventEmitterWithCurrent<number>(42)
emitter.emit(44)
emitter.emit(45)
emitter.subscribe((n: number): void => {
  console.log(n)
})
emitter.emit(46)

The above will log:

44
45
46

Keywords

events

FAQs

Package last updated on 14 Sep 2022

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