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

@jancassio/dispatcher

v1.0.1

Published

A lightweight dispatcher object for general usage.

Downloads

2

Readme

Dispatcher

Twitter: @jancassio npm version

A very simple, lightweight and easy-to-use dispatcher object.

Why another dispatcher?

There are my reasons to re-invent a dispatcher object for javascript projects.

  1. So many features probably you don't (or never) use.
  2. Heavy file output.
  3. Not so easy to extend or not flexible.
  4. Dependencies.
  5. Needs some compiler/transpiler to use with ES5 (classic javascript syntax).

What this dispatcher is?

  1. Enough features, only emit, listen and unlisten events.
  2. Lightweight for any kind of project.
  3. Easy to extend, pretty simple flexibility.
  4. Zero dependencies.
  5. Works perfectly in ES5 and ES6.

Usages

Here is the most simple way

import Dispatcher from '@jancassio/dispatcher';

// Constant to be reused in event types, useful to avoid typos, across usage.
const EVENT_GO = 'event.go';

// A dispatcher's event handler.
function onEventGo (event) {
  console.log('[ onEventGo ]',
    'event.type: ', event.type,
    'event.payload', event.payload
  )
  
  // For unlisten/unsubscribe event. 
  Dispatcher.off( EVENT_GO, this.onEventGO );
}

// An action to emit event.
function sendEvent () {
  Dispatcher.emit(EVENT_GO, {message:"Hello World"});
}

// Register onEventGo to be called when 'event.go' be emitted.
Dispatcher.on(EVENT_GO, this.onEventGo);

// send the event after 1 second
setTimeout(this.sendEvent.bind(this), 1000);

Sometimes, you want to extend to be part of your structure, no problem.

import Dispatcher from '@jancassio/dispatcher';

function MyClass () {}

MyClass.prototype = Object.create(Dispatcher, {
  constructor: MyClass
});

Some situations require to handle many emitters with a single handlers, also you need to flag "who is emitting an event?", ok here is.

import Dispatcher from '@jancassio/dispatcher';

// Constant to be reused in event types, useful to avoid typos, across usage.
// ES6 classProperties module available only
var EVENT_GO = (id = 0) => { return `event.go.${id}` };
// or ES5
// var EVENT_GO = function (id) { return 'event.go.' + (id || 0); }

// A dispatcher's event handler.
function onEventGo (event) {
  console.log('[ onEventGo ]',
    'event.type: ', event.type,
    'event.payload', event.payload
  )
}

// An action to emit event.
function sendEvent (id) {
  Dispatcher.emit(EVENT_GO(id), {message:"Hello World"});
}

// Register onEventGo to be called when 'event.go' be emitted.


// Dirty simple example of emit many events to a single handler with
// a event identifier.

for (var i = 100; i > 0; i--) {
  Dispatcher.on(EVENT_GO(i), onEventGo);
  setTimeout(this.sendEvent.bind(this, i), 1000)
}

Contribution

I really like to receive feedbacks. Bugs, features, etc, are always welcome. I just want to make sure, this dispatcher is helpful and works as excpected to everyone.

License

MIT