0.0.3 • Published 4 days ago
sgnls v0.0.3
sgnls
small, simple signals for the browser and node
an easy way to create and use signals in your code base, with a tiny footprint.
usage
npm i sgnls
import signal from 'sgnls';
const $favPasta = signal('lasagna');
$favPasta.effect(newValue => {
document.title = `my favorite pasta is ${newValue}`;
});
$favPasta.set('carbonara');
api
import
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.
get
returns the current value of the signal.
const $signal = signal('initial value');
$signal.get();
set
sets the value of the signal.
const $signal = signal('initial value');
$signal.set('new value');
update
updates the value of the signal by mutating it through a function.
const $signal = signal(['a', 'b', 'c']);
$signal.update(value => [...value, 'd']);
effect
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');
stop
stops the attached effects from invoking.
const $signal = signal('initial value');
$signal.stop();
license
mit