A tiny typed messaging system inspired by js-signals that uses ES2015 sets

Path to Version 2

Version 2 of micro-signals will introduce some breaking interface changes. Most notably will be a
shift from using bindings to a remove method on the signal itself. While bindings were a very nice
interface, these changes will allow us to achieve late listener support (memorized signals) in a
synchronous signal with a reduced chance for user error.

Previously, adding late listener support had been put off due to the inability to access the return
value of a listener during the initial cached dispatch of the signal (which would need to happen
while adding the signal). In addition, attempting to use the binding could throw an error during
the initial call if the user did not check for the availability of a binding. We considered
several ways around these issues:

Make micro-signals asynchronous

If a dispatch always calls the listener asynchronously then the binding is always available
during the initial call of the listener function, whether a cached value or not. However, some
projects may require the use of micro-signals in an environment where asynchronous behavior
is not desirable or easy to implement. A synchronous signal still supports asynchronous
dispatching (an asynchronous action can be triggered from the listener), but an asynchronous
signal does not allow for synchronous action dispatching.

Pass a binding to the listener

This would ensure that the listener always had a valid reference to the binding. However, the
user still needs a way to detach a listener that has not been called yet which, if we
continued to use the binding interface, would mean there is still a binding returned from the
add function. This could be an easy spot for user error, as forgetting to use the binding
passed into a listener would cause the same errors mentioned above.

Do not use bindings

If we use add and remove methods on signals, we can be sure we are always able to remove during
a listener add operation.

Several variations of the above were considered, but at the end of it all, option 3 seemed to be the
cleanest way to provide a synchronous signal with late listener support that avoided the most
opportunities for user error.

In order to add some of the convenience of bindings back to the API, there are plans to add the
ability to tag a listener and then remove listeners based on either listener or tags.

About

micro-signals is an attempt to provide a simple and flexible signal library for TypeScript and
JavaScript consumption. It borrows ideas from libraries such as
RxJS and
js-signals.

The library has no relation to mini-signals and the
name micro-signals is not meant to imply that this library is smaller than mini-signals in any way.
The package was named and published before it was noticed that there already was a mini-signals
library. Also, it may not seem very "micro" at this point. The original implementation was
much smaller and
at the time the name made more sense. However, the hope is that this library can provide a very
useful signal interface and remain as "micro" as possible.

Usage

Install using npm install micro-signals.

Signal

Signals allow adding and removing listeners, similar to js-signals, but with methods that can
create new transformed signals (such as map and filter). These methods are similar to the matching
methods on a JavaScript array. Each provides a new Signal (it does not modify the current signal)
and is chainable. All of the Signals described below (MappedSignal, FilteredSignal, etc.) have a
matching method on the base Signal.

Basic Usage of a Signal

import{Signal}from'micro-signals';

import*asassertfrom'assert';

constsignal=newSignal<string>();

constreceived:string[]=[];

constlistener=(payload:string)=>{

received.push(payload);

};

signal.add(listener);

signal.dispatch('a');

signal.remove(listener);

signal.dispatch('b');

assert.deepEqual(received,['a']);

Using Tags

Listeners can also be added and removed with tags. Tags can be provided with the listener to the add
function. If remove is then called with the listener or tag, the listener will be correctly removed.
An example might be tagging a listener with an instance of the class from within a class, we can
then remove the listener with only the reference to this. This functionality is provided to
partially mitigate the loss of bindings and the difficulty that keeping track of listeners can pose
in some cases. However, this interface does not provide the full convenience of bindings. If there
are any ideas on improvements in this area, suggestions are welcome.

import{Signal}from'micro-signals';

import*asassertfrom'assert';

classTestConsumer{

receivedPayloads:number[]=[];

constructor(private_signal:Signal<number>){

this._signal.add(x=>this.receivedPayloads.push(x),this);

}

detach(){

this._signal.remove(this);

}

}

constsignal=newSignal<number>();

