@cripty2001/utils 0.0.185 → 0.0.186
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/react-whispr.d.ts +0 -2
- package/dist/react-whispr.js +11 -7
- package/package.json +1 -1
package/dist/react-whispr.d.ts
CHANGED
|
@@ -9,8 +9,6 @@ import { SearcherData } from "./Searcher";
|
|
|
9
9
|
* @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)
|
|
10
10
|
* @returns A reactive react value
|
|
11
11
|
*
|
|
12
|
-
* @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.
|
|
13
|
-
*
|
|
14
12
|
* @example
|
|
15
13
|
* export default function MyComponent(props: { value: Whispr<string> }) {
|
|
16
14
|
* const value = useWhisprValue(props.value);
|
package/dist/react-whispr.js
CHANGED
|
@@ -25,8 +25,6 @@ const Searcher_1 = require("./Searcher");
|
|
|
25
25
|
* @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)
|
|
26
26
|
* @returns A reactive react value
|
|
27
27
|
*
|
|
28
|
-
* @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.
|
|
29
|
-
*
|
|
30
28
|
* @example
|
|
31
29
|
* export default function MyComponent(props: { value: Whispr<string> }) {
|
|
32
30
|
* const value = useWhisprValue(props.value);
|
|
@@ -35,15 +33,21 @@ const Searcher_1 = require("./Searcher");
|
|
|
35
33
|
* }
|
|
36
34
|
*/
|
|
37
35
|
function useWhisprValue(w, computer = (d) => d) {
|
|
38
|
-
const value_w = (0, react_1.useRef)(whispr_1.Whispr.
|
|
39
|
-
const
|
|
36
|
+
const value_w = (0, react_1.useRef)(whispr_1.Whispr.create(computer(w.value))[0]);
|
|
37
|
+
const unsubscribe = (0, react_1.useRef)(() => { });
|
|
38
|
+
const [value, setValue] = (0, react_1.useState)(value_w.current.value);
|
|
39
|
+
const valueref = (0, react_1.useRef)(value); // Yep, react and his strange stale closures...
|
|
40
40
|
(0, react_1.useEffect)(() => {
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
unsubscribe.current();
|
|
42
|
+
value_w.current = whispr_1.Whispr.from({ w }, ({ w }) => computer(w));
|
|
43
|
+
unsubscribe.current = value_w.current.subscribe((newValue) => {
|
|
44
|
+
if ((0, lodash_1.isEqual)(newValue, valueref.current))
|
|
43
45
|
return;
|
|
44
46
|
setValue(newValue);
|
|
47
|
+
valueref.current = newValue;
|
|
45
48
|
});
|
|
46
|
-
|
|
49
|
+
return () => unsubscribe.current();
|
|
50
|
+
}, [w, computer]);
|
|
47
51
|
return value;
|
|
48
52
|
}
|
|
49
53
|
/**
|
package/package.json
CHANGED