solid-state-tools 1.0.1 → 1.0.2

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 ADDED
@@ -0,0 +1,78 @@
1
+ # Solid JS State Tools
2
+
3
+ This package is a collection of utilities for managing [Solid JS](https://docs.solidjs.com/) state.
4
+
5
+ ## Atoms (`atom`)
6
+
7
+ Atoms combine the getter and setter of a [signal](https://docs.solidjs.com/concepts/signals) into one function.
8
+
9
+ ```ts
10
+ const count = atom(createSignal(0));
11
+
12
+ console.log(count()); // 0
13
+
14
+ count(100);
15
+ console.log(count()); // 100
16
+
17
+ count(count() + 1);
18
+ console.log(count()); // 101
19
+
20
+ count((c) => c + 1);
21
+ console.log(count()); // 102
22
+ ```
23
+
24
+ When called with no arguments, the atom acts like the signal's getter. If an argument is passed, it is forwarded to the signal's setter. Note that `undefined` _is_ considered an argument and is forwarded to the setter, as desired.
25
+
26
+ Atoms simplify the boilerplate that comes with managing getters and setters separately. However, signals are still preferred for granular control of the getter and setter. Additionally, atoms incur a tiny performance cost. Thus, atoms do not replace signals. They coexist.
27
+
28
+ ## Atomic signals (`asig`)
29
+
30
+ Because creating and wrapping a signal with `atom` is so common, a shorthand utility was created called `asig` (atomic signal).
31
+
32
+ ```ts
33
+ const count = asig(0);
34
+ // is a shorthand for
35
+ const count = atom(createSignal(0));
36
+ ```
37
+
38
+ ## Co-signals (`createCosignal`)
39
+
40
+ A signal can be summarized as a getter setter pair. However, Solid JS's setters are more complex than just a function that accepts a new value for the signal. They can also accept a function which acts like a map predicate from the old value to the new one.
41
+
42
+ All this is to say that creating signals manually can be tedious since the setter has to handle this edge case. Co-signals make defining the getter setter pair easy.
43
+
44
+ ```ts
45
+ const [ count, setCount ] = createSignal(0);
46
+ const [ double, setDouble ] = createCosignal([
47
+ () => count() * 2,
48
+
49
+ // Notice how we don't need to handle `x` being a function here.
50
+ (x) => void setCount(x / 2),
51
+ ]);
52
+
53
+ // Yet, we can still pass a mapping function here.
54
+ double(x => x + 2);
55
+ console.log(double(), count()); // 12 6
56
+
57
+ // Or simply use it traditionally.
58
+ double(10);
59
+ console.log(double(), count()); // 10 5
60
+ ```
61
+
62
+ It's important to note that the getter is not automatically memoized and should be wrapped with `createMemo`, when appropriate.
63
+
64
+ ```ts
65
+ const [ double, setDouble ] = createCosignal([
66
+ // Memoize to reduce extra computation.
67
+ createMemo(() => count() * 2),
68
+ /* ... */
69
+ ]);
70
+ ```
71
+
72
+ Finally, co-signals can be wrapped with `atom`, of course.
73
+
74
+ ```ts
75
+ const double = atom(createCosignal(/* ... */));
76
+ ```
77
+
78
+ To be clear, `createCosignal` and `createSignal` return the same kind of value and therefore should work in every situation native signals do.
package/dist/atom.d.ts CHANGED
@@ -8,12 +8,12 @@ export type Asig<T> = Atom<Signal<T>>;
8
8
  */
9
9
  export declare function asig<T>(): Atom<Signal<T | undefined>>;
10
10
  export declare function asig<T>(value: T, options?: SignalOptions<T>): Atom<Signal<T>>;
11
- export type Cosig<T> = [
11
+ export type Cosignal<T> = [
12
12
  () => T,
13
13
  (value: T) => T
14
14
  ];
15
15
  /**
16
16
  * Creates a signal from a getter/setter pair.
17
17
  */
18
- export declare function cosig<T>(cosig: Cosig<T>): Signal<T>;
18
+ export declare function createCosignal<T>(cosignal: Cosignal<T>): Signal<T>;
19
19
  //# sourceMappingURL=atom.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"atom.d.ts","sourceRoot":"","sources":["../src/atom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,KAAK,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AAElF,MAAM,MAAM,QAAQ,GAAG,SAAS,CAAE,MAAM,GAAG,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,KAAK,GAAG,CAAE,CAAC;AAErE,MAAM,MAAM,IAAI,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAA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,CAAC,CAAC;AAUlE,MAAM,MAAM,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAEtC;;GAEG;AACH,wBAAgB,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC;AACvD,wBAAgB,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAK/E,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI;IACzB,MAAM,CAAC;IACP,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC;CACf,CAAC;AAEF;;GAEG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAMlE"}
package/dist/atom.js CHANGED
@@ -13,8 +13,8 @@ export function asig(...args) {
13
13
  /**
14
14
  * Creates a signal from a getter/setter pair.
15
15
  */
16
- export function cosig(cosig) {
17
- const [get, set] = cosig;
16
+ export function createCosignal(cosignal) {
17
+ const [get, set] = cosignal;
18
18
  return [
19
19
  get,
20
20
  (value) => set((typeof value === "function") ? value(untrack(get)) : value),
package/dist/atom.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"atom.js","sourceRoot":"","sources":["../src/atom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,OAAO,EAAmC,MAAM,UAAU,CAAC;AAOlF,MAAM,UAAU,IAAI,CAAC,KAAc;IAClC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,IAAI,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,UAAU,IAAI,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,UAAU,EAAE,CAAC;YACtE,O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EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;QAC/E,CAAC;IACF,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;AAC1D,CAAC;AASD,MAAM,UAAU,IAAI,CAAI,GAAG,IAA+C;IACzE,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,YAAY,EAAK,CAAC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAM,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1F,CAAC;AAOD;;GAEG;AACH,MAAM,UAAU,cAAc,CAAI,QAAqB;IACtD,MAAM,CAAE,GAAG,EAAE,GAAG,CAAE,GAAG,QAAQ,CAAC;IAC9B,OAAO;QACN,GAAG;QACH,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,CAAE,KAAa,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;KACvE,CAAC;AAChB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-state-tools",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "A collection of Solid JS state utilities",
5
5
  "author": "Reed Syllas <reedsyllas@gmail.com>",
6
6
  "license": "ISC",