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

signals-lite

v1.0.0

Published

A propery based event (signal) dispatcher.

Downloads

7

Readme

SignalsLite.js

SignalsLite for JavaScript is an implementation of the Signals event pattern, modeled after AS3 Signals. In the signals paradigm, events are addressed as named properties rather than strings, and each event is an instance of the SignalLite object. The idea behind Signals was to create a dispatch method which required less boilerplate to implement and use, and to have a more formal contract between listener and dispatcher. Here's a quick example:

// using SignalLite to expose event
var myInterface = {
	clicked: new SignalLite(),
	loaded: new SiganlLite()
};

// subscribing to Signals
myInterface.clicked.add( function( obj ) {
	// respond to the click.
} );

// dispatching with arbitrary object
myInterface.clicked.broadcast( obj );

In JavaScript, the property based model (obj.clicked, vs. strings based model addEventListener( "clicked" ) ) has additional benefit over string based models, because when you type something wrong (obj.laoded.clicked) JavaScript will throw up an error, rather than silently adding an event listener to an event that will never fire.

The lightweight signals paradigm doesn't require an event object like the DOM model. No standard object is passed to the listener, unless you want it to. Instead, all arguments passed to the broadcast method will be passed on to the listeners. This is nice because you can actually pass whatever you want to the listeners, without having to create a custom Event object, or code up any boilerplate. Just pass the values or references your listeners will need, if any.

SignalDispatcher provides a safe dispatching model. If there is a JavaScript error thrown in a listener, it will not block the remaining signals. Care was taken to make sure errors are not supressed. Note: There is an error in Firefox where DOM events do suppress errors - there's a workaround in SignalsLite.js to report the error to the JavaScript console, but these errors will not trigger window.onerror. To compensate, there is an eachError function you can set to catch these errors.

Installation

Use Bower to install (Currently only SignalsLite):

bower install signals-lite

or npm (Currently only SignalsLite):

npm install signals-lite

API Quick Reference

These are the public methods and properties provided by a SignalLite instance:

SignalLite( target, eachReturn ) = {
	add           ( listener, target )
	addToTop      ( listener, target )
	once          ( listener, target )
	remove        ( listener )
	removeAll     ()
	getLength     () // returns the number of listeners
	broadcast     ()
	stopBroadcast ()
	eachReturn    property function
}
// all of SignalLite, plus
Signal( target, eachReturn ) = {
	namespace.add     ( "{yourNamespace}" )
	namespace.remove  ( "{yourNamespace}" )
	{yourNamespace} = {
		add       ( listener, target )
		addToTop  ( listener, target )
		once      ( listener, target )
		remove    ( listener )
		removeAll ()
	}
	priority( n ).add      ( listener, target )
	priority( n ).addToTop ( listener, target )
	priority( n ).once     ( listener, target )
}
// all of Signal and SignalLite with safe dispatch method
SignalDispatcher( target, eachReturn, eachError ) = {
	dispatch     ( ... rest )
	stopDispatch ()
	eachError    property function
}

There are 3 ways to add a listener to a Signal, add, once, and addToTop.

  • add adds the listener to the end of the list.
  • once adds the listener to the end of the list, and will only fire one time, then remove itself.
  • addToTop adds the listener to the beginning of the list.

All add methods can take a second argument which will set the this variable in the listener. This is useful if you are using a property of another object as a listener, and would like the this variable to resolve correctly.

All add and remove methods return the target specified in the constructor, to facilitate chainability.

Namespaces

Namespace support is modeled after jQuery event namespacing. This is great because it allows you to remove listeners even when you don't have access to a reference of the listener function, as in cases where an anonymous function was added, or the listener was added in another scope or closure. In SignalsLite.js, namespaces are property based, just like signals themselves. To create a namespace, register a namespace using the namespace property methods:

var signaled = new Signal();

// add the namespace
signaled.namespace.add( "module" );

// use the namespace
signaled.module.add( function() {
	// do something
} );

// This contrived closure makes referencing the listener from the outside impossible
(function() {
	var private = "some value";
	signaled.module.add( function() {
		// do something
	} );
} )();

// now we can remove the anonymous function and the scope protected listener
signaled.module.removeAll();

// Yay!

SignalsLite.js is unit tested.

MIT License.