ustor 0.0.8 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -2
- package/dist/index.es.js +30 -24
- package/dist/index.js +30 -24
- package/dist/index.min.js +1 -1
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,6 +23,8 @@ This library provides a powerful reactivity system for creating stateful objects
|
|
|
23
23
|
- [Deeply Nested Structures](#deeply-nested-structures)
|
|
24
24
|
- [Arrays and Objects](#arrays-and-objects)
|
|
25
25
|
- [Reactivity and Effects](#reactivity-and-effects)
|
|
26
|
+
- [`state.$prop`](#stateprop)
|
|
27
|
+
|
|
26
28
|
- [Integration with Solid.js, Preact Signals or any](#integration-with-solidjs-preact-signals-or-any)
|
|
27
29
|
|
|
28
30
|
## Installation
|
|
@@ -188,9 +190,20 @@ s.a = 4;
|
|
|
188
190
|
console.log(effectValue); // 8
|
|
189
191
|
```
|
|
190
192
|
|
|
193
|
+
### `state.$prop`
|
|
194
|
+
|
|
195
|
+
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.
|
|
196
|
+
|
|
197
|
+
For example, if you have a store with a count property:
|
|
198
|
+
|
|
199
|
+
```js
|
|
200
|
+
const s = store({ count: 0 });
|
|
201
|
+
console.log(s.count); // 0
|
|
202
|
+
console.log(s.$count); // The underlying signal object
|
|
203
|
+
```
|
|
191
204
|
## Integration with Solid.js, Preact Signals or any
|
|
192
205
|
|
|
193
|
-
This library can be used with Solid.js, Preact Signals,
|
|
206
|
+
This library can be used with Solid.js, Preact Signals, or any other UI framework to provide reactive signals.
|
|
194
207
|
|
|
195
208
|
To use Solid.js:
|
|
196
209
|
|
|
@@ -233,7 +246,7 @@ const state = store({
|
|
|
233
246
|
});
|
|
234
247
|
```
|
|
235
248
|
|
|
236
|
-
The library provides a unified API to work with different reactive systems, allowing you to switch between Solid.js, Preact Signals,
|
|
249
|
+
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.
|
|
237
250
|
|
|
238
251
|
### License
|
|
239
252
|
|
package/dist/index.es.js
CHANGED
|
@@ -1,28 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
const api = {};
|
|
2
|
+
const _s = Symbol();
|
|
3
|
+
const is = (v) => v[_s];
|
|
4
|
+
const isObject = (v) => v && v.constructor === Object;
|
|
5
|
+
const resolve = (v) => Array.isArray(v) ? store(v) : isObject(v) ? Object.seal(store(v)) : v;
|
|
6
|
+
const store = (values, proto) => {
|
|
7
|
+
if ((is(values) && !proto) || !(api.signal && api.get && api.set && api.is)) return values;
|
|
8
|
+
if (Array.isArray(values)) values.forEach((item, i) => values[i] = is(item) ? item : store(item));
|
|
9
|
+
if (isObject(values)) {
|
|
10
|
+
let state = Object.create(proto || Object.getPrototypeOf(values));
|
|
11
|
+
Object.entries(Object.getOwnPropertyDescriptors(values)).forEach(
|
|
12
|
+
([key, desc]) => {
|
|
13
|
+
let signal = api.is(desc.value) ? desc.value : api.signal(resolve(desc.value)),
|
|
14
|
+
memo = desc.get ? (api.memo ? api.memo(desc.get.bind(state)) : desc.get.bind(state)()) : signal;
|
|
15
|
+
Object.defineProperty(state, key, {
|
|
16
|
+
get: () => (desc.get ? memo : api.get(signal)),
|
|
17
|
+
set: desc.get ? desc.set?.bind(state) : v => api.set(signal, resolve(v)),
|
|
18
|
+
enumerable: true,
|
|
19
|
+
});
|
|
20
|
+
if (!key.startsWith("$")) {
|
|
21
|
+
Object.defineProperty(state, "$" + key, {
|
|
22
|
+
get: () => signal,
|
|
23
|
+
enumerable: false,
|
|
19
24
|
});
|
|
20
25
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
}
|
|
27
|
+
);
|
|
28
|
+
Object.defineProperty(state, _s, { value: true });
|
|
29
|
+
return state;
|
|
30
|
+
}
|
|
31
|
+
return values;
|
|
32
|
+
};
|
|
27
33
|
|
|
28
34
|
export { api, is, store };
|
package/dist/index.js
CHANGED
|
@@ -1,31 +1,37 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
3
|
+
const api = {};
|
|
4
|
+
const _s = Symbol();
|
|
5
|
+
const is = (v) => v[_s];
|
|
6
|
+
const isObject = (v) => v && v.constructor === Object;
|
|
7
|
+
const resolve = (v) => Array.isArray(v) ? store(v) : isObject(v) ? Object.seal(store(v)) : v;
|
|
8
|
+
const store = (values, proto) => {
|
|
9
|
+
if ((is(values) && !proto) || !(api.signal && api.get && api.set && api.is)) return values;
|
|
10
|
+
if (Array.isArray(values)) values.forEach((item, i) => values[i] = is(item) ? item : store(item));
|
|
11
|
+
if (isObject(values)) {
|
|
12
|
+
let state = Object.create(proto || Object.getPrototypeOf(values));
|
|
13
|
+
Object.entries(Object.getOwnPropertyDescriptors(values)).forEach(
|
|
14
|
+
([key, desc]) => {
|
|
15
|
+
let signal = api.is(desc.value) ? desc.value : api.signal(resolve(desc.value)),
|
|
16
|
+
memo = desc.get ? (api.memo ? api.memo(desc.get.bind(state)) : desc.get.bind(state)()) : signal;
|
|
17
|
+
Object.defineProperty(state, key, {
|
|
18
|
+
get: () => (desc.get ? memo : api.get(signal)),
|
|
19
|
+
set: desc.get ? desc.set?.bind(state) : v => api.set(signal, resolve(v)),
|
|
20
|
+
enumerable: true,
|
|
21
|
+
});
|
|
22
|
+
if (!key.startsWith("$")) {
|
|
23
|
+
Object.defineProperty(state, "$" + key, {
|
|
24
|
+
get: () => signal,
|
|
25
|
+
enumerable: false,
|
|
21
26
|
});
|
|
22
27
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
Object.defineProperty(state, _s, { value: true });
|
|
31
|
+
return state;
|
|
32
|
+
}
|
|
33
|
+
return values;
|
|
34
|
+
};
|
|
29
35
|
|
|
30
36
|
exports.api = api;
|
|
31
37
|
exports.is = is;
|
package/dist/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var ustor=function(e){
|
|
1
|
+
var ustor=function(e){const t={},r=Symbol(),s=e=>e[r],i=e=>e&&e.constructor===Object,n=e=>Array.isArray(e)?a(e):i(e)?Object.seal(a(e)):e,a=(e,o)=>{if(s(e)&&!o||!(t.signal&&t.get&&t.set&&t.is))return e;if(Array.isArray(e)&&e.forEach(((t,r)=>e[r]=s(t)?t:a(t))),i(e)){let s=Object.create(o||Object.getPrototypeOf(e));return Object.entries(Object.getOwnPropertyDescriptors(e)).forEach((([e,r])=>{let i=t.is(r.value)?r.value:t.signal(n(r.value)),a=r.get?t.memo?t.memo(r.get.bind(s)):r.get.bind(s)():i;Object.defineProperty(s,e,{get:()=>r.get?a:t.get(i),set:r.get?r.set?.bind(s):e=>t.set(i,n(e)),enumerable:!0}),e.startsWith("$")||Object.defineProperty(s,"$"+e,{get:()=>i,enumerable:!1})})),Object.defineProperty(s,r,{value:!0}),s}return e};return e.api=t,e.is=s,e.store=a,e}({});
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ustor={})}(this,(function(e){
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ustor={})}(this,(function(e){const t={},r=Symbol(),o=e=>e[r],i=e=>e&&e.constructor===Object,n=e=>Array.isArray(e)?s(e):i(e)?Object.seal(s(e)):e,s=(e,a)=>{if(o(e)&&!a||!(t.signal&&t.get&&t.set&&t.is))return e;if(Array.isArray(e)&&e.forEach(((t,r)=>e[r]=o(t)?t:s(t))),i(e)){let o=Object.create(a||Object.getPrototypeOf(e));return Object.entries(Object.getOwnPropertyDescriptors(e)).forEach((([e,r])=>{let i=t.is(r.value)?r.value:t.signal(n(r.value)),s=r.get?t.memo?t.memo(r.get.bind(o)):r.get.bind(o)():i;Object.defineProperty(o,e,{get:()=>r.get?s:t.get(i),set:r.get?r.set?.bind(o):e=>t.set(i,n(e)),enumerable:!0}),e.startsWith("$")||Object.defineProperty(o,"$"+e,{get:()=>i,enumerable:!1})})),Object.defineProperty(o,r,{value:!0}),o}return e};e.api=t,e.is=o,e.store=s}));
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ustor",
|
|
3
3
|
"description": "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.",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.js",
|
|
7
7
|
"main": "./dist/index.js",
|