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

min-signal

v1.0.2

Published

lightweight signal with priority support.

Downloads

521

Readme

min-signal

min-signal is a lightweight version of js-signals which is heavily inspired by Robert Penner’s AS3-Signals. Signal is an alternative to Events/Promises/Callbacks. To know more about the differences, you can check out the Wiki page of js-signals.

Unlike other trimmed version js-signals, min-signal preverses some useful features like priorty and arguments injection which comes in handy during the development.

Dispatch the same signal within the signal callback is not recommended. If it happens the new signal execution will kick in and stop the unfinished execution.

Examples

Basic Example

Example of using the basic functions: add(), dispatch() and remove() with min-signal

var onStarted = new MinSignal();

function callback(param1, param2) {
    console.log(param1 + ' ' + param2);
}

onStarted.add(callback); //add listener
onStarted.dispatch('foo', 'bar'); //dispatch signal passing custom parameters
onStarted.remove(callback); //remove a single listener

addOnce()

If you want the callback to be dispatched only once, you can use addOnce().

var onStarted = new MinSignal();

function callback() {
    console.log('hello');
}

onStarted.addOnce(callback);
onStarted.dispatch(); // log : hello
onStarted.dispatch(); // do nothing

Priority

The third argument of the add() and addOnce() is the priority, the higher it is, the earlier it will be dispatched. The default value is 0, and order by first comes first served principle.

var onStarted = new MinSignal();

function callback1() {
    console.log('1');
}

function callback2() {
    console.log('2');
}

function callback3() {
    console.log('3');
}

onStarted.add(callback1);
onStarted.add(callback2);
onStarted.add(callback3, null, 10);
onStarted.dispatch(); // log : 3, 1, 2

Context

Like function binding, you can provide the context for the callback binding. Same context with the same function will be ignored.

var onStarted = new MinSignal();

function callback() {
    console.log(this.id);
}

var a = {id: 'a'};
var b = {id: 'b'};

onStarted.add(callback, a);
onStarted.add(callback, a);
onStarted.add(callback, b);
onStarted.dispatch(); // log : a, b

Argument prefix

You can also add argument prefix with the add() and addOnce() like in Function.bind(). One thing need to keep in mind is that, for the duplicated callback checking, it only check the function and the context, same function with the same context with new argument prefix will be rejected instead of overriding.

var onStarted = new MinSignal();

function callback(param1, param2, param3, param4) {
    console.log(param1 + param2 + param3 + param4);
}

onStarted.add(callback, null, 0, 'a', 'b');
onStarted.dispatch('c', 'd'); // log : abcd

Stop Propagation

You can stop propagation by returning false in the callback function. By stopping propagation, in the dispatch() call it will return the listener which triggered the stop propagation.

var onStarted = new MinSignal();

onStarted.add(function(){
	return false; // returns "false" to stop propagation
});
onStarted.add(function(){
	console.log('wont be triggered'); // this function won't be trigger
});

onStarted.dispatch(); // log : {...} // will return the listener that stopped the propagation

Installation

Manually

Download min-signal here.

Npm

Check out the npm page here.

Testing

run npm run test to run the test suite.

TODO

  • additional features?
  • add more tests

License

MIT