@cripty2001/utils 0.0.1 → 0.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/package.json +15 -1
- package/src/react-whispr.ts +69 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cripty2001/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Internal Set of utils. If you need them use them, otherwise go to the next package ;)",
|
|
5
5
|
"homepage": "https://github.com/cripty2001/utils#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
"build": "tsc"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
+
"@types/react": "^19.2.0",
|
|
23
|
+
"react": "^18.0.0",
|
|
22
24
|
"typescript": "^5.9.3"
|
|
23
25
|
},
|
|
24
26
|
"dependencies": {
|
|
@@ -26,6 +28,14 @@
|
|
|
26
28
|
"@types/lodash": "^4.17.20",
|
|
27
29
|
"lodash": "^4.17.21"
|
|
28
30
|
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": "^18.0.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependenciesMeta": {
|
|
35
|
+
"react": {
|
|
36
|
+
"optional": true
|
|
37
|
+
}
|
|
38
|
+
},
|
|
29
39
|
"exports": {
|
|
30
40
|
".": {
|
|
31
41
|
"import": "./dist/index.js",
|
|
@@ -34,6 +44,10 @@
|
|
|
34
44
|
"./dispatcher": {
|
|
35
45
|
"import": "./dist/Dispatcher.js",
|
|
36
46
|
"require": "./dist/Dispatcher.js"
|
|
47
|
+
},
|
|
48
|
+
"./react-whispr": {
|
|
49
|
+
"import": "./dist/react-whispr.js",
|
|
50
|
+
"require": "./dist/react-whispr.js"
|
|
37
51
|
}
|
|
38
52
|
}
|
|
39
53
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Whispr, type WhisprSetter } from "@cripty2001/whispr";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
|
+
|
|
4
|
+
import { isEqual } from "lodash";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Convert a Whispr value into a reactive react value, usable in function components with the standard react reactive system.
|
|
8
|
+
* If the value is not a Whispr, it is returned as is, thus allowing for progressive migration and adoption
|
|
9
|
+
* @param w A reactive react value or a Whispr containing it
|
|
10
|
+
* @param computer An optional function to compute the returned value. This is useful to extract a part of a larger whispr, or to compute a derived value. It is the analog of useMemo with a single dependency (the whispr itself)
|
|
11
|
+
* @returns A reactive react value
|
|
12
|
+
*
|
|
13
|
+
* @remarks The value is NOT REACTIVE, and the same applies to the computer, if any. Only changes to its content will trigger a change, not changes to the object itself.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* export default function MyComponent(props: { value: Whispr<string> }) {
|
|
17
|
+
* const value = useWhisprValue(props.value);
|
|
18
|
+
*
|
|
19
|
+
* return <div>{value}</div>;
|
|
20
|
+
* }
|
|
21
|
+
*/
|
|
22
|
+
export function useWhisprValue<I, O = I>(
|
|
23
|
+
w: Whispr<I>,
|
|
24
|
+
computer: (data: I) => O = (d) => d as unknown as O
|
|
25
|
+
): O {
|
|
26
|
+
const value_w = useRef(
|
|
27
|
+
Whispr.from(
|
|
28
|
+
{ w },
|
|
29
|
+
({ w }) => computer(w)
|
|
30
|
+
)
|
|
31
|
+
).current;
|
|
32
|
+
|
|
33
|
+
const [value, setValue] = useState(value_w.value);
|
|
34
|
+
|
|
35
|
+
value_w.subscribe((newValue) => {
|
|
36
|
+
if (isEqual(newValue, value))
|
|
37
|
+
return;
|
|
38
|
+
|
|
39
|
+
setValue(newValue);
|
|
40
|
+
}, false); // Already got the initial value, and this will call syncronously generate a react warning as called on an not yet mounted component
|
|
41
|
+
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Wrap a (react) value into a Whispr, if it is not one already.
|
|
47
|
+
* @param data A Whispr or a normal (react reactable) value
|
|
48
|
+
* @returns The whispr'd value, or the original whispr if it was one (allow for incremental adoption)
|
|
49
|
+
*
|
|
50
|
+
* @remarks The returned whispr has already been ref-fed, so it can be directly used without worrying about react recreating it or similar bad things
|
|
51
|
+
*/
|
|
52
|
+
export function useWhispr<T>(data: T | Whispr<T>): Whispr<T> {
|
|
53
|
+
const [w, setW] = useRef(Whispr.create(
|
|
54
|
+
data instanceof Whispr ?
|
|
55
|
+
data.value :
|
|
56
|
+
data
|
|
57
|
+
)).current;
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
setW(data instanceof Whispr ? data.value : data);
|
|
61
|
+
}, [data, setW]);
|
|
62
|
+
|
|
63
|
+
// Hooks can't be called conditionally, so we need to do this check at the end
|
|
64
|
+
if (data instanceof Whispr)
|
|
65
|
+
return data;
|
|
66
|
+
|
|
67
|
+
return w;
|
|
68
|
+
}
|
|
69
|
+
|