reactive-signals

2.0.1 • Public • Published

reactive-signals

This is a lightweight library which is all about reactive and asynchronous programming.

Your primitive is a signal, it can hold one value at a time:

const { createSignal } = require('reactive-signals');
const signal = createSignal();
signal.value = 42;

When a value is changed, you can be notified of the change:

const signal = createSignal();
let triggerCount = 0;
signal.event(() => triggerCount++);
signal.value = 42; // trigger count changes from 0 to 1

Sometimes the object listening to the signal might be detroyed, in this case you will need to unsubscribe from the signal.

const signal = createSignal();
let triggerCount = 0;
const subscription = signal.event(() => triggerCount++);
signal.value = 42; // triggerCount changes from 0 to 1
subscription.cancel();
signal.value = 13; // triggerCount stays 1 because we unsubscribed

There are a few more things which help manage compelxity and improve performance, signals will detect if the value changes and only trigger an event if the value has indeed changed:

const signal = createSignal();
let triggerCount = 0;
signal.value = 13;
signal.event(() => triggerCount++);
signal.value = 13; // no change
signal.value = 42; // triggerCount changes to 1
signal.value = 42; // triggerCount stays 1

You can supply your own object comparer:

const signal = createSignal({x:1,y:2}, {
    equals: (a,b) => a.x === b.x && a.y === b.y
});
let triggerCount = 0;
signal.event(() => triggerCount++);
signal.value = {x:1, y:2}; // no change
signal.value = {x:1, y:3}; // triggerCount changes to 1
signal.value = {x:1, y:3}; // triggerCount stays 1

other features

Best check out the code. Typedef file contains additional documentation Tests cover additional use cases

Readme

Keywords

none

Package Sidebar

Install

npm i reactive-signals

Weekly Downloads

1

Version

2.0.1

License

ISC

Unpacked Size

13.3 kB

Total Files

8

Last publish

Collaborators

  • peterekepeter