Skip to content

wacky6/EventSource

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EventSource

A constrained but fast EventEmitter alternative.

Don't get confused with W3 EventSource!
This is a EventEmitter alternative (for simple situations). Not Server-Sent-Event!

Goal

  • No event name
  • Fast for one-or-two listeners

You are welcomed to help optimize this library

Usage

npm install node-event-source

let EventSource = require('node-event-source')
let es = new EventSource()
let callback = (arg)=>console.log(arg)
es.attach( callback )
es.emit('hello world')   // es.emit === callback

You can chain attach, detach together. But not emit

API

EventSource.attach(callback)

Add callback to listeners

EventSource.detach(callback)

Remove callback from listeners

EventSource.emit(arguments)

Emit event, calls all listeners
If there is only one listener, this method is the listener itself

Internals

If there is only one listener, this.emit directly set as listener
If there is more than one listener, they are stored in an Array, then called in a for-loop in this.emit()

Benchmark

node benchmark-EventSource.js

My result:

EE @1: 300ms
ES @1: 106ms
EE @3: 712ms
ES @3: 401ms
EE @10: 1390ms
ES @10: 1034ms

LICENSE

MIT

About

Constrained but faster EventEmitter

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published