jsebrech / tiny-signals

The tiniest implementation of signals, ideal for vanilla JavaScript projects.
The Unlicense
73 stars 0 forks source link

Tiny Signals

The tiniest implementation of signals, ideal for vanilla JavaScript projects.

Based loosely on the signals API of Preact.

Part of the Plain Vanilla Web project.

Usage

Copy signals.js into your project.

Use it like this:

import { signal, computed } from './signals.js';

const name = signal('Jane');
const surname = signal('Doe');
const fullName = computed(() => `${name} ${surname}`, [name, surname]);
// Logs name every time it changes:
fullName.effect(() => console.log(fullName.value));
// -> Jane Doe

// Updating `name` updates `fullName`, which triggers the effect again:
name.value = 'John';
// -> John Doe

API:

Example

Run a static server:

npx http-server .

Browse to http://localhost:8080/example/adder.html

Other versions

Typescript: felixranesberger/tiny-signals-ts