solid-state-tools 1.0.2 → 1.0.4
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 +4 -4
- package/dist/atom.d.ts +60 -2
- package/dist/atom.d.ts.map +1 -1
- package/dist/atom.js +39 -5
- package/dist/atom.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -51,11 +51,11 @@ const [ double, setDouble ] = createCosignal([
|
|
|
51
51
|
]);
|
|
52
52
|
|
|
53
53
|
// Yet, we can still pass a mapping function here.
|
|
54
|
-
|
|
54
|
+
setDouble(x => x + 2);
|
|
55
55
|
console.log(double(), count()); // 12 6
|
|
56
56
|
|
|
57
57
|
// Or simply use it traditionally.
|
|
58
|
-
|
|
58
|
+
setDouble(10);
|
|
59
59
|
console.log(double(), count()); // 10 5
|
|
60
60
|
```
|
|
61
61
|
|
|
@@ -69,10 +69,10 @@ const [ double, setDouble ] = createCosignal([
|
|
|
69
69
|
]);
|
|
70
70
|
```
|
|
71
71
|
|
|
72
|
-
Finally, co-signals can be wrapped with `atom
|
|
72
|
+
Finally, co-signals can be wrapped with `atom` to combine their getter and setter.
|
|
73
73
|
|
|
74
74
|
```ts
|
|
75
75
|
const double = atom(createCosignal(/* ... */));
|
|
76
76
|
```
|
|
77
77
|
|
|
78
|
-
To be clear, `createCosignal` and `createSignal` return the same kind of value and therefore should work in every situation native signals do.
|
|
78
|
+
To be clear, `createCosignal` and `createSignal` return the same kind of value (`Signal`) and therefore should work in every situation native signals do.
|
package/dist/atom.d.ts
CHANGED
|
@@ -1,19 +1,77 @@
|
|
|
1
1
|
import { type Signal, type SignalOptions } from "solid-js";
|
|
2
|
+
/**
|
|
3
|
+
* Any tuple of two functions where the first accepts no arguments and the second accepts any amount.
|
|
4
|
+
*/
|
|
2
5
|
export type Atomical = readonly [() => any, (...args: any) => any];
|
|
6
|
+
/**
|
|
7
|
+
* An atom is a polymorphic function that calls one of two
|
|
8
|
+
* functions depending on the number of arguments it has.
|
|
9
|
+
*
|
|
10
|
+
* If called with zero arguments, the first function is called.
|
|
11
|
+
* Otherwise, the second function is called and all of the arguments are forwarded to it.
|
|
12
|
+
*/
|
|
3
13
|
export type Atom<T extends Atomical = Atomical> = T[0] & T[1];
|
|
14
|
+
/**
|
|
15
|
+
* Combine a getter and setter function pair into one.
|
|
16
|
+
*
|
|
17
|
+
* Accepts a tuple of two functions.
|
|
18
|
+
* The first is the getter, the second is the setter.
|
|
19
|
+
* A new function is returned that, when called with zero arguments, calls the getter.
|
|
20
|
+
* Otherwise, it calls the setter and forwards all of the arguments.
|
|
21
|
+
*
|
|
22
|
+
* ### Example
|
|
23
|
+
* ```
|
|
24
|
+
* const count = atom(createSignal(0));
|
|
25
|
+
*
|
|
26
|
+
* // Read
|
|
27
|
+
* count();
|
|
28
|
+
*
|
|
29
|
+
* // Write
|
|
30
|
+
* count(100);
|
|
31
|
+
* count(x => x + 1);
|
|
32
|
+
* count(count() + 1);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
4
35
|
export declare function atom<const T extends Atomical>(value: T): Atom<T>;
|
|
36
|
+
/**
|
|
37
|
+
* An atomic signal.
|
|
38
|
+
* A signal where the getter and setter are combined into one function.
|
|
39
|
+
*/
|
|
5
40
|
export type Asig<T> = Atom<Signal<T>>;
|
|
6
41
|
/**
|
|
7
|
-
*
|
|
42
|
+
* Create an atomic signal.
|
|
43
|
+
* Shorthand for `atom(createSignal(...))`.
|
|
44
|
+
*
|
|
45
|
+
* ### Example
|
|
46
|
+
* ```
|
|
47
|
+
* const count = asig(0);
|
|
48
|
+
* ```
|
|
8
49
|
*/
|
|
9
50
|
export declare function asig<T>(): Atom<Signal<T | undefined>>;
|
|
10
51
|
export declare function asig<T>(value: T, options?: SignalOptions<T>): Atom<Signal<T>>;
|
|
52
|
+
/**
|
|
53
|
+
* A getter setter pair.
|
|
54
|
+
* While similar to `Signal`, the setter of
|
|
55
|
+
* `Cosignal` does not accept a mapping function.
|
|
56
|
+
*/
|
|
11
57
|
export type Cosignal<T> = [
|
|
12
58
|
() => T,
|
|
13
59
|
(value: T) => T
|
|
14
60
|
];
|
|
15
61
|
/**
|
|
16
|
-
*
|
|
62
|
+
* Create signal from a getter/setter tuple.
|
|
63
|
+
*
|
|
64
|
+
* ### Example
|
|
65
|
+
* ```
|
|
66
|
+
* const [ count, setCount ] = createSignal(0);
|
|
67
|
+
* const [ double, setDouble ] = createCosignal([
|
|
68
|
+
* () => count() * 2,
|
|
69
|
+
* (x) => void setCount(x / 2),
|
|
70
|
+
* ]);
|
|
71
|
+
*
|
|
72
|
+
* double(x => x + 2);
|
|
73
|
+
* console.log(double(), count()); // 12 6
|
|
74
|
+
* ```
|
|
17
75
|
*/
|
|
18
76
|
export declare function createCosignal<T>(cosignal: Cosignal<T>): Signal<T>;
|
|
19
77
|
//# sourceMappingURL=atom.d.ts.map
|
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,EAAyB,KAAK,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;
|
|
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;AAGlF;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,SAAS,CAAE,MAAM,GAAG,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,KAAK,GAAG,CAAE,CAAC;AAErE;;;;;;GAMG;AACH,MAAM,MAAM,IAAI,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,IAAI,CAAC,KAAK,CAAC,CAAC,SAAS,QAAQ,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;AAmBlE;;;GAGG;AACH,MAAM,MAAM,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAEtC;;;;;;;;GAQG;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;;;;GAIG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI;IACzB,MAAM,CAAC;IACP,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC;CACf,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAoBlE"}
|
package/dist/atom.js
CHANGED
|
@@ -1,19 +1,53 @@
|
|
|
1
1
|
import { createSignal, untrack } from "solid-js";
|
|
2
|
+
import { isDev } from "solid-js/web";
|
|
2
3
|
export function atom(value) {
|
|
3
|
-
if (
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
if (isDev) {
|
|
5
|
+
// Assert that the input is valid.
|
|
6
|
+
// For production, the checks are skipped for performance.
|
|
7
|
+
if (!Array.isArray(value)) {
|
|
8
|
+
throw new Error(`expected a getter setter pair as an array, but got ${typeof value}`);
|
|
9
|
+
}
|
|
10
|
+
if (typeof value[0] !== "function") {
|
|
11
|
+
throw new Error(`expected a getter function, but got ${typeof value[0]}`);
|
|
12
|
+
}
|
|
13
|
+
if (typeof value[1] !== "function") {
|
|
14
|
+
throw new Error(`expected a setter function, but got ${typeof value[1]}`);
|
|
6
15
|
}
|
|
7
16
|
}
|
|
8
|
-
|
|
17
|
+
return (...args) => (args.length === 0) ? value[0]() : value[1](...args);
|
|
9
18
|
}
|
|
10
19
|
export function asig(...args) {
|
|
11
20
|
return atom(args.length === 0 ? createSignal() : createSignal(args[0], args[1]));
|
|
12
21
|
}
|
|
13
22
|
/**
|
|
14
|
-
*
|
|
23
|
+
* Create signal from a getter/setter tuple.
|
|
24
|
+
*
|
|
25
|
+
* ### Example
|
|
26
|
+
* ```
|
|
27
|
+
* const [ count, setCount ] = createSignal(0);
|
|
28
|
+
* const [ double, setDouble ] = createCosignal([
|
|
29
|
+
* () => count() * 2,
|
|
30
|
+
* (x) => void setCount(x / 2),
|
|
31
|
+
* ]);
|
|
32
|
+
*
|
|
33
|
+
* double(x => x + 2);
|
|
34
|
+
* console.log(double(), count()); // 12 6
|
|
35
|
+
* ```
|
|
15
36
|
*/
|
|
16
37
|
export function createCosignal(cosignal) {
|
|
38
|
+
if (isDev) {
|
|
39
|
+
// Assert that the input is valid.
|
|
40
|
+
// For production, the checks are skipped for performance.
|
|
41
|
+
if (!Array.isArray(cosignal)) {
|
|
42
|
+
throw new Error(`expected a getter setter pair as an array, but got ${typeof cosignal}`);
|
|
43
|
+
}
|
|
44
|
+
if (typeof cosignal[0] !== "function") {
|
|
45
|
+
throw new Error(`expected a getter function, but got ${typeof cosignal[0]}`);
|
|
46
|
+
}
|
|
47
|
+
if (typeof cosignal[1] !== "function") {
|
|
48
|
+
throw new Error(`expected a setter function, but got ${typeof cosignal[1]}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
17
51
|
const [get, set] = cosignal;
|
|
18
52
|
return [
|
|
19
53
|
get,
|
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,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