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

emoney

v1.1.1

Published

A lightweight, isomorphic event emitter

Downloads

4

Readme

E$

A lightweight, isomorphic event emitter.

travis-ci

Overview

E$ can be used standalone or inherited by other classes.

// Standalone
const emoney = E$({
	handleE$() { ... }
});

// Inherited
class E$Extended extends E$ {
	handleE$() { ... }
}

E$ provides a clean way to interface with object instances.

emoney
	.$when('loading', (e, pct) => console.log('loading... (%s%)', pct))
	.$when('ready', () => console.log('ready!'))
	.$when('error', (e, err) => console.error(err.stack));

E$ instances can communicate via the handleE$ method.

const emitter = E$();
const watcher = E$({
	handleE$(e, str, obj) {
		expect(str).to.eql('awesome');
		expect(obj).to.eql({ rad: true });
	}
});
watcher.$watch(emitter);
emitter.$emit('gnarly', ['awesome', { rad: true }]);
expect(watcher.handleE$).to.have.been.called.once;

E$ can be used to create a DOM-like event tree.

const emitter = E$();
const watcher1 = E$();
const watcher2 = E$();
const spy = sinon.spy();

watcher2
	.$watch(watcher1)
	.$when('gnarly', spy);

watcher1
	.$watch(emitter)
	.$when('gnarly', e => e.stopPropagation());

emitter.$emit('gnarly', () => expect(spy).to.have.not.been.called);

Methods

(static) E$.is(subject) → {boolean}

Returns true if subject is E$-ish, false otherwise.

const emoney = E$();
const emoneyIsh = new E$Extended();
const somethingElse = new SomethingElse();

emoney instanceof E$;     // true
E$.is(emoney);            // true

emoneyIsh instanceof E$;  // false
E$.is(emoneyIsh);         // true

E$.is(somethingElse);     // false

.$when(events, argsopt, fnopt) → {instance}

Adds an event listener.

| Parameter | Type | Description | Required | | --------- | ---- | ----------- | -------- | | events | stringarray | The event(s) to be handled. | yes | | args | variantarray | The argument(s) to be bound to the event handler. | no | | fn | functionE$ | The event handler.If E$.is(fn) == true, the event will be bound to instance.handleE$.If fn is falsy, the event will be bound to emoney.handleE$. | no |

// basic use
emoney.$when('gnarly', () => { ... });

// bind an argument to multiple events
emoney.$when(['gnarly', 'rad'], 'arg', () => { ... });

.$once(events, argsopt, fnopt) → {instance}

Adds an event listener that is removed after the first time it is invoked.

| Parameter | Type | Description | Required | | --------- | ---- | ----------- | -------- | | events | stringarray | The event(s) to be handled. | yes | | args | variantarray | The argument(s) to be bound to the event fn. | no | | fn | functionE$ | The event handler. | no |

// basic use
emoney.$once('gnarly', () => { ... });

// bind an argument to multiple events
emoney.$once(['gnarly', 'rad'], 'arg', () => { ... });

.$emit(events, argsopt, callbackopt) → {instance}

Emits an event.

| Parameter | Type | Description | Required | | --------- | ---- | ----------- | -------- | | events | stringarray | The event(s) to be emitted. | yes | | args | variantarray | The argument(s) to be passed to the event handler. | no | | callback | function | A function to be executed at the end of the event chain (see event behavior). | no |

// basic use
emoney.$emit('gnarly', () => { ... });

// pass an argument to multiple event handlers
emoney.$emit(['gnarly', 'rad'], 'arg', () => { ... });

// pass multiple arguments to an event handler
emoney.$emit('gnarly', ['arg1', 'arg2'], () => { ... });

.$dispel(events, wildopt, fnopt) → {instance}

Removes an event listener.

| Parameter | Type | Description | Required | | --------- | ---- | ----------- | -------- | | events | stringarraynull | The event(s) to be removed. | yes | | wild | boolean | A boolean value denoting whether handlers bound to the wildcard event should be removed. | no | | fn | functionE$ | The event handler. | no |

// remove any gnarly listeners bound to fn
emoney.$dispel('gnarly', fn);

// remove all gnarly or rad listeners bound to any handler
emoney.$dispel(['gnarly', 'rad']);

// remove all listeners bound to fn except wildcard listeners
emoney.$dispel(null, fn);

// remove all listeners bound to fn
emoney.$dispel(null, true, fn);

// remove all listeners
emoney.$dispel(null, true);

.$watch(emitters) → {instance}

Starts watching E$ instance(s).

| Parameter | Type | Description | Required | | --------- | ---- | ----------- | -------- | | emitters | E$array | The E$ instance(s) to watch. | yes |

// watch a single emitter
listener.$watch(emitter1);

// watch multiple emitters
listener.$watch([emitter1, emitter2]);

.$unwatch(emitters) → {instance}

Stops watching E$ instance(s).

| Parameter | Type | Description | Required | | --------- | ---- | ----------- | -------- | | emitters | E$array | The E$ instance(s) to stop watching. | yes |

// stop watching a single emitter
listener.$unwatch(emitter1);

// stop watching multiple emitters
listener.$unwatch([emitter1, emitter2]);

Events

Properties

| Property | Type | Default | Description | | -------- | ---- | ------- | ----------- | | target | E$ | n/a | The event target. | | type | string | n/a | The event type. | | defaultPrevented | boolean | false | A flag denoting whether default was prevented. | | cancelBubble | boolean | false | A flag denoting whether propagation was stopped. | | timeStamp | number | n/a | The time at which the event was first triggered. |

Methods

.preventDefault()

Prevents the $emit callback from being executed.

emoney
	.$when('gnarly', (e) => {
		e.preventDefault();
		console.log('fn1');
	})
	.$when('gnarly', () => console.log('fn2'))
	.$emit('gnarly', () => console.log('cb'));

/**
 * > 'fn1'
 * > 'fn2'
 */

.stopPropagation()

Stops execution of the event chain and executes the emit callback.

emoney
	.$when('gnarly', (e) => {
		e.stopPropagation();
		console.log('fn1');
	})
	.$when('gnarly', () => console.log('fn2'))
	.$emit('gnarly', () => console.log('cb'));

/**
 * > 'fn1'
 * > 'cb'
 */

Behavior

normal execution

defaultPrevented

cancelBubble

Build & Test

npm i && npm run build