@cripty2001/utils 0.0.74 → 0.0.76

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.
@@ -79,9 +79,7 @@ class Client {
79
79
  case 401:
80
80
  case 403:
81
81
  if (testedToken === this.authToken.value) {
82
- console.log("Invalidating token");
83
82
  this.setAuthToken(null);
84
- console.log("Token invalidated");
85
83
  }
86
84
  throw new ClientError("Permission denied");
87
85
  case 200:
@@ -42,6 +42,14 @@ export declare function useCurrentTimestamp(refresh?: number): number;
42
42
  * @returns The debounced value
43
43
  */
44
44
  export declare function useDebounced<T>(value: T): T;
45
+ /**
46
+ * Allow for having a locally defined state, that can be kept synced with an external one, if available.
47
+ * @param def The default value
48
+ * @param value The value to sync
49
+ * @param setValue The function to set the value
50
+ * @returns The synced value and the function to set it
51
+ */
52
+ export declare function useSynced<T extends any>(def: T, value: T | undefined, setValue: ((value: T) => void) | undefined): [T, React.Dispatch<React.SetStateAction<T>>];
45
53
  /**
46
54
  *
47
55
  * Wraps an async function into a reactable data.
@@ -5,13 +5,14 @@ exports.useWhispr = useWhispr;
5
5
  exports.useOnWhispr = useOnWhispr;
6
6
  exports.useCurrentTimestamp = useCurrentTimestamp;
7
7
  exports.useDebounced = useDebounced;
8
+ exports.useSynced = useSynced;
8
9
  exports.useAsync = useAsync;
9
10
  exports.useRelTime = useRelTime;
10
11
  const whispr_1 = require("@cripty2001/whispr");
11
12
  const react_1 = require("react");
12
13
  const lodash_1 = require("lodash");
13
- const Dispatcher_1 = require("./Dispatcher");
14
14
  const _1 = require(".");
15
+ const Dispatcher_1 = require("./Dispatcher");
15
16
  /**
16
17
  * Convert a Whispr value into a reactive react value, usable in function components with the standard react reactive system.
17
18
  * If the value is not a Whispr, it is returned as is, thus allowing for progressive migration and adoption
@@ -91,6 +92,29 @@ function useDebounced(value) {
91
92
  }, [value]);
92
93
  return debounced;
93
94
  }
95
+ /**
96
+ * Allow for having a locally defined state, that can be kept synced with an external one, if available.
97
+ * @param def The default value
98
+ * @param value The value to sync
99
+ * @param setValue The function to set the value
100
+ * @returns The synced value and the function to set it
101
+ */
102
+ function useSynced(def, value, setValue) {
103
+ const [v, setV] = (0, react_1.useState)(def);
104
+ if (value !== undefined) {
105
+ (0, react_1.useEffect)(() => {
106
+ setV(value);
107
+ }, [value, setV]);
108
+ }
109
+ if (setValue !== undefined) {
110
+ (0, react_1.useEffect)(() => {
111
+ if ((0, lodash_1.isEqual)(v, value))
112
+ return;
113
+ setValue(v);
114
+ }, [v, setValue]);
115
+ }
116
+ return [v, setV];
117
+ }
94
118
  /**
95
119
  *
96
120
  * Wraps an async function into a reactable data.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.74",
3
+ "version": "0.0.76",
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": {