consttestConsumer=newTestConsumer(signal);

signal.dispatch(1);

testConsumer.detach();

signal.dispatch(2);

assert.deepEqual(testConsumer.receivedPayloads,[1]);

Using the Extended Signal Interface

import{Signal}from'micro-signals';

import*asassertfrom'assert';

constsignal=newSignal<string>();

constreceived:string[]=[];

signal

.filter(payload=>payload==='world')

.map(payload=>`hello ${payload}!`)

.add(payload=>received.push(payload));

signal

.filter(payload=>payload==='moon')

.map(payload=>`goodnight ${payload}!`)

.add(payload=>received.push(payload));

signal.dispatch('world');

signal.dispatch('sun');

signal.dispatch('moon');

assert.deepEqual(received,['hello world!','goodnight moon!']);

Static Methods

Signal.merge

Signal.merge takes an arbitrary number of signals as constructor arguments and forward payloads from
all of the provided signals to the returned signal. This allow multiplexing of Signals. This matches
the behavior of the Signal.merge instance method.

import{Signal}from'micro-signals';

import*asassertfrom'assert';

constsignal1=newSignal<string>();

constsignal2=newSignal<string>();

constmergedSignal=Signal.merge(signal1,signal2);

constreceived:string[]=[];

mergedSignal.add(payload=>{

received.push(payload);

});

signal1.dispatch('Hello');

signal2.dispatch('world');

signal1.dispatch('!');

assert.deepEqual(received,['Hello','world','!']);

Signal.promisify

Turn signals into promises. The first argument is a resolution signal. When the resolution signal is
dispatched the promise will be resolved with the dispatched value. The second argument is an
optional rejection signal. When the rejection signal is dispatched the promise will be rejected with
the dispatched value. This matches the behavior of the Signal.promisify instance method.

Instance Methods

Signal.cache

Signal.cache is used to provide late listener support to signals. It takes a cache instance that
is responsible for determining what values are dispatched to late listeners, and returns a signal
that will provide all cached values to new listeners.

These caches implement a simple interface consisting of an add function to process values
that have been dispatched by the base signal and a forEach function to iterate over values that
should be provided to late listeners.

Two basic cache types are provided to cover the most common use cases for cached signals.
ValueCache replays the most recent value (similar to memorize in js-signals) and CollectionCache
replays all dispatched values.

Please note, there is currently not a way to unbind the cached signal from its base signal. This has
the potential to leak attached listeners. In practice this may not be an issue for most use cases.
Many use cases may have the base signal and the cached signal on the same object. For example:

import{Signal,ValueCache}from'micro-signals';

import*asassertfrom'assert';

classToggleState{

private_stateChanged=newSignal<boolean>();

publicstateChanged=this._stateChanged.cache(newValueCache());

publicsetState(state:boolean){

this._stateChanged.dispatch(state);

}

}

consttoggleState=newToggleState();

toggleState.setState(true);

letstate=undefined;

toggleState.stateChanged.add(toggleState=>state=toggleState);

assert.strictEqual(state,true);

In this case both signals will become unreachable at the same time, and therefore the base signal
will never prevent any cleanup of the cached signal and its context. In many other cases this
leaking may be negligible as well. However, if this functionality is desired, please file an issue
or pull request against the repository.

Signal.readOnly

readOnly provides a wrapper around a signal with no dispatch method. This is primarily used to
publicly expose a signal while indicating that consumers of the signal should not dispatch the
signal.

import{Signal}from'micro-signals';

import*asassertfrom'assert';

constsignal=newSignal<string>();

constreadOnlySignal=signal.readOnly();

constreceived:string[]=[];

readOnlySignal.add(payload=>{

received.push(payload);

});

assert.equal((readOnlySignalasany).dispatch,undefined);

signal.dispatch('a');

assert.deepEqual(received,['a']);

Signal.filter

Signal.filter provides the ability to filter values coming through a Signal, similar to filtering an
array in JavaScript.

import{Signal}from'micro-signals';

import*asassertfrom'assert';

constsignal=newSignal<number>();

constfilteredSignal=signal.filter(x=>x>=0);

constreceived:number[]=[];

filteredSignal.add(payload=>{

received.push(payload);

});

[-4,0,6,-2,8,0].forEach(x=>signal.dispatch(x));

assert.deepEqual(received,[0,6,8,0]);

Signal.filter also returns a signal of the correct type when filtering using a type predicate.

Signal.map

Signal.map provides the ability to transform payloads coming through a Signal, similar to mapping an
array in JavaScript.

import{Signal}from'micro-signals';

import*asassertfrom'assert';

constsignal=newSignal<string>();

constmappedSignal=signal.map(x=>`${x}!`);

constreceived:string[]=[];

mappedSignal.add(payload=>{

received.push(payload);

});

['cat','dog','frog','sloth'].forEach(x=>signal.dispatch(x));

assert.deepEqual(received,['cat!','dog!','frog!','sloth!']);

Signal.merge

Signal.merge takes an arbitrary number of signals as constructor arguments and forward payloads from
all of the provided signals and the base signal. This allow multiplexing of Signals. Effectively it
merges the provided signals with the base signal.

import{Signal}from'micro-signals';

import*asassertfrom'assert';

constsignal1=newSignal<string>();

constsignal2=newSignal<string>();

constmergedSignal=signal1.merge(signal2);

constreceived:string[]=[];

mergedSignal.add(payload=>{

received.push(payload);

});

signal1.dispatch('Hello');

signal2.dispatch('world');

signal1.dispatch('!');

assert.deepEqual(received,['Hello','world','!']);

Signal.promisify

Turn signals into promises. The base signal is a resolution signal. When the resolution signal is
dispatched the promise will be resolved with the dispatched value. The second argument is an
optional rejection signal. When the rejection signal is dispatched the promise will be rejected with
the dispatched value.

ExtendedSignal

An ExtendedSignal class is provided for the creation of a custom signal or wrapping a basic signal
(containing only an add method) with the remainder of the methods found on a micro-signals Signal
(such as map, filter, and so on). In many cases this gives us an easy way to ensure an intermediate
Signal does not block garbage collection, and in some cases may present a simpler way to obtain a
desired interface. For example, this makes it possible to extend the ExtendedSignal class to get the
Signal interface without having to expose the add method anywhere.

Compare the following code examples:

import{ExtendedSignal}from'micro-signals';

importEventEmitter=require('events');

import*asassertfrom'assert';

constemitter=newEventEmitter();

constsignal=newExtendedSignal<number>({

add(listener){

emitter.on('event',listener);

},

remove(listener){

emitter.removeListener('event',listener);

},

});

constreceived:number[]=[];

signal.map(x=>x+1).add(payload=>received.push(payload));

emitter.emit('event',1);

emitter.emit('event',2);

assert.deepEqual(received,[2,3]);

import{Signal}from'micro-signals';

importEventEmitter=require('events');

import*asassertfrom'assert';

constemitter=newEventEmitter();

constsignal=newSignal<number>();

emitter.on('event',payload=>signal.dispatch(payload));

constreceived:number[]=[];

signal.map(x=>x+1).add(payload=>received.push(payload));

emitter.emit('event',1);

emitter.emit('event',2);

assert.deepEqual(received,[2,3]);

Though the second is more terse, there is always a listener connected to the underlying emitter
object. In some cases this may prevent proper garbage collection. In the top example, the Signal
only acts as a transformation layer, transforming listeners to provide the interface of a Signal
without storing any state itself. This is how the internal signal transformations work in order to
remove unnecessary intermediate signals. Feel free to use or ignore the ExtendedSignal at your
discretion.

Interfaces

Several interfaces are exported as well for convenience:

Listener is an interface that defines a function that can be passed to Signal methods taking a
listener (such as add or addOnce).