queue-event-emitter

Simple `EventEmitter` which runs every event handler in a queue

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
queue-event-emitter
202.1.14 years ago4 years agoMinified + gzip package size for queue-event-emitter in KB

Readme

queue-event-emitter
npm version build status
QueueEventEmitter is a simple EventEmitter which runs every event handler in a queue.

Installation

npm i --save queue-event-emitter

Usage

const QueueEventEmitter = require('queue-event-emitter')

// helper function
const sleep = (time) => (
  new Promise((resolve) => setTimeout(resolve, time))
)

// event emitter
const emitter = new QueueEventEmitter()

// event handlers, should return promise
emitter.on('first', async (data) => {
  await sleep(3000)
  console.log('data:', data)
})
emitter.on('second', async (data) => {
  await sleep(1000)
  console.log('data:', data)
})
emitter.on('third', async (data) => {
  await sleep(10)
  console.log('data:', data)
})

// send events
emitter.emit('first', 10)
emitter.emit('second', 20)
emitter.emit('third', 30)
emitter.emit('first', 40)
emitter.emit('third', 50)
emitter.emit('third', 60)
emitter.emit('second', 70)

// result in console will be
// data: 10
// data: 20
// data: 30
// data: 40
// data: 50
// data: 60
// data: 70

API

Implements the same api as node's EventEmitter.

Options

QueueEventEmitter class accepts following options: - options.concurrency <number> The maximum number of events to execute at once. (Default: 1)

Licence

Licensed under MIT