npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xlcyun/event-man

v0.2.7

Published

Event Man is a event emitter which support async/sync listener, and is flexiable to customize how to the wait async listeners to finish.

Downloads

2

Readme

Event-Man

Event Man is a event emitter which support async/sync listener, and is flexiable to customize how to the wait async listeners to finish.

installation

npm install --save @xlcyun/event-man

Usage

const EventMan = require("./EventMan.js")

let event = new EventMan()

event.on("finished", async name => {
  console.log("Finished by " + name)
})

await event.emit("finished", "Richard").once

Output:

Finished by Richard

As we know, async is a syntactic sugar of Promise, async function will return a Promise. When you emit an event, Event Man will call all the async/sync listener, and save the returned Promises.

If you access the getters all, any, race or allFinish, rather than once which has a side effect of clearing all the returned Promises, you can retrieve the results multiple times:

let allFinish = await event.emit("finished", "Richard").allFinish
...
let all = await event.all // retrieve using all getter.

You can call clear() to clear the Promises that created and stored in Event Man of the last time emition.

await event.emit("clear")
event.clear()
// or
await event.emit("clear").clear()

Be aware that emit function return the instance of Event Man itself.

call clearAll() to clear all the Promises stored in Event Man.

event.clearAll()

once

let result = await event.emit("finished").once

Return a Promise which works like Promise.all.

It also will clear the Promises that created by the last time emition. Therefore is equal to:

let result = await event.emit("finished").all
event.clear()

result will be a array contains the values of all async listeners return.

all

Works like Promise.all.

let result = await event.emit("finished").all

any

Works like Promise.any

let result = await event.emit("finished").any

race

Works like Promise.race

let result = await event.emit("finished").race

allFinish

let result = await event.emit("finished").allFinish

The Promise returned by allFinish will never be reject, but resolve with a array contains the results returned by async function(resolved value or rejected value), check on the example:

const resolvedFunc = async function() {
  return "resolved function"
}
const rejectedFunc = async function() {
  throw new Error("rejected")
}

const EventMan = require("./EventMan.js")

async function allFinishExample() {
  let event = new EventMan()

  event.on("allFinish", resolvedFunc)
  event.on("allFinish", rejectedFunc)

  let res = await event.emit("allFinish").allFinish
  console.log(res)
}
allFinishExample()

Output:

[ 'resolved function',
  Error: rejected
      at EventMan.rejectedFunc (E:\Project\EventMan\wziuqsp3w_code_chunk:5:9)
      at EventMan.emit (E:\Project\EventMan\EventMan.js:56:48)
      at allFinishExample (E:\Project\EventMan\wziuqsp3w_code_chunk:16:25)
      at Object. (E:\Project\EventMan\wziuqsp3w_code_chunk:19:1)
      at Module._compile (internal/modules/cjs/loader.js:776:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
      at Module.load (internal/modules/cjs/loader.js:653:32)
      at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
      at Function.Module._load (internal/modules/cjs/loader.js:585:3)
      at Function.Module.runMain (internal/modules/cjs/loader.js:829:12) ]