Home

Awesome

<em>µ</em>signal

Downloads Coverage Status build status

<sup>Social Media Photo by Carlos Alberto Gómez Iñiguez on Unsplash</sup>

A blend of @preact/signals-core and solid-js basic reactivity API, with API and DX mostly identical to @preact/signals-core but extra goodness inspired by solid-js, 803 bytes minified with brotli.

import {signal, computed, effect, batch, Signal} from 'usignal';
// const {signal, computed, effect, batch, Signal} = require('usignal');

signal(0) instanceof Signal;          // true
computed(() => {}) instanceof Signal; // true

effect(
  () => { console.log('fx') },
  void 0,       // optional value to pass along the callback as initial/prev value
  {async: true} // optionally make the effect async: false by default
);

// try every example shown in
// https://github.com/preactjs/signals
// or see test/index.js file to see it in action

Exports

This is a dual module so it works in either CommonJS or ECMAScript module systems.

Current exports are exactly these:

import {
  signal,
  computed,
  effect,
  batch,
  Signal
} from 'usignal';

The Signal export is useful only as brand check for either computed or signal references, but it cannot be used as constructor right away.

Exports - Extra

To allow developers to try and use different patterns there are a few variants of this module, still based on the very same core primitives:


Differently thought ...

const one = signal(1);
const two = signal(2);
const three = computed(() => one + two);

three.value;  // 3 indeed!

Benchmark

The benchmark currently compares S, solid, preact/signals, and cellx against usignal.

Please note preact is currently not able to solve nested effects so its logic might be simpler than other libraries.

npm run benchmark

current status

Tests

This module is 100% code covered, including the WeakRef possible leaks which is tested through the test/leak.js file, which is part of the build script process.

To use other libraries as reference, I have also added preact/signals-core and solid-js dev-dependencies within the test folder.

Please note preact is currently not able to solve nested effects so its logic might be simpler than other libraries.

The following instructions are needed to test other libraries too:

cd usignal
npm i
cd test
npm i
cd ..

# normal tests
npm test usignal      # shows also code-coverage
npm test solid
npm test preact

# leak test
npm run leak usignal  # calculate leaks via internals
npm run leak solid
npm run leak preact

About the leak test

This file is not meant at all as meaningful benchmark against other libraries, it's simply there to allow me to spot regressions on future updates of the library:

How to integrate with Lit

You create a following mixin function. Your class inherits from Mixin. Please see the demo for details.

import { effect, signal } from 'usignal';

export function WithUsignal(Base){
  return class WithUsignal extends Base {
    #disposeEffect
    #reactivePropUpdate = signal(0);

    disconnectedCallback() {
      this.#disposeEffect?.();
    }

    performUpdate() {
      if (!this.isUpdatePending) {
        return;
      }

      if (this.#disposeEffect) {
        this.#reactivePropUpdate.value++;
        return;
      }

      this.#disposeEffect = effect(() => {      
        this.isUpdatePending = true;
        this.#reactivePropUpdate.value;
        super.performUpdate();
      });
    }
  };
}