Skip to content

substrate-system/signs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

signs

tests types module semantic versioning Common Changelog install size GZip size dependencies

A smaller, simpler signals.

This is not as magical or robust as other implementations, but it is very small. Look at alien-signals or @preact/signals if you need more magic or functionality.

Contents

Install

npm i -S @substrate-system/signs

Example

Counting clicks.

See the live demo of this.

This example is only 886 bytes after minifying and gzipping.

import { effect, sign } from '@substrate-system/signs'
const qs = document.querySelector.bind(document)

const count = sign(0)

qs('#root').innerHTML = `
    <h1 class="count">${count.value}</h1>
    <button class="plus">Plus</button>
    <button class="reset">Reset</button>
`

effect(() => {
    qs('h1').innerHTML = count.value
})

qs('button.reset').addEventListener('click', ev => {
    ev.preventDefault()
    count.value = 0
})

qs('button.plus').addEventListener('click', ev => {
    ev.preventDefault()
    count.value++
})

API

sign

Create a new sign.

type Sign<T> = {
    value:T
    peek:()=>T
}

function sign<T> (value:T, options?: SignOptions):Sign<T>

sign.peek

Get the sign's current value, but do not subscribe to that sign.

type Sign<T> = {
    value:T
    peek:()=>T
}

.peek Example

import { sign, effect } from '@substrate-system/signs'
const delta = sign(0)
const count = sign(0)

effect(() => {
    // Update `count` without subscribing to `count`:
    count.value = count.peek() + delta.value;
})

// rerun the effect
delta.value = 1

// Do not rerun the effect,
// b/c we used `.peek`, not `.value`
count.value = 10

effect

Call the subscriber function any time the sign's value changes.

function effect (fn:()=>any):()=>void

computed

Create a new sign that will update whenever the root sign changes.

function computed<T> (fn:()=>T):Sign<T>

computed example

import { sign, computed } from '@substrate-system/signs'

const hello = sign('hello')
const derived = computed(() => {
    return hello.value + ' world'
})

hello.value = 'goodbye'

console.log('derived value', derived.value)
// => 'goodbye world'

batch

Combine multiple signal writes into one single update that is triggered when the callback completes.

function batch<T> (fn:()=>T):T

batch Example

import { sign, computed, effect, batch } from '@substrate-system/signs'

const name = sign('Jane')
const surname = sign('Doe')
const fullName = computed(() => name.value + ' ' + surname.value)

// Will be called initially and once after the batch completes
effect(() => console.log(fullName.value))
// => Jane Doe
// => Foo Bar

// Combines both signal writes into one update.
batch(() => {
    name.value = 'Foo'
    surname.value = 'Bar'
})

Modules

This exposes ESM and common JS via package.json exports field.

ESM

import { sign, effect, computed, batch, CycleError } from '@substrate-system/signs'

Common JS

const { sign, effect, computed, batch, CycleError } = require('@substrate-system/signs')

pre-built JS

This package exposes minified JS files too. Copy them to a location that is accessible to your web server, then link to them in HTML.

copy

cp ./node_modules/@substrate-system/signs/dist/index.min.js ./public/signs.min.js

HTML

<script type="module" src="/signs.min.js"></script>

Develop

Example

Start a localhost server of the example directory:

npm start

Filesizes

A convenient script that will tell you the filesize of the example app:

npm run size

Test

npm test | npx tap-spec

See also

About

Smaller, simpler signals

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •