@cripty2001/utils 0.0.140 → 0.0.142

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,3 +79,10 @@ export declare function useRelTime(refresh?: number): (ts: Date | number) => str
79
79
  *
80
80
  */
81
81
  export declare function useSearcher<T>(data: SearcherData<T>[], q: string): SearcherData<T>[];
82
+ /**
83
+ * A react ref hook with safe laxy initialization, ready for safe effects.
84
+ * @remarks The initialization function will only be called once, and the result will be stored in the ref.
85
+ * @param value The value to initialize the ref with. If a function is provided, it will be called to initialize the ref.
86
+ * @returns
87
+ */
88
+ export declare function useSafeRef<T>(value: (() => T)): T;
@@ -9,6 +9,7 @@ exports.useSynced = useSynced;
9
9
  exports.useAsync = useAsync;
10
10
  exports.useRelTime = useRelTime;
11
11
  exports.useSearcher = useSearcher;
12
+ exports.useSafeRef = useSafeRef;
12
13
  const whispr_1 = require("@cripty2001/whispr");
13
14
  const react_1 = require("react");
14
15
  const lodash_1 = require("lodash");
@@ -148,18 +149,14 @@ function useSynced(def, value, setValue) {
148
149
  */
149
150
  function useAsync(f, data, debouce = 200) {
150
151
  // Initing reactive input
151
- const whisprRef = (0, react_1.useRef)(null);
152
- if (!whisprRef.current) {
153
- whisprRef.current = whispr_1.Whispr.create(data ?? (0, _1.getRandomId)());
154
- }
155
- const [input, setInput] = whisprRef.current;
152
+ const [input, setInput] = useSafeRef(() => whispr_1.Whispr.create(data ?? (0, _1.getRandomId)()));
156
153
  if (data !== null) {
157
154
  (0, react_1.useEffect)(() => {
158
155
  setInput(data); // Debouncing already handled by dispatcher
159
156
  }, [data, setInput]);
160
157
  }
161
158
  // Initing dispatcher
162
- const dispatcher = (0, react_1.useRef)(new Dispatcher_1.Dispatcher(input, f, debouce)).current;
159
+ const dispatcher = useSafeRef(() => new Dispatcher_1.Dispatcher(input, f, debouce));
163
160
  // Returning dispatcher
164
161
  return dispatcher;
165
162
  }
@@ -234,3 +231,18 @@ function useSearcher(data, q) {
234
231
  const searcher = (0, Searcher_1.useSearcher_w)(data_w, q_w);
235
232
  return useWhisprValue(searcher);
236
233
  }
234
+ /**
235
+ * A react ref hook with safe laxy initialization, ready for safe effects.
236
+ * @remarks The initialization function will only be called once, and the result will be stored in the ref.
237
+ * @param value The value to initialize the ref with. If a function is provided, it will be called to initialize the ref.
238
+ * @returns
239
+ */
240
+ function useSafeRef(value) {
241
+ const ref = (0, react_1.useRef)(null);
242
+ (0, react_1.useEffect)(() => {
243
+ if (ref.current !== null)
244
+ return;
245
+ ref.current = value();
246
+ }, []);
247
+ return ref.current;
248
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.140",
3
+ "version": "0.0.142",
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": {