@cripty2001/utils 0.0.141 → 0.0.143

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