Skip to content
/ event-x Public

Lightest javascript event emitters written the pure functional programming way

License

Notifications You must be signed in to change notification settings

iphong/event-x

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

event-x

Event emitter the functional programming way rewritten from the ground up. No more constructors, extends or mixins bullshit, it works seamlessly out of the box and you can implement it into your existing code base without any modification.

You can make anything in your application to be capable of emitting events, and when i say “anything” i really mean it. From React Components, Backbone Views, Containers, DOM Elements or just simply any plain objects, And it presence is completely transparent and you can opt out anytime.

Install

npm install event-x

Basic Usage

import events from 'event-x'

const dog = {}

events.on(dog, 'bark', () => console.log('dog barked'))

events.emit(dog, 'bark')// should log "dog barked"

events.off(dog, 'bark')

events.emit(dog, 'bark') // Nothing will happen

Asynchronous operation

If any listener return a promise, then the setState will wait for all of them to be resolved first. Since this is ES6 Promise therefore it fully supports async / await operation

(async () => {
  const container = {}

  const delay = () => new Promise(resolve => setTimeout(resolve, 3000))

  events.on(container, 'foo', delay)

  await events.emit(container, 'foo')
  await events.emit(container, 'foo')
  console.log('Done') // shoud log "Done" after 6 seconds
})()

It works on anything

Attach listeners to any objects by calling events.on()

Trigger events on any objects by calling events.emit()


Check out my other library Container-X for easy state management in your React application.

About

Lightest javascript event emitters written the pure functional programming way

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published