Implementation of signals for Javascript. Simple reactivity system.
import signal from '@peter.naydenov/signals';
const
initalValue = 0
, signalNest = signal ()
, simple = signalNest.state ( initalValue )
;
simple.get () // => 0
simple.set ( 1 ) // => true
simple.get () // => 1
const complex = signalNest.computed ( () => simplel.get () + 10 );
complex.get () // => 11
signal.set ( 2 ) // => true
complex.get () // => 12
// Effect is executed immediately after signal state change
signalNest.effect ( [simple], () => {
console.log ( 'signal state changed' )
})
// Computed state is lazy evaluated. So effect will not be executed until it is called
signalNest.effect ( [complex], () => {
console.log ( 'computed effect' )
})
simple.set ( 3 ) // => true, => simple changed
// ... no execution of computed effect. Requires a call
complex.get () // -> 'computed effect'
npm install @peter.naydenov/signals
Call from your project:
import signal from '@peter.naydenov/signals';
// create a signal nest - a collection of signal states, computed states and effects
const signalNest = signal ();
// start using the signal library
const a = signalNest.state ( 0 );
signalNest.effect ( [a], () => console.log ( 'a was changed' ) );
// etc...
'@peter.naydenov/signals' was created and supported by Peter Naydenov.
'@peter.naydenov/signals' is released under the MIT License.