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

@themost/events

v1.3.0

Published

Sync and async event emitters

Downloads

375

Readme

npm GitHub top language License GitHub last commit GitHub Release Date npm Snyk Vulnerabilities for npm package

MOST Web Framework Logo

@themost/events

Sync and async event emitters for both browser and node.js

Usage

npm i @themost/events

AsyncSeriesEventEmitter

Use AsyncSeriesEventEmitter for executing a collection of async event subscribers in series. The following example demonstrates a before load event which executes event subscribers and continues.

const { AsyncSeriesEventEmitter } = require('@themost/events');
class UserAction {
    
    constructor() {
        this.beforeLoad = new AsyncSeriesEventEmitter();
    }
    
    async load() {
        await this.beforeLoad.emit({
            target: this
        });
        this.dateCreated = new Date();
    }        
}

(async function () {
    const item = new UserAction();
    item.beforeLoad.subscribe((event) => {
        event.target.status = 'waiting';
    });

    item.beforeLoad.subscribe((event) => {
        return new Promise((resolve) => {
            // wait for something
            setTimeout(() => {
                event.target.status = 'active';
                resolve();
            }, 1000);
        });
    });
    await item.load();
    console.log('Loaded', 'status', item.status);
})().then(() => {
    //
});

AsyncEventEmitter

Use AsyncEventEmitter for executing a collection of async event subscribers in parallel.

const { AsyncEventEmitter } = require('@themost/events');
class UserAction {
    
    constructor() {
        this.beforeLoad = new AsyncEventEmitter();
    }
    
    async load() {
        this.beforeLoad.emit({
            target: this
        });
        this.dateCreated = new Date();
    }        
}

const item = new UserAction();
item.beforeLoad.subscribe((event) => {
    event.target.status = 'waiting';
});

item.beforeLoad.subscribe((event) => {
    return new Promise((resolve) => {
        // wait for something
        setTimeout(() => {
            event.target.status = 'active';
            resolve();
        }, 1000);
    });
});
item.load();

SyncSeriesEventEmitter

Use SyncSeriesEventEmitter for executing a collection of sync event subscribers in series. The following example demonstrates an after load event which executes event subscribers and continues.

const { SyncSeriesEventEmitter } = require('@themost/events');
class UserAction {
    constructor() {
        this.afterLoad = new SyncSeriesEventEmitter();
    }
    
    load() {
        this.status = 'unknown';
        this.afterLoad.emit({
            target: this
        });
    }        
}

const item = new UserAction();

item.afterLoad.subscribe((event) => {
    event.target.status = 'waiting';
    event.target.dateCreated = new Date();
});

item.afterLoad.subscribe((event) => {
    if (event.target.status === 'waiting') {
        event.target.status = 'active';
    }
});

// perform load
item.load();
console.log('Loaded', 'status', item.status);
console.log('Loaded', 'dateCreated', item.dateCreated);

ProcessEventEmitter

Use ProcessEventEmitter for sending and receiving process messages in both fork and cluster mode under node.js.

Import @themost/events/platform-server/register in your startup script

import '@themost/events/platform-server/register'

If your application is running in cluster mode, each message received by the primary process will be forwarded to each worker of a cluster. This operation is very important when you are implementing shared services across cluster workers and enables the communication between of them.

Start sending and receiving messages:

new ProcessEventEmitter().emit(msg);

...

new ProcessEventEmitter().subscribe((value) => {
    // write your code here
});