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

do.js

v1.0.0

Published

Minimalistic Event Dispatcher

Downloads

12

Readme

do.js

Minimalistic Event Dispatcher

About

do.js is a simple event dispatching library written in < 20 lines of code.

See usage on how to use it.

function Do(parent) {
	var listeners = [];
	this.do = function(callback) {
		listeners.push(callback);
	};
	this.undo = function(callback) {
		listeners.splice(listeners.indexOf(callback), 1);
	};
	this.fire = function() {
		for (var v = 0; v < listeners.length; v++) {
			listeners[v].apply(parent, arguments);
		}
	};
}

Why Do?

Not that I want to create yet another xyzsource library/micro-framework, but here's the story.

While on the train to work, I was tinkling with source and needed a simple event listener system.

That's how I wrote this (in probably less than 5 minutes) just because I needed something simple and have something I could understand easily. The api also attempts to be english like while minimise typing.

Do.js can be pronounced as doh-dot-j-s, or do-dot-j-s. Doh like in play dough, do-ra-me, doraemon, domo. Do like in "do this", "I do", or a do-while loop. (My original name for this class would be "ondo", but nvm it..)

One difference between your usual way you handle

element.addEventListener('bla', function(something) {});
vs
element.onBla.on(function(something) {});

Of course, this library may not be of production quality or fitting your usual/preferred event dispatching style, in that case feel free to try out eventdispatcher.js or many other libraries out there.

This event handling approach is however nothing new, and my idea's inception probably started with Robert Penner's (the guy who also brought us easing/tweening equations) related work with Signals in as3. You may also like to check out this page which includes a good comparison of different observer pattern implementations.

Usage

function Cat() {
	this.name = 'cat';
	this.onSay = new Do(this); // Create an event listener type
}

Cat.prototype.says = function(something) {
	console.log('Cat says ' + something);
	this.onSay.fire(something); // Notifies listeners of something
}

var cat = new Cat();

cat.onSay.do(function(something) {  // Adds listener
	console.log('I hear ' + this.name + ' say ' + something);
});

cat.says('Meow!');

Node

Do supports node.js.

var Do = require('./do.js');

var onFire = new Do();
onFire.do(something)

onFire.fire('bla');

Tests

Not yet. You make a pull request.

Contact

For improvements, suggestions, you may use github issues. You may also subscribe to my Twitter: @blurspline

License

MIT