solid-state-tools 1.4.0 → 1.4.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/dist/index.d.ts +127 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +107 -0
- package/dist/index.js.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { type Accessor, type Signal, type SignalOptions } from "solid-js";
|
|
2
|
+
/**
|
|
3
|
+
* @description
|
|
4
|
+
* Any tuple of two functions where the first accepts no arguments and the second accepts any amount.
|
|
5
|
+
*
|
|
6
|
+
* Used as the source of an {@link Atom}.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link Atom}, {@link atom}
|
|
9
|
+
*/
|
|
10
|
+
export type SignalLike = readonly [() => any, (...args: any) => any];
|
|
11
|
+
/**
|
|
12
|
+
* @description
|
|
13
|
+
* An {@link Atom} is a polymorphic function that calls one of two
|
|
14
|
+
* functions depending on the number of arguments it has.
|
|
15
|
+
*
|
|
16
|
+
* If called with zero arguments, the first function is called.
|
|
17
|
+
* Otherwise, the second function is called with all arguments forwarded to it.
|
|
18
|
+
*
|
|
19
|
+
* @see {@link SignalLike}, {@link atom} (constructor)
|
|
20
|
+
*/
|
|
21
|
+
export type Atom<T extends SignalLike = SignalLike> = T[0] & T[1];
|
|
22
|
+
/**
|
|
23
|
+
* @description
|
|
24
|
+
* Combine a getter and setter function pair into one.
|
|
25
|
+
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#atoms-atom)
|
|
26
|
+
*
|
|
27
|
+
* Accepts a tuple of two functions.
|
|
28
|
+
* The first is the getter, the second is the setter.
|
|
29
|
+
* A new function is returned that, when called with zero arguments, calls the getter.
|
|
30
|
+
* Otherwise, it calls the setter and forwards all of the arguments.
|
|
31
|
+
*
|
|
32
|
+
* @see {@link SignalLike} (input), {@link Atom} (output)
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```
|
|
36
|
+
* const count: Atom<Signal<T>> = atom(createSignal(0));
|
|
37
|
+
*
|
|
38
|
+
* // Read
|
|
39
|
+
* count();
|
|
40
|
+
*
|
|
41
|
+
* // Write
|
|
42
|
+
* count(100);
|
|
43
|
+
* count(x => x + 1);
|
|
44
|
+
* count(count() + 1);
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function atom<const T extends SignalLike>(signal: T): Atom<T>;
|
|
48
|
+
/**
|
|
49
|
+
* @description
|
|
50
|
+
* Similar to a signal setter, except it doesn't accept a mapping function nor return a result.
|
|
51
|
+
*
|
|
52
|
+
* @see {@link createCouple} (for example usage)
|
|
53
|
+
*/
|
|
54
|
+
export type Writer<T> = (value: T) => void;
|
|
55
|
+
export interface CoupleOptions {
|
|
56
|
+
/**
|
|
57
|
+
* @default true
|
|
58
|
+
*/
|
|
59
|
+
memoized: boolean;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @description
|
|
63
|
+
* Create a signal from a getter setter pair.
|
|
64
|
+
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#couples-createcouple)
|
|
65
|
+
*
|
|
66
|
+
* **The getter is immediately invoked for memoization.**
|
|
67
|
+
*
|
|
68
|
+
* @see {@link Accessor} (input), {@link Writer} (input), {@link Signal} (output)
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```
|
|
72
|
+
* const [ count, setCount ] = createSignal(0);
|
|
73
|
+
* const [ double, setDouble ] = createCouple(() => count() * 2, (x) => setCount(x / 2));
|
|
74
|
+
*
|
|
75
|
+
* setDouble(x => x + 2);
|
|
76
|
+
* console.log(double(), count()); // 2 1
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function createCouple<T>(getter: Accessor<T>, setter: Writer<T>, options?: CoupleOptions): Signal<T>;
|
|
80
|
+
/**
|
|
81
|
+
* @description
|
|
82
|
+
* An atomic signal. A signal where the getter and setter are combined into one function.
|
|
83
|
+
*
|
|
84
|
+
* @see {@link Atom}, {@link asig} (constructor)
|
|
85
|
+
*/
|
|
86
|
+
export type Asig<T> = Atom<Signal<T>>;
|
|
87
|
+
/**
|
|
88
|
+
* @description
|
|
89
|
+
* Create an atomic signal. Short for `atom(createSignal(...))`.
|
|
90
|
+
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#atomic-signals-asig)
|
|
91
|
+
*
|
|
92
|
+
* @see {@link Asig} (output), {@link Atom}, {@link atom}
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```
|
|
96
|
+
* const count = asig(0);
|
|
97
|
+
*
|
|
98
|
+
* count(10);
|
|
99
|
+
* console.log(count()); // 10
|
|
100
|
+
*
|
|
101
|
+
* count(x => x + 10);
|
|
102
|
+
* console.log(count()); // 20
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export declare function asig<T>(value: T, options?: SignalOptions<T>): Asig<T>;
|
|
106
|
+
export declare function asig<T>(): Asig<T | undefined>;
|
|
107
|
+
/**
|
|
108
|
+
* @description
|
|
109
|
+
* Create an atomic getter setter pair. Short for `atom(createCouple(...))`.
|
|
110
|
+
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#atomic-couples-apair)
|
|
111
|
+
*
|
|
112
|
+
* @see {@link Accessor} (input), {@link Writer} (input), {@link Asig} (output)
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```
|
|
116
|
+
* const count = asig(0);
|
|
117
|
+
* const double = apair(() => count() * 2, (x) => count(x / 2));
|
|
118
|
+
*
|
|
119
|
+
* count(10);
|
|
120
|
+
* console.log(count(), double()); // 10 20
|
|
121
|
+
*
|
|
122
|
+
* double(100);
|
|
123
|
+
* console.log(count(), double()); // 50 100
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export declare function apair<T>(getter: Accessor<T>, setter: Writer<T>): Asig<T>;
|
|
127
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|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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,GAAG,IAAI,CAAC,CAAC,CAAC,CAExE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { createMemo, createSignal, untrack } from "solid-js";
|
|
2
|
+
import { isDev } from "solid-js/web";
|
|
3
|
+
/**
|
|
4
|
+
* @description
|
|
5
|
+
* Combine a getter and setter function pair into one.
|
|
6
|
+
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#atoms-atom)
|
|
7
|
+
*
|
|
8
|
+
* Accepts a tuple of two functions.
|
|
9
|
+
* The first is the getter, the second is the setter.
|
|
10
|
+
* A new function is returned that, when called with zero arguments, calls the getter.
|
|
11
|
+
* Otherwise, it calls the setter and forwards all of the arguments.
|
|
12
|
+
*
|
|
13
|
+
* @see {@link SignalLike} (input), {@link Atom} (output)
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```
|
|
17
|
+
* const count: Atom<Signal<T>> = atom(createSignal(0));
|
|
18
|
+
*
|
|
19
|
+
* // Read
|
|
20
|
+
* count();
|
|
21
|
+
*
|
|
22
|
+
* // Write
|
|
23
|
+
* count(100);
|
|
24
|
+
* count(x => x + 1);
|
|
25
|
+
* count(count() + 1);
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function atom(signal) {
|
|
29
|
+
if (isDev) {
|
|
30
|
+
// Assert that the input is valid.
|
|
31
|
+
// For production, these checks are skipped for performance.
|
|
32
|
+
if (!Array.isArray(signal)) {
|
|
33
|
+
throw new Error(`expected a getter setter pair as an array, but got ${typeof signal}`);
|
|
34
|
+
}
|
|
35
|
+
if (typeof signal[0] !== "function") {
|
|
36
|
+
throw new Error(`expected a getter function, but got ${typeof signal[0]}`);
|
|
37
|
+
}
|
|
38
|
+
if (typeof signal[1] !== "function") {
|
|
39
|
+
throw new Error(`expected a setter function, but got ${typeof signal[1]}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const [getter, setter] = signal;
|
|
43
|
+
return (...args) => (args.length === 0) ? getter() : setter(...args);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* @description
|
|
47
|
+
* Create a signal from a getter setter pair.
|
|
48
|
+
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#couples-createcouple)
|
|
49
|
+
*
|
|
50
|
+
* **The getter is immediately invoked for memoization.**
|
|
51
|
+
*
|
|
52
|
+
* @see {@link Accessor} (input), {@link Writer} (input), {@link Signal} (output)
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```
|
|
56
|
+
* const [ count, setCount ] = createSignal(0);
|
|
57
|
+
* const [ double, setDouble ] = createCouple(() => count() * 2, (x) => setCount(x / 2));
|
|
58
|
+
*
|
|
59
|
+
* setDouble(x => x + 2);
|
|
60
|
+
* console.log(double(), count()); // 2 1
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export function createCouple(getter, setter, options) {
|
|
64
|
+
if (isDev) {
|
|
65
|
+
// Assert that the input is valid.
|
|
66
|
+
// For production, these checks are skipped for performance.
|
|
67
|
+
if (typeof getter !== "function") {
|
|
68
|
+
throw new Error(`expected getter to be a function, but got ${typeof getter}`);
|
|
69
|
+
}
|
|
70
|
+
if (typeof setter !== "function") {
|
|
71
|
+
throw new Error(`expected setter to be a function, but got ${typeof setter}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const get = options?.memoized === false ? getter : createMemo(getter);
|
|
75
|
+
const set = ((source) => {
|
|
76
|
+
const value = (typeof source === "function") ? source(untrack(get)) : source;
|
|
77
|
+
setter(value);
|
|
78
|
+
return value;
|
|
79
|
+
});
|
|
80
|
+
return [get, set];
|
|
81
|
+
}
|
|
82
|
+
export function asig(value, options) {
|
|
83
|
+
return atom(createSignal(value, options));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* @description
|
|
87
|
+
* Create an atomic getter setter pair. Short for `atom(createCouple(...))`.
|
|
88
|
+
* [See documentation.](https://github.com/ReedSyllas/solid-state-tools#atomic-couples-apair)
|
|
89
|
+
*
|
|
90
|
+
* @see {@link Accessor} (input), {@link Writer} (input), {@link Asig} (output)
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```
|
|
94
|
+
* const count = asig(0);
|
|
95
|
+
* const double = apair(() => count() * 2, (x) => count(x / 2));
|
|
96
|
+
*
|
|
97
|
+
* count(10);
|
|
98
|
+
* console.log(count(), double()); // 10 20
|
|
99
|
+
*
|
|
100
|
+
* double(100);
|
|
101
|
+
* console.log(count(), double()); // 50 100
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export function apair(getter, setter) {
|
|
105
|
+
return atom(createCouple(getter, setter));
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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,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