Skip to content

small, simple signals for the browser and node ๐Ÿ™‹๐Ÿผ

License

Notifications You must be signed in to change notification settings

robinloeffel/sgnls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

19 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

sgnls

version on npm weekly downloads on npm types license

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