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

@uttori/event-dispatcher

v3.0.2

Published

An event bus system for registering, unregistering and triggering events.

Downloads

24

Readme

view on npm npm module downloads Build Status

Coverage Status Tree-Shaking Support Dependency Count Minified + GZip Minified

Uttori Event Dispatcher

An event bus system for registering, unregistering and triggering events.

Install

npm install --save @uttori/event-dispatcher

Example

const hooks = new EventDispatcher();
hooks.on('update', callback);
hooks.dispatch('update', { data }, this);
hooks.off('update', callback);

API Reference

EventDispatcher

An event bus system for registering, unregistering and triggering events.

Kind: global class
Properties

| Name | Type | Description | | --- | --- | --- | | events | Record.<string, UttoriEvent> | The collection of events to listen for. |

new EventDispatcher()

Creates a new EventDispatcher instance.

Example (new EventDispatcher())

const bus = new EventDispatcher();
bus.on('update', callback);
bus.dispatch('update', { data }, { context });
bus.off('update', callback);

eventDispatcher.events : Record.<string, UttoriEvent>

The collection of events to listen for.

Kind: instance property of EventDispatcher

eventDispatcher.validate(label, data, [context]) ⇒ Promise.<boolean>

Fires off an event with passed in data and context for a given label.

Kind: instance method of EventDispatcher
Returns: Promise.<boolean> - The conclusion of the spam checks, true being it is spam, false meaning it is clean.

| Param | Type | Description | | --- | --- | --- | | label | string | The human readable identifier of the event. | | data | any | Data to be used, updated, or modified by event callbacks. | | [context] | object | Context to help with updating or modification of the data. |

Example

is_spam = await bus.validate('check-for-spam', { data }, this);

eventDispatcher.filter(label, data, [context]) ⇒ Promise.<T>

Fires off an event with passed in data and context for a given label.

Kind: instance method of EventDispatcher
Returns: Promise.<T> - The original input data, either modified or untouched.

| Param | Type | Description | | --- | --- | --- | | label | string | The human readable identifier of the event. | | data | any | Data to be used, updated, or modified by event callbacks. | | [context] | object | Context to help with updating or modification of the data. |

Example

output = await bus.filter('loaded', { data }, this);

eventDispatcher.dispatch(label, data, [context])

Fires off an event with passed in data and context for a given label.

Kind: instance method of EventDispatcher

| Param | Type | Description | | --- | --- | --- | | label | string | The human readable identifier of the event. | | data | any | Data to be used, updated, or modified by event callbacks. | | [context] | object | Context to help with updating or modification of the data. |

Example

bus.dispatch('loaded', { data }, this);

eventDispatcher.fetch(label, data, [context]) ⇒ Promise.<Array.<T>>

Fires off an event with passed in data and context for a given label and returns an array of the results.

Kind: instance method of EventDispatcher
Returns: Promise.<Array.<T>> - An array of the results.

| Param | Type | Description | | --- | --- | --- | | label | string | The human readable identifier of the event. | | data | any | Data to be used by event callbacks. | | [context] | object | Context to help with updating or modification of the data. |

Example

popular = await bus.fetch('popular-documents', { limit: 10 }, this);

eventDispatcher.on(label, callback)

Add a function to an event that will be called when the label is dispatched. If no label is found, one is created.

Kind: instance method of EventDispatcher

| Param | Type | Description | | --- | --- | --- | | label | string | The human readable identifier of the event. | | callback | UttoriEventCallback.<any, any> | Function to be called when the event is fired. |

Example

bus.on('loaded', callback);

eventDispatcher.once(label, callback)

Add a function to an event that will be called only once when the label is dispatched. Uses the EventDispatcher.on method with a function wrapped to call off on use.

Kind: instance method of EventDispatcher

| Param | Type | Description | | --- | --- | --- | | label | string | The human readable identifier of the event. | | callback | UttoriEventCallback.<any, any> | Function to be called when the event is fired. |

Example

bus.once('one-time-process', callback);

eventDispatcher.off(label, callback)

Remove a function from an event.

Kind: instance method of EventDispatcher

| Param | Type | Description | | --- | --- | --- | | label | string | The human readable identifier of the event. | | callback | UttoriEventCallback.<any, any> | Function to be removed. |

Example

bus.off('loaded', callback);

EventDispatcher.check(label)

Verifies an event label.

Kind: static method of EventDispatcher

| Param | Type | Description | | --- | --- | --- | | label | string | The human readable identifier of the event. |

Example

EventDispatcher.check('event'); // No Error
EventDispatcher.check(1); // Throws Error

Tests

To run the test suite, first install the dependencies, then run npm test:

npm install
npm test
DEBUG=Uttori* npm test

Contributors

License