small, simple signals for the browser and node
an easy way to create and use signals in your code base, with a tiny footprint.
npm i sgnls
import signal from 'sgnls';
const $favPasta = signal('lasagna');
$favPasta.effect(newValue => {
document.title = `my favorite pasta is ${newValue}`;
});
$favPasta.set('carbonara');
sgnls
comes with a straightforward api. it exports one default function, which returns a signal object.
import signal from 'sgnls';
const $signal = signal('initial value');
said object then exposes the following five methods.
returns the current value of the signal.
const $signal = signal('initial value');
$signal.get();
sets the value of the signal.
const $signal = signal('initial value');
$signal.set('new value');
updates the value of the signal by mutating it through a function.
const $signal = signal(['a', 'b', 'c']);
$signal.update(value => [...value, 'd']);
sets up an effect to be called whenever the signal changes.
note: the effect is called once immediately after the setup!
const $signal = signal('initial value');
$signal.effect(newValue => {
console.log(newValue);
});
$signal.set('new value');
stops the attached effects from invoking.
const $signal = signal('initial value');
$signal.stop();
mit