@cripty2001/utils 0.0.185 → 0.0.187

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.
@@ -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);
@@ -33,7 +31,7 @@ export declare function useWhispr<T>(data: T | Whispr<T>): Whispr<T>;
33
31
  * @param cb The callback to call on value change
34
32
  * @param unsafe If true, the callback will be allowed to throw errors, that will then bubble up
35
33
  */
36
- export declare function useOnWhispr<T>(w: Whispr<T>, unsafe: boolean | undefined, cb: (value: T) => void): void;
34
+ export declare function useOnWhispr<T>(w: Whispr<T>, cb: (value: T) => void, unsafe?: boolean): void;
37
35
  /**
38
36
  * Return a reactive current timestamp (ms)
39
37
  * @returns The current timestamp
@@ -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.from({ w }, ({ w }) => computer(w))).current;
39
- const [value, setValue] = (0, react_1.useState)(value_w.value);
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
- return value_w.subscribe((newValue) => {
42
- if ((0, lodash_1.isEqual)(newValue, value))
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
- }, [value_w]);
49
+ return () => unsubscribe.current();
50
+ }, [w, computer]);
47
51
  return value;
48
52
  }
49
53
  /**
@@ -71,7 +75,7 @@ function useWhispr(data) {
71
75
  * @param cb The callback to call on value change
72
76
  * @param unsafe If true, the callback will be allowed to throw errors, that will then bubble up
73
77
  */
74
- function useOnWhispr(w, unsafe = false, cb) {
78
+ function useOnWhispr(w, cb, unsafe = false) {
75
79
  (0, react_1.useEffect)(() => {
76
80
  const unsub = w.subscribe(cb, undefined, unsafe);
77
81
  return () => unsub();
@@ -209,11 +213,11 @@ function useAsync(f, data, debouce = 200) {
209
213
  */
210
214
  function useAsyncEffect(f, data, debounce = 200) {
211
215
  const dispatcher = useAsync(f, data, debounce);
212
- useOnWhispr(dispatcher.data, true, (data) => {
216
+ useOnWhispr(dispatcher.data, (data) => {
213
217
  if (!data.loading && !data.ok) {
214
218
  throw data.error;
215
219
  }
216
- });
220
+ }, true);
217
221
  }
218
222
  /**
219
223
  * Format a timestamp into a relative time string (e.g. "5 minutes ago", "in 2 hours"), using the browser locale.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.185",
3
+ "version": "0.0.187",
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": {