https://github.com/peternaydenov/signals
Signal for Javascript
https://github.com/peternaydenov/signals
Last synced: 4 months ago
JSON representation
Signal for Javascript
- Host: GitHub
- URL: https://github.com/peternaydenov/signals
- Owner: PeterNaydenov
- License: mit
- Created: 2025-01-11T07:44:10.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2025-01-25T08:51:00.000Z (4 months ago)
- Last Synced: 2025-01-25T09:25:32.448Z (4 months ago)
- Language: JavaScript
- Size: 75.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
- License: LICENSE
Awesome Lists containing this project
README
# Signals (@peter.naydenov/signals)





Implementation of signals for Javascript. Simple reactivity system.
Library cover all needed features: has states, effects and computed states.All `computed states` are lazy evaluated, computed evaluation is memorized for future calls. New evaluation will be executed only if dirty flag is set.
`Effects` are function that will be executed immediately after change of a watched state or computed state. If effect watches a state, will be executed imidiately after the state change. Effects that watch computed states are executed after next call when the value of that computed state is re-evaluated.
### What else?
Signal entities are living in a entity - `signal-nest`. Signal-nest is like a storage for related signal entities. Smaller size of the nest will work faster, will be easyer to understand the relations among signal entities. Nothing stops you from creating single signal-nest for everything but multiple signal-nests are more developer friendly.Provide a validation function for signal states. Check if value is valid and then set it.
## How it looks like
```js
import signal from '@peter.naydenov/signals';const
initalValue = 0
, signalNest = signal ()
, simple = signalNest.state ( initalValue )
;simple.get () // => 0
simple.set ( 1 ) // => true
simple.get () // => 1const 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'
```## Installation
```bash
npm install @peter.naydenov/signals
```Call from your project:
```js
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...
```## Usage - Practical Patterns
### Validation
Creating a state has optional validation function as an argument.```js
const a = signalNest.state ( 0, ( value ) => value > 0 );
a.set ( 1 ) // => true
a.set ( -1 ) // => false// Set function returns boolean so you can use it in if statements
if ( a.set ( 1 ) ) {
// if setting was successful...
}
else {
// if setting failed ...
}
```## Credits
'@peter.naydenov/signals' was created and supported by Peter Naydenov.## License
'@peter.naydenov/signals' is released under the [MIT License](https://github.com/PeterNaydenov/signals/blob/master/LICENSE).