Giter Club home page Giter Club logo

node-await-event-emitter's Introduction

await-event-emitter

Note: node-await-event-emitter is just implements the series processing, If you need parallel case, Please use the package tapable which is used by webpack.

Await events library like EventEmitter

build status Test coverage NPM version NPM Downloads

Why?

The concept of Webpack plugin has lots of lifecycle hooks, they implement this via EventEmitter. In the primitive events module on nodejs, the usage as follows

const EventEmitter = require('events')
const emitter = new EventEmitter()

emitter
  .on('event', () => {
    // do something *synchronously*
  })
  .emit('event', '...arguments')

The listener must be synchronous, that is way i wrote it.
And await-event-emitter support synchronous emitter magically 😄

Installation

npm install --save await-event-emitter

Usage

const AwaitEventEmitter = require('await-event-emitter').default

const emitter = new AwaitEventEmitter()
const tick = () =>
  new Promise((resolve) => {
    setTimeout(() => {
      console.log('tick')
      resolve()
    }, 1000)
  })

emitter.on('event', async () => {
  // wait to print
  await tick()
})

async function run() {
  // NOTE: it's important to `await` the reset process
  await emitter.emit('event', '...arguments')
  await emitter.emit('event', 'again')

  // support emit it synchronously
  emitter.emitSync('event', 'again')
}

run()

API

Class AwaitEventEmitter

  • addListener(event, listener) : AwaitEventEmitter
    alias: on
  • once(event, listener)
  • prependListener(event, listener) : AwaitEventEmitter
    alias: prepend
  • prependOnceListener(event, listener) : AwaitEventEmitter
    alias: prependOnce
  • removeListener(event, listener) : AwaitEventEmitter
    alias: off
  • listeners(event) : []
  • emit(event, ...args) : Promise.resolve(boolean)
    emit listeners asynchronously, we recommended await it resolved the result
  • emitSync(event, ...args) : boolean emit listeners synchronously

Test

npm test

Contributing

  • Fork it!
  • Create your new branch:
    git checkout -b feature-new or git checkout -b fix-which-bug
  • Start your magic work now
  • Make sure npm test passes
  • Commit your changes:
    git commit -am 'feat: some description (close #123)' or git commit -am 'fix: some description (fix #123)'
  • Push to the branch: git push
  • Submit a pull request :)

Authors

This library is written and maintained by imcuttle, [email protected].

License

MIT - imcuttle 🐟

node-await-event-emitter's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

node-await-event-emitter's Issues

Export default

Hi @imcuttle , I'm using your library and recently updated to the last version, and now I've started to get an issue on AwaitEventEmitter not been a constructor, this example from the README:

const AwaitEventEmitter = require('await-event-emitter')
const emitter = new AwaitEventEmitter()

Doesn't work anymore (at least on my project).
After some debug in order to make it work I had to add the default:

const AwaitEventEmitter = require('await-event-emitter').default
const emitter = new AwaitEventEmitter()

I'm not an expert, but I think this is because how the module is been exported:
exports.default = AwaitEventEmitter;
Instead of:
export default AwaitEventEmitter

As far I understand you moved to TS, so maybe this is related some compile configuration or something like that maybe?
I don't mind changing my code to include the missing .default but you should check it and change the README in case this is not an issue.

BTW: Great job with this module! I totally loved it! :)

Why do listeners also have to block/wait for each other?

Having emitter wait for all subscribers/listeners is quite useful however, I don't see a reason why multiple listeners have to block each other and executed in sequence (they should run independently). The listeners should run in parallel (i.e. Promise.all(listeners)).
(E.g. code below shows C being blocked until B is finished)

const AwaitEventEmitter = require('await-event-emitter').default

const emitter = new AwaitEventEmitter()
const tick = (name,args,time) =>
  new Promise((resolve) => {
    setTimeout(() => {
      console.log(`${name} -${args}`);
      resolve()
    }, time || 1000)
  })

emitter.on('event', async (args) => {  await tick('A',args) });
emitter.on('event', async (args) => {  await tick('B',args,4000) });
emitter.on('event', async (args) => {  await tick('C',args) });

async function run() {
  await emitter.emit('event', 'one')
  await emitter.emit('event', 'two')
  emitter.emitSync('event', 'three')
}
run();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.