Gathering detailed insights and metrics for ustor
Gathering detailed insights and metrics for ustor
A lightweight reactive store library that provides a powerful reactivity system for managing state, compatible with any UI framework, including, Solid.js, and Preact Signals.
npm install ustor
Typescript
Module System
Node Version
NPM Version
67.3
Supply Chain
98.6
Quality
81.8
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
802
Last Day
2
Last Week
5
Last Month
55
Last Year
802
1 Stars
10 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
0.1.0
Package Id
ustor@0.1.0
Unpacked Size
13.76 kB
Size
4.30 kB
File Count
6
NPM Version
10.8.2
Node Version
20.18.0
Publised On
12 Nov 2024
Cumulative downloads
Total Downloads
Last day
0%
2
Compared to previous day
Last week
-28.6%
5
Compared to previous week
Last month
57.1%
55
Compared to previous month
Last year
0%
802
Compared to previous year
This library provides a powerful reactivity system for creating stateful objects using signals, integrated seamlessly into JavaScript objects and arrays. This library does not depend on any specific reactive library and can be used by any UI framework to manage and react to changes in state effectively.
To use this library, you can install it using npm or yarn:
1npm install ustor 2yarn add ustor
This library can be integrated with solid-js
, @preact/signals-core
, ulive
, usignal
, oby
, sinuous
any other libarary you can think of for managing reactive signals.
First, import the store
function and the api
object. You need to initialize api
with your preferred signal implementation.
1import { store, api } from "./src"; 2import { createSignal } from "solid-js"; 3// import { signal } from '@preact/signals-core'; // 'usignal' 'ulive' 4 5// Solid.js setup 6api.signal = createSignal; 7api.get = (v) => v[0](); 8api.set = (signal, v) => signal[1](v); 9api.is = (v) => 10 (Array.isArray(v) && 11 typeof v[0] === "function" && 12 typeof v[1] === "function") || 13 v[0]?.name?.includes("readSignal"); 14 15// @preact/signals-core, usignal or ulive setup 16api.signal = signal; 17api.get = (v) => v.value; 18api.set = (signal, v) => (signal.value = v); 19api.is = (v) => v?.peek;
store(values, proto)
The store
function creates a stateful object that automatically converts properties into reactive signals.
The function returns a reactive store object that maintains the structure of the original values
while adding reactivity.
is(value)
The is
function checks if a given value is a reactive store instance.
Returns true
if the value is a reactive store, otherwise false
.
api.is
The api.is function checks if a given value is a signal.
Returns true if the value is a signal, otherwise false.
api.signal
The api.signal
function is used to create a reactive signal.
Returns a signal that can be used to create reactive state within the store.
api.get
The api.get
function retrieves the current value of a signal.
Returns the current value of the signal.
api.set
The api.set
function updates the value of a signal.
Updates the signal with the provided value.
1const s = store({ a: 1, b: 2 }); 2console.log(s.a); // 1 3s.a = 5; 4console.log(s.a); // 5
You can use the store
function to create a reactive state object. Assigning a new value to s.a
will automatically trigger updates wherever s.a
is used.
1const s = store({ 2 a: 2, 3 b: 3, 4 get sum() { 5 return this.a + this.b; 6 }, 7}); 8 9console.log(s.sum); // 5 10s.a = 5; 11console.log(s.sum); // 8
Here, you can define getters that automatically compute derived state values, and these getters will update whenever the underlying signals change.
The store
function can also handle deeply nested objects, converting nested properties into reactive signals:
1const s = store({ nested: { deep: { value: 10 } } }); 2const deepValue = s.nested.deep.value * 2; 3 4console.log(deepValue); // 20 5s.nested.deep.value = 15; 6console.log(s.nested.deep.value) * 2; // 30
This library can handle arrays and objects seamlessly, automatically wrapping array elements with reactivity:
1const s = store({ list: [1, 2, 3] }); 2const sum = s.list.reduce((acc, item) => acc + item, 0); 3 4console.log(sum); // 6 5s.list = [1, 5, 3]; 6console.log(s.list.reduce((acc, item) => acc + item, 0)); // 9
The signal value trigger with effects to track dependencies and automatically re-run whenever dependencies change:
1const s = store({ a: 1 }); 2let effectValue = 0; 3 4effect(() => { 5 effectValue = s.a * 2; 6}); 7 8console.log(effectValue); // 2 9s.a = 4; 10console.log(effectValue); // 8
state.$prop
The $
properties are automatically generated for each property in the store. These $
-prefixed properties contain the underlying signal, providing direct access to the signal itself, separate from the reactive value it holds. The $
properties are non-enumerable and are useful when you need to access or manipulate the signal directly, rather than the reactive value.
For example, if you have a store with a count property:
1const s = store({ count: 0 }); 2console.log(s.count); // 0 3console.log(s.$count); // The underlying signal object
This library can be used with Solid.js, Preact Signals, or any other UI framework to provide reactive signals.
To use Solid.js:
1import { createSignal } from "solid-js"; 2 3api.signal = createSignal; 4api.get = (v) => v?.[0](); 5api.set = (signal, v) => signal?.[1](v); 6api.is = (v) => 7 (Array.isArray(v) && 8 typeof v[0] === "function" && 9 typeof v[1] === "function") || 10 v?.[0]?.name?.includes("readSignal");
To use Preact Signals, usignal, ulive, etc:
1import { signal } from "@preact/signals-core"; 2 3api.signal = signal; 4api.get = (v) => v?.value; 5api.set = (signal, v) => (signal.value = v); 6api.is = (v) => v?.peek;
To use any signal library
1import ... from "..."; 2 3api.signal = ...; 4api.get = ...; 5api.set = (signal, value) => ...; 6api.is = (v) => ...; 7 8const state = store({ 9 count: 0 10});
The library provides a unified API to work with different reactive systems, allowing you to switch between Solid.js, Preact Signals, or any other UI framework easily.
This library is provided "as-is" under the MIT license. Feel free to use, modify, and distribute it in your projects.
No vulnerabilities found.
No security vulnerabilities found.