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

event-iterable

v1.0.8

Published

wrapper that creates an AsyncIterable of events coming from an EventEmitter

Downloads

14

Readme

Class:EventIterable

const { EventIterable } = require( "eventIterable" );
import { EventIterable } from "eventIterable";

EventIterable is a wrapper for EventEmitter, creating an AsyncIterable of the requested events which the user can, in an async context, iterate through using a for await ... of statement. When being iterated over, EventIterable produces objects of type: { eventName:string|symbol, value:any } where eventName is one of the eventNames provided in the wrap function.

EventIterable.wrap( eventEmitter, eventNames[, signal ] )

  • eventEmitter <EventEmitter> the event emitter being wrapped
  • eventNames <string> | <symbol> | <(string|symbol)[]> the event names to be captured
  • signal <AbortSignal> optional signal from an AbortController to signal the EventIterable to stop as an alternative to eventIterable.stop()
  • Returns <EventIterable>

eventIterable.stop()

The stop function causes the EventIterable to stop handling events and exit its generator function.

Example

An example of using event-iterable to wrap an EventEmitter and use it as an AsyncIterable. The EventEmitter in this example alternates between two types of messages, "tick" and "tock" accompanied by a date object, which are emitted at a variable, random interval between 0 and 2 seconds. The example also shows how to use an AbortController (provided by the node-abort-controller package) to stop it, the non-controller version is simply the .stop() method.

const { EventEmitter } = require( "events" );

const { AbortController } = require( "node-abort-controller" );

const { EventIterable } = require( "event-iterable" );

class TimerEventEmitter extends EventEmitter {

    #tick = false;
    #timeout = undefined;

    constructor() {
        super();
        this.ticktock();
    }

    ticktock() {
        this.#tick = !this.#tick;
        this.emit( this.#tick?"tick":"tock", new Date() );
        this.#timeout = setTimeout( this.ticktock.bind( this ), Math.random()*2000 );
    }

    stop() {
        clearTimeout( this.#timeout );
    }
}

( async function main():Promise<void> {
    const eventEmitter = new TimerEventEmitter();
    // eventEmitter.on( "tick", console.log );
    // eventEmitter.on( "tock", console.log );

    // After 10 seconds, we'll abort using the Abort Controller
    const abortController = new AbortController();
    setTimeout( () => abortController.abort(), 10000 );

    const iterable = EventIterable.wrap( eventEmitter, ["tick", "tock"], abortController.signal );
    for await ( const event of iterable ) {
        console.log( event );
    }
    eventEmitter.stop();
} )();