solid-state-tools 1.3.0 → 1.3.1
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 +17 -6
- package/dist/atom.d.ts +1 -1
- package/dist/atom.d.ts.map +1 -1
- package/dist/atom.js +30 -8
- package/dist/atom.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Solid State Tools
|
|
2
2
|
|
|
3
|
+
Links: [GitHub Repository](https://github.com/ReedSyllas/solid-state-tools), [NPM Package](https://www.npmjs.com/package/solid-state-tools).
|
|
4
|
+
|
|
3
5
|
Solid State Tools is a collection of simple utilities for managing [Solid JS](https://docs.solidjs.com/) state.
|
|
4
6
|
|
|
5
7
|
All features are intended to compliment Solid JS's existing state system, building upon the existing foundation.
|
|
@@ -56,6 +58,14 @@ const count: Asig<number> = asig(0);
|
|
|
56
58
|
const count: Atom<Signal<number>> = atom(createSignal(0));
|
|
57
59
|
```
|
|
58
60
|
|
|
61
|
+
The second parameter (optional) is the config object, which is simply forwarded to the `createSignal` call.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
const list = asig([], { equals: false });
|
|
65
|
+
// is short for
|
|
66
|
+
const list = atom(createSignal([], { equals: false }));
|
|
67
|
+
```
|
|
68
|
+
|
|
59
69
|
## Couples (`createCouple`)
|
|
60
70
|
|
|
61
71
|
A signal can be summarized as a getter setter pair.
|
|
@@ -99,7 +109,7 @@ const [ double, setDouble ] = [
|
|
|
99
109
|
];
|
|
100
110
|
```
|
|
101
111
|
|
|
102
|
-
With that,
|
|
112
|
+
With that, the following statements work (as is expected of a setter):
|
|
103
113
|
|
|
104
114
|
```ts
|
|
105
115
|
setDouble(10); // double: 10, count: 5
|
|
@@ -107,13 +117,14 @@ setDouble(x => x + 1); // double: 11, count: 5.5
|
|
|
107
117
|
console.log(setDouble(20)); // Prints: 20
|
|
108
118
|
```
|
|
109
119
|
|
|
110
|
-
But,
|
|
120
|
+
But, the crusty boilerplate to get it working is annoying as heck.
|
|
111
121
|
|
|
112
|
-
**Enter the `createCouple` utility
|
|
122
|
+
**Enter the `createCouple` utility.**
|
|
113
123
|
|
|
114
124
|
It accepts a getter and a co-setter and returns a signal.
|
|
115
125
|
A co-setter is similar to a setter, except that it doesn't take a function nor does it return a value.
|
|
116
|
-
Basically, it's the previous example without the boilerplate.
|
|
126
|
+
Basically, it's the previous example without the boilerplate.
|
|
127
|
+
See it in action:
|
|
117
128
|
|
|
118
129
|
```ts
|
|
119
130
|
const [ count, setCount ] = createSignal(0);
|
|
@@ -140,8 +151,8 @@ console.log(setDouble(10), count()); // 10 5
|
|
|
140
151
|
Much better, right?
|
|
141
152
|
|
|
142
153
|
> [!NOTE]
|
|
143
|
-
> The getter passed to `createCouple` is always memoized.
|
|
144
|
-
> This
|
|
154
|
+
> The getter passed to `createCouple` is always [memoized](https://docs.solidjs.com/concepts/derived-values/memos).
|
|
155
|
+
> This immediately invokes the getter, so keep that in mind.
|
|
145
156
|
|
|
146
157
|
By the way! A `createCouple` call, like any expression that evaluates to a signal, can be converted into an atom so that the getter and setter are merged into one.
|
|
147
158
|
|
package/dist/atom.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ export type Atom<T extends SignalLike = SignalLike> = T[0] & T[1];
|
|
|
32
32
|
* count(count() + 1);
|
|
33
33
|
* ```
|
|
34
34
|
*/
|
|
35
|
-
export declare function atom<const T extends SignalLike>(
|
|
35
|
+
export declare function atom<const T extends SignalLike>(signal: T): Atom<T>;
|
|
36
36
|
/**
|
|
37
37
|
* Similar to a signal setter, except it doesn't accept a mapping function nor return a result.
|
|
38
38
|
*/
|
package/dist/atom.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"atom.d.ts","sourceRoot":"","sources":["../src/atom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqC,KAAK,QAAQ,EAAe,KAAK,MAAM,EAAE,KAAK,aAAa,EAA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,MAAM,CAAC,CAAC,CAAC,CAmBnF;AAID;;;GAGG;AACH,MAAM,MAAM,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAEtC;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;AAC/C,wBAAgB,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;AAKvE;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAE1E"}
|
package/dist/atom.js
CHANGED
|
@@ -1,20 +1,42 @@
|
|
|
1
1
|
import { createMemo, createSignal, untrack } from "solid-js";
|
|
2
2
|
import { isDev } from "solid-js/web";
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Combine a getter and setter function pair into one.
|
|
5
|
+
*
|
|
6
|
+
* Accepts a tuple of two functions.
|
|
7
|
+
* The first is the getter, the second is the setter.
|
|
8
|
+
* A new function is returned that, when called with zero arguments, calls the getter.
|
|
9
|
+
* Otherwise, it calls the setter and forwards all of the arguments.
|
|
10
|
+
*
|
|
11
|
+
* ### Example
|
|
12
|
+
* ```
|
|
13
|
+
* const count = atom(createSignal(0));
|
|
14
|
+
*
|
|
15
|
+
* // Read
|
|
16
|
+
* count();
|
|
17
|
+
*
|
|
18
|
+
* // Write
|
|
19
|
+
* count(100);
|
|
20
|
+
* count(x => x + 1);
|
|
21
|
+
* count(count() + 1);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function atom(signal) {
|
|
4
25
|
if (isDev) {
|
|
5
26
|
// Assert that the input is valid.
|
|
6
27
|
// For production, these checks are skipped for performance.
|
|
7
|
-
if (!Array.isArray(
|
|
8
|
-
throw new Error(`expected a getter setter pair as an array, but got ${typeof
|
|
28
|
+
if (!Array.isArray(signal)) {
|
|
29
|
+
throw new Error(`expected a getter setter pair as an array, but got ${typeof signal}`);
|
|
9
30
|
}
|
|
10
|
-
if (typeof
|
|
11
|
-
throw new Error(`expected a getter function, but got ${typeof
|
|
31
|
+
if (typeof signal[0] !== "function") {
|
|
32
|
+
throw new Error(`expected a getter function, but got ${typeof signal[0]}`);
|
|
12
33
|
}
|
|
13
|
-
if (typeof
|
|
14
|
-
throw new Error(`expected a setter function, but got ${typeof
|
|
34
|
+
if (typeof signal[1] !== "function") {
|
|
35
|
+
throw new Error(`expected a setter function, but got ${typeof signal[1]}`);
|
|
15
36
|
}
|
|
16
37
|
}
|
|
17
|
-
|
|
38
|
+
const [getter, setter] = signal;
|
|
39
|
+
return (...args) => (args.length === 0) ? getter() : setter(...args);
|
|
18
40
|
}
|
|
19
41
|
/**
|
|
20
42
|
* Create a signal from a cosignal.
|
package/dist/atom.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"atom.js","sourceRoot":"","sources":["../src/atom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,OAAO,EAA+D,MAAM,UAAU,CAAC;AAC1H,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"atom.js","sourceRoot":"","sources":["../src/atom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,OAAO,EAA+D,MAAM,UAAU,CAAC;AAC1H,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAkBrC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,IAAI,CAA6B,MAAS;IACzD,IAAI,KAAK,EAAE,CAAC;QACX,kCAAkC;QACl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