solid-state-tools 1.4.1 → 1.5.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 +16 -19
- package/dist/index.d.ts +10 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -9
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,15 +10,12 @@ The package is small and only has a peer dependency of Solid JS.
|
|
|
10
10
|
|
|
11
11
|
# Usage
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
1. [asig](#atomic-signals-asig)
|
|
21
|
-
2. [apair](#atomic-couples-apair)
|
|
13
|
+
| Utility | Summary |
|
|
14
|
+
| ------------------------------- | ------------------------------------------------ |
|
|
15
|
+
| [atom](#atoms-atom) | Combines a getter setter pair into one function. |
|
|
16
|
+
| [createPair](#pairs-createpair) | Creates a signal from a getter setter pair. |
|
|
17
|
+
| [asig](#atomic-signals-asig) | Shorthand for `atom(createSignal(...))` |
|
|
18
|
+
| [apair](#atomic-pairs-apair) | Shorthand for `atom(createCouple(...))` |
|
|
22
19
|
|
|
23
20
|
Read the sections below for a breakdown of each utility.
|
|
24
21
|
|
|
@@ -69,7 +66,7 @@ const list = asig([], { equals: false });
|
|
|
69
66
|
const list = atom(createSignal([], { equals: false }));
|
|
70
67
|
```
|
|
71
68
|
|
|
72
|
-
##
|
|
69
|
+
## Pairs (`createPair`)
|
|
73
70
|
|
|
74
71
|
A signal can be summarized as a getter setter pair.
|
|
75
72
|
However, the setter of a Solid JS signal is more complex than it appears at first glance.
|
|
@@ -122,7 +119,7 @@ console.log(setDouble(20)); // 20
|
|
|
122
119
|
|
|
123
120
|
But, the crusty boilerplate to get it working is annoying as heck.
|
|
124
121
|
|
|
125
|
-
**Enter the `
|
|
122
|
+
**Enter the `createPair` utility.**
|
|
126
123
|
|
|
127
124
|
It accepts a getter and a writer and returns a signal.
|
|
128
125
|
A writer is similar to a setter, except that it doesn't accept a function as input nor does it return a value.
|
|
@@ -132,7 +129,7 @@ See it in action:
|
|
|
132
129
|
```ts
|
|
133
130
|
const [ count, setCount ] = createSignal(0);
|
|
134
131
|
|
|
135
|
-
const [ double, setDouble ] =
|
|
132
|
+
const [ double, setDouble ] = createPair(
|
|
136
133
|
// The provided getter is automatically memoized.
|
|
137
134
|
() => count() * 2,
|
|
138
135
|
|
|
@@ -155,32 +152,32 @@ console.log(setDouble(10), count()); // 10 5
|
|
|
155
152
|
It can be succinctly rewritten into this:
|
|
156
153
|
|
|
157
154
|
```ts
|
|
158
|
-
const [ double, setDouble ] =
|
|
155
|
+
const [ double, setDouble ] = createPair(() => count() * 2, (x) => setCount(x / 2));
|
|
159
156
|
```
|
|
160
157
|
|
|
161
158
|
Much better, right?
|
|
162
159
|
|
|
163
160
|
> [!NOTE]
|
|
164
|
-
> The getter passed to `
|
|
161
|
+
> The getter passed to `createPair` is [memoized](https://docs.solidjs.com/concepts/derived-values/memos) unless otherwise set in the `options`.
|
|
165
162
|
> Memoization immediately invokes the getter, so keep that in mind because it can cause undesirable side-effects.
|
|
166
163
|
|
|
167
|
-
By the way! The `
|
|
164
|
+
By the way! The `createPair` output, like any signal, can be converted into an atom so that the getter and setter are merged together. See [atom](#atoms-atom) for details.
|
|
168
165
|
|
|
169
166
|
```ts
|
|
170
|
-
const double = atom(
|
|
167
|
+
const double = atom(createPair(() => count() * 2, (x) => setCount(x / 2)));
|
|
171
168
|
|
|
172
169
|
double() // read
|
|
173
170
|
double(10) // write
|
|
174
171
|
```
|
|
175
172
|
|
|
176
|
-
## Atomic
|
|
173
|
+
## Atomic pairs (`apair`)
|
|
177
174
|
|
|
178
|
-
Similar to [atomic signals](#atomic-signals-asig), wrapping a `
|
|
175
|
+
Similar to [atomic signals](#atomic-signals-asig), wrapping a `createPair` call with `atom` is a common enough pattern to warrant a shorthand: `apair`.
|
|
179
176
|
|
|
180
177
|
```ts
|
|
181
178
|
const double = apair(() => count() * 2, (double) => count(double / 2));
|
|
182
179
|
// is short for
|
|
183
|
-
const double = atom(
|
|
180
|
+
const double = atom(createPair(() => count() * 2, (double) => count(double / 2)));
|
|
184
181
|
```
|
|
185
182
|
|
|
186
183
|
# Conclusion
|
package/dist/index.d.ts
CHANGED
|
@@ -49,10 +49,10 @@ export declare function atom<const T extends SignalLike>(signal: T): Atom<T>;
|
|
|
49
49
|
* @description
|
|
50
50
|
* Similar to a signal setter, except it doesn't accept a mapping function nor return a result.
|
|
51
51
|
*
|
|
52
|
-
* @see {@link
|
|
52
|
+
* @see {@link createPair} (for example usage)
|
|
53
53
|
*/
|
|
54
54
|
export type Writer<T> = (value: T) => void;
|
|
55
|
-
export interface
|
|
55
|
+
export interface PairOptions {
|
|
56
56
|
/**
|
|
57
57
|
* @default true
|
|
58
58
|
*/
|
|
@@ -61,22 +61,22 @@ export interface CoupleOptions {
|
|
|
61
61
|
/**
|
|
62
62
|
* @description
|
|
63
63
|
* Create a signal from a getter setter pair.
|
|
64
|
-
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#
|
|
64
|
+
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#pairs-createpair)
|
|
65
65
|
*
|
|
66
66
|
* **The getter is immediately invoked for memoization.**
|
|
67
67
|
*
|
|
68
|
-
* @see {@link Accessor} (input), {@link Writer} (input), {@link Signal} (output)
|
|
68
|
+
* @see {@link Accessor} (input), {@link Writer} (input), {@link PairOptions} (input), {@link Signal} (output)
|
|
69
69
|
*
|
|
70
70
|
* @example
|
|
71
71
|
* ```
|
|
72
72
|
* const [ count, setCount ] = createSignal(0);
|
|
73
|
-
* const [ double, setDouble ] =
|
|
73
|
+
* const [ double, setDouble ] = createPair(() => count() * 2, (x) => setCount(x / 2));
|
|
74
74
|
*
|
|
75
75
|
* setDouble(x => x + 2);
|
|
76
76
|
* console.log(double(), count()); // 2 1
|
|
77
77
|
* ```
|
|
78
78
|
*/
|
|
79
|
-
export declare function
|
|
79
|
+
export declare function createPair<T>(getter: Accessor<T>, setter: Writer<T>, options?: PairOptions): Signal<T>;
|
|
80
80
|
/**
|
|
81
81
|
* @description
|
|
82
82
|
* An atomic signal. A signal where the getter and setter are combined into one function.
|
|
@@ -106,10 +106,10 @@ export declare function asig<T>(value: T, options?: SignalOptions<T>): Asig<T>;
|
|
|
106
106
|
export declare function asig<T>(): Asig<T | undefined>;
|
|
107
107
|
/**
|
|
108
108
|
* @description
|
|
109
|
-
* Create an atomic getter setter pair. Short for `atom(
|
|
110
|
-
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#atomic-
|
|
109
|
+
* Create an atomic getter setter pair. Short for `atom(createPair(...))`.
|
|
110
|
+
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#atomic-pairs-apair)
|
|
111
111
|
*
|
|
112
|
-
* @see {@link Accessor} (input), {@link Writer} (input), {@link Asig} (output)
|
|
112
|
+
* @see {@link Accessor} (input), {@link Writer} (input), {@link PairOptions} (input), {@link Asig} (output)
|
|
113
113
|
*
|
|
114
114
|
* @example
|
|
115
115
|
* ```
|
|
@@ -123,5 +123,5 @@ export declare function asig<T>(): Asig<T | undefined>;
|
|
|
123
123
|
* console.log(count(), double()); // 50 100
|
|
124
124
|
* ```
|
|
125
125
|
*/
|
|
126
|
-
export declare function apair<T>(getter: Accessor<T>, setter: Writer<T
|
|
126
|
+
export declare function apair<T>(getter: Accessor<T>, setter: Writer<T>, options?: PairOptions): Asig<T>;
|
|
127
127
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqC,KAAK,QAAQ,EAAe,KAAK,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AAK1H;;;;;;;GAOG;AACH,MAAM,MAAM,UAAU,GAAG,S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AW;IAC3B;;OAEG;IACH,QAAQ,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,MAAM,CAAC,CAAC,CAAC,CAmBtG;AAID;;;;;GAKG;AACH,MAAM,MAAM,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAEtC;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;AACvE,wBAAgB,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;AAK/C;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC,CAAC,CAAC,CAE/F"}
|
package/dist/index.js
CHANGED
|
@@ -45,22 +45,22 @@ export function atom(signal) {
|
|
|
45
45
|
/**
|
|
46
46
|
* @description
|
|
47
47
|
* Create a signal from a getter setter pair.
|
|
48
|
-
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#
|
|
48
|
+
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#pairs-createpair)
|
|
49
49
|
*
|
|
50
50
|
* **The getter is immediately invoked for memoization.**
|
|
51
51
|
*
|
|
52
|
-
* @see {@link Accessor} (input), {@link Writer} (input), {@link Signal} (output)
|
|
52
|
+
* @see {@link Accessor} (input), {@link Writer} (input), {@link PairOptions} (input), {@link Signal} (output)
|
|
53
53
|
*
|
|
54
54
|
* @example
|
|
55
55
|
* ```
|
|
56
56
|
* const [ count, setCount ] = createSignal(0);
|
|
57
|
-
* const [ double, setDouble ] =
|
|
57
|
+
* const [ double, setDouble ] = createPair(() => count() * 2, (x) => setCount(x / 2));
|
|
58
58
|
*
|
|
59
59
|
* setDouble(x => x + 2);
|
|
60
60
|
* console.log(double(), count()); // 2 1
|
|
61
61
|
* ```
|
|
62
62
|
*/
|
|
63
|
-
export function
|
|
63
|
+
export function createPair(getter, setter, options) {
|
|
64
64
|
if (isDev) {
|
|
65
65
|
// Assert that the input is valid.
|
|
66
66
|
// For production, these checks are skipped for performance.
|
|
@@ -84,10 +84,10 @@ export function asig(value, options) {
|
|
|
84
84
|
}
|
|
85
85
|
/**
|
|
86
86
|
* @description
|
|
87
|
-
* Create an atomic getter setter pair. Short for `atom(
|
|
88
|
-
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#atomic-
|
|
87
|
+
* Create an atomic getter setter pair. Short for `atom(createPair(...))`.
|
|
88
|
+
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#atomic-pairs-apair)
|
|
89
89
|
*
|
|
90
|
-
* @see {@link Accessor} (input), {@link Writer} (input), {@link Asig} (output)
|
|
90
|
+
* @see {@link Accessor} (input), {@link Writer} (input), {@link PairOptions} (input), {@link Asig} (output)
|
|
91
91
|
*
|
|
92
92
|
* @example
|
|
93
93
|
* ```
|
|
@@ -101,7 +101,7 @@ export function asig(value, options) {
|
|
|
101
101
|
* console.log(count(), double()); // 50 100
|
|
102
102
|
* ```
|
|
103
103
|
*/
|
|
104
|
-
export function apair(getter, setter) {
|
|
105
|
-
return atom(
|
|
104
|
+
export function apair(getter, setter, options) {
|
|
105
|
+
return atom(createPair(getter, setter, options));
|
|
106
106
|
}
|
|
107
107
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,OAAO,EAA+D,MAAM,UAAU,CAAC;AAC1H,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AA0BrC;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,IAAI,CAA6B,MAAS;IACzD,IAAI,KAAK,EAAE,CAAC;QACX,kCAAkC;QAClC,4DAA4D;QAE5D,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,sDAAsD,OAAO,MAAM,EAAE,CAAC,CAAC;QACxF,CAAC;QACD,IAAI,OAAO,MAAM,CAAC,CAAC,CAAC,KAAK,UAAU,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CAAC,uCAAuC,OAAO,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAC5E,CAAC;QACD,IAAI,OAAO,MAAM,CAAC,CAAC,CAAC,KAAK,UAAU,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CAAC,uCAAuC,OAAO,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAC5E,CAAC;IACF,CAAC;IACD,MAAM,CAAE,MAAM,EAAE,MAAM,CAAE,GAAG,MAAM,CAAC;IAClC,OAAO,CAAC,GAAG,IAAe,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC;AACjF,CAAC;AAiBD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,OAAO,EAA+D,MAAM,U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