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

signalkit

v1.0.3

Published

Signals & delegates

Downloads

9

Readme

signalkit

Tiny signals library. Signals are an alternative to EventEmitter wherein each possible event is represented by a unique object to which listeners can be attached. There are a couple of benefits to this approach:

Anonymous signal objects can be passed around independently of their owner object

In the example below, MyObserver could be passed any signal at all:

function MyObserver(signal) {
    signal.connect(function() {
        // do something
    })
}

With EventEmitter, we'd need to attach to a fixed event of a given object, or pass in the event name explicitly. I find this less elegant.

function MyObserver(obj, eventName) {
    obj.on(eventName || 'data', function() {
        // do something
    })
}

No subtle bugs caused by misspelt event names

obj.on('foobar', function() { ... });
obj.emit('foobaz'); // nothing happens

Contrast with:

var obj = {};
obj.foobar = new Signal();
obj.foobar.connect(function() { ... });
obj.foobaz.emit(); // runtime error

Example

var Signal = require('signalkit').Signal;
var signal = new Signal('foo');

var unsub1 = signal.connect(function(name) {
    console.log("handler 1: " + name);
});

var foo = {
    bar: function(name) {
        console.log("handler 2: " + name);
    }
};

var unsub2 = signal.connect(foo, 'bar');

console.log("emit fred...");
signal.emit("fred");

console.log("emit bob...");
unsub1();
signal.emit("bob");

console.log("emit alice...");
unsub2();
signal.emit("alice");

API

Signals

var signal = new Signal(name, [parent])

Create a new signal with a given name and optional parent. If a parent is specified all calls to emit() will propagate recursively through the parent chain.

signal.connect(fn)

signal.connect_c(fn)

Connect a supplied function so it will be called when this Signal is emitted.

The _c suffixed version returns a function that can be called to cancel the connection.

signal.connect(object, methodName)

signal.connect_c(object, methodName)

Connect a supplied object/method to this Signal, i.e. call object[methodName]() when signal is emitted. The method lookup is lazy; that is, object[methodName] is resolved each time the signal is fired, rather than being bound at the time of connection.

The _c suffixed version returns a function that can be called to cancel the connection.

signal.once(fn)

signal.once_c(fn)

As per signal.connect(fn), except that the function is called once and once only.

The _c suffixed version returns a function that can be called to cancel the connection.

signal.once(object, methodName)

signal.once_c(object, methodName)

As per signal.connect(object, methodName), except that the method is called once and once only.

The _c suffixed version returns a function that can be called to cancel the connection.

signal.emit(args...)

Emit this signal, invoking each connected function with the given arguments. All handlers are guaranteed to fire; any errors thrown by handlers will be caught and re-raised asynchronously.

The emit() call will propagate recursively through the signal's parent chain.

signal.clear()

Remove all connections.

Error handling

You can catch - and handle - any errors thrown during event handling by overriding signal.onError, which receives the thrown error as an argument. Your error handler may return false to prevent any successive handlers from being fired. The default behaviour is to continue firing the remaining handlers and rethrow the error asynchronously.

The default error handler for all signals can be changed by setting Signal.prototype.onError.

TODO

  • Async emit. Not sure if this should be specified at signal creation time or emission time.