@cripty2001/utils 0.0.250 → 0.0.252

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.
@@ -64,21 +64,25 @@ export declare function useSynced<T extends any>(def: T, value: T | undefined, s
64
64
  * Check the dispatcher's `data` property to access the error state. The dispatcher's promise resolves successfully
65
65
  * even when errors occur - errors are captured and stored in the reactive state for UI consumption.
66
66
  *
67
- * @param f The async function to call. It should return a promise that resolves to the data. It is not reactive.
68
67
  * @param data The data to give to f. It must be stable, as anything in the dependency array of the useEffect and similars in the react ecosystem. If null, this function will act like an useEffect with an empty dependency array.
69
68
  * @param debouce Debounce time in ms. Default to 200ms. The async function will not be called if this time has not passed since the useAsync first invocation or value change. If another change happens during the wait, the first function call is never executed. Not reactive
70
- * @returns A Dispatcher object containing:
71
- * - `data`: A Whispr<DispatcherStatePayload<O>> that contains the loading state, progress, and either the result data or error
72
- * - `filtered`: A Whispr<O | null> that contains the result data when successful, or null when loading or on error
69
+ * @param interval The interval to re-execute the async function. If null, the async function will not be re-executed.
70
+ * @param f The async function to call. It should return a promise that resolves to the data. It is not reactive.
71
+
72
+ * @returns An array containing:
73
+ * - A Dispatcher object containing:
74
+ * -- `data`: A Whispr<DispatcherStatePayload<O>> that contains the loading state, progress, and either the result data or error
75
+ * -- `filtered`: A Whispr<O | null> that contains the result data when successful, or null when loading or on error
76
+ * - A function to trigger a manual refresh
73
77
  *
74
78
  * @type I Input for the async function.
75
79
  * @type O Output for the async function.
76
80
  *
77
81
  * @example
78
- * const dispatcher = useAsync(async (userId) => {
82
+ * const [dispatcher, refresh] = useAsync(userId, 200, null, async (userId) => {
79
83
  * const response = await fetch(`/api/users/${userId}`);
80
84
  * return response.json();
81
- * }, userId);
85
+ * });
82
86
  *
83
87
  * const state = useWhisprValue(dispatcher.data);
84
88
  * // state can be: { loading: true, progress: 0 } | { loading: false, ok: true, data: T } | { loading: false, ok: false, error: Error }
@@ -87,17 +91,7 @@ export declare function useSynced<T extends any>(def: T, value: T | undefined, s
87
91
  * console.error('Error:', state.error);
88
92
  * }
89
93
  */
90
- export declare function useAsync<I, O>(f: (input: I, setProgress: (p: number) => void, signal: AbortSignal) => Promise<O>, data: I, debouce?: number): Dispatcher<any, O>;
91
- /**
92
- * Like useAsync, but it also automatically re-execute the async function periodically
93
- *
94
- * @param data @see useAsync
95
- * @param interval The interval to re-execute the async function. If null, the async function will not be re-executed.
96
- * @param f @see useAsync
97
- *
98
- * @returns [@see useAsync, A function to trigger a manual refresh]
99
- */
100
- export declare function useAsyncPeriodic<I, O>(data: I, interval: number, f: (input: I, setProgress: (p: number) => void, signal: AbortSignal) => Promise<O>): [Dispatcher<any, O>, () => void];
94
+ export declare function useAsync<I, O>(data: I, debouce: number, interval: number | null, f: (input: I, setProgress: (p: number) => void, signal: AbortSignal) => Promise<O>): [Dispatcher<any, O>, () => void];
101
95
  /**
102
96
  * Async version of useEffect with debouncing. Executes an async function as a side effect when data changes.
103
97
  *
@@ -8,7 +8,6 @@ exports.useClock = useClock;
8
8
  exports.useDebounced = useDebounced;
9
9
  exports.useSynced = useSynced;
10
10
  exports.useAsync = useAsync;
11
- exports.useAsyncPeriodic = useAsyncPeriodic;
12
11
  exports.useAsyncEffect = useAsyncEffect;
13
12
  exports.useRelTime = useRelTime;
14
13
  exports.useSearcher = useSearcher;
@@ -154,6 +153,55 @@ function useSynced(def, value, setValue) {
154
153
  }, []);
155
154
  return [v, syncedSetter];
156
155
  }
156
+ // /**
157
+ // * Wraps an async function into a reactable data structure that tracks loading state, progress, and results.
158
+ // *
159
+ // * **Error Handling:** This function does NOT throw errors. Instead, errors are stored in the returned dispatcher's state.
160
+ // * Check the dispatcher's `data` property to access the error state. The dispatcher's promise resolves successfully
161
+ // * even when errors occur - errors are captured and stored in the reactive state for UI consumption.
162
+ // *
163
+ // * @param f The async function to call. It should return a promise that resolves to the data. It is not reactive.
164
+ // * @param data The data to give to f. It must be stable, as anything in the dependency array of the useEffect and similars in the react ecosystem. If null, this function will act like an useEffect with an empty dependency array.
165
+ // * @param debouce Debounce time in ms. Default to 200ms. The async function will not be called if this time has not passed since the useAsync first invocation or value change. If another change happens during the wait, the first function call is never executed. Not reactive
166
+ // * @returns A Dispatcher object containing:
167
+ // * - `data`: A Whispr<DispatcherStatePayload<O>> that contains the loading state, progress, and either the result data or error
168
+ // * - `filtered`: A Whispr<O | null> that contains the result data when successful, or null when loading or on error
169
+ // *
170
+ // * @type I Input for the async function.
171
+ // * @type O Output for the async function.
172
+ // *
173
+ // * @example
174
+ // * const dispatcher = useAsync(async (userId) => {
175
+ // * const response = await fetch(`/api/users/${userId}`);
176
+ // * return response.json();
177
+ // * }, userId);
178
+ // *
179
+ // * const state = useWhisprValue(dispatcher.data);
180
+ // * // state can be: { loading: true, progress: 0 } | { loading: false, ok: true, data: T } | { loading: false, ok: false, error: Error }
181
+ // *
182
+ // * if (!state.loading && !state.ok) {
183
+ // * console.error('Error:', state.error);
184
+ // * }
185
+ // */
186
+ // export function useAsync<I, O>(
187
+ // f: (input: I, setProgress: (p: number) => void, signal: AbortSignal) => Promise<O>,
188
+ // data: I,
189
+ // debouce: number = 200
190
+ // ): Dispatcher<any, O> {
191
+ // // Initing reactive input
192
+ // const [input, setInput] = useSafeRef(() => Whispr.create(data ?? getRandomId() as I));
193
+ // useEffect(() => {
194
+ // if (data !== null) {
195
+ // setInput(data); // Debouncing already handled by dispatcher
196
+ // }
197
+ // }, [data, setInput]);
198
+ // // Initing dispatcher
199
+ // const dispatcher: Dispatcher<I, O> = useSafeRef(() =>
200
+ // new Dispatcher<I, O>(input, f, debouce)
201
+ // );
202
+ // // Returning dispatcher
203
+ // return dispatcher;
204
+ // }
157
205
  /**
158
206
  * Wraps an async function into a reactable data structure that tracks loading state, progress, and results.
159
207
  *
@@ -161,21 +209,25 @@ function useSynced(def, value, setValue) {
161
209
  * Check the dispatcher's `data` property to access the error state. The dispatcher's promise resolves successfully
162
210
  * even when errors occur - errors are captured and stored in the reactive state for UI consumption.
163
211
  *
164
- * @param f The async function to call. It should return a promise that resolves to the data. It is not reactive.
165
212
  * @param data The data to give to f. It must be stable, as anything in the dependency array of the useEffect and similars in the react ecosystem. If null, this function will act like an useEffect with an empty dependency array.
166
213
  * @param debouce Debounce time in ms. Default to 200ms. The async function will not be called if this time has not passed since the useAsync first invocation or value change. If another change happens during the wait, the first function call is never executed. Not reactive
167
- * @returns A Dispatcher object containing:
168
- * - `data`: A Whispr<DispatcherStatePayload<O>> that contains the loading state, progress, and either the result data or error
169
- * - `filtered`: A Whispr<O | null> that contains the result data when successful, or null when loading or on error
214
+ * @param interval The interval to re-execute the async function. If null, the async function will not be re-executed.
215
+ * @param f The async function to call. It should return a promise that resolves to the data. It is not reactive.
216
+
217
+ * @returns An array containing:
218
+ * - A Dispatcher object containing:
219
+ * -- `data`: A Whispr<DispatcherStatePayload<O>> that contains the loading state, progress, and either the result data or error
220
+ * -- `filtered`: A Whispr<O | null> that contains the result data when successful, or null when loading or on error
221
+ * - A function to trigger a manual refresh
170
222
  *
171
223
  * @type I Input for the async function.
172
224
  * @type O Output for the async function.
173
225
  *
174
226
  * @example
175
- * const dispatcher = useAsync(async (userId) => {
227
+ * const [dispatcher, refresh] = useAsync(userId, 200, null, async (userId) => {
176
228
  * const response = await fetch(`/api/users/${userId}`);
177
229
  * return response.json();
178
- * }, userId);
230
+ * });
179
231
  *
180
232
  * const state = useWhisprValue(dispatcher.data);
181
233
  * // state can be: { loading: true, progress: 0 } | { loading: false, ok: true, data: T } | { loading: false, ok: false, error: Error }
@@ -184,7 +236,7 @@ function useSynced(def, value, setValue) {
184
236
  * console.error('Error:', state.error);
185
237
  * }
186
238
  */
187
- function useAsync(f, data, debouce = 200) {
239
+ function useAsync(data, debouce, interval, f) {
188
240
  // Initing reactive input
189
241
  const [input, setInput] = useSafeRef(() => whispr_1.Whispr.create(data ?? (0, _1.getRandomId)()));
190
242
  (0, react_1.useEffect)(() => {
@@ -192,32 +244,25 @@ function useAsync(f, data, debouce = 200) {
192
244
  setInput(data); // Debouncing already handled by dispatcher
193
245
  }
194
246
  }, [data, setInput]);
247
+ // Initing refresh bundle
248
+ const timer = interval !== null ?
249
+ (0, _1.getClock)(interval) :
250
+ whispr_1.Whispr.create(0)[0];
251
+ const [refresh, setRefresh] = whispr_1.Whispr.create(Math.random());
252
+ const bundle = whispr_1.Whispr.from({
253
+ input,
254
+ timer,
255
+ refresh
256
+ }, ({ input, timer, refresh }) => ({
257
+ data: input,
258
+ timer,
259
+ refresh
260
+ }));
195
261
  // Initing dispatcher
196
- const dispatcher = useSafeRef(() => new Dispatcher_1.Dispatcher(input, f, debouce));
197
- // Returning dispatcher
198
- return dispatcher;
199
- }
200
- /**
201
- * Like useAsync, but it also automatically re-execute the async function periodically
202
- *
203
- * @param data @see useAsync
204
- * @param interval The interval to re-execute the async function. If null, the async function will not be re-executed.
205
- * @param f @see useAsync
206
- *
207
- * @returns [@see useAsync, A function to trigger a manual refresh]
208
- */
209
- function useAsyncPeriodic(data, interval, f) {
210
- const timer = useClock(interval);
211
- const [refresh, setRefresh] = (0, react_1.useState)(Math.random());
212
- const bundle = (0, react_1.useMemo)(() => {
213
- return {
214
- data,
215
- timer,
216
- refresh
217
- };
218
- }, [data, timer, refresh]);
262
+ const dispatcher = useSafeRef(() => new Dispatcher_1.Dispatcher(bundle, (i, p, s) => f(i.data, p, s), debouce));
263
+ // Returning dispatcher and refresh function
219
264
  return [
220
- useAsync((i, p, s) => f(i.data, p, s), bundle),
265
+ dispatcher,
221
266
  () => {
222
267
  setRefresh(Math.random());
223
268
  }
@@ -248,7 +293,7 @@ function useAsyncPeriodic(data, interval, f) {
248
293
  * }, userId, 300);
249
294
  */
250
295
  function useAsyncEffect(f, data, debounce = 200) {
251
- const dispatcher = useAsync(f, data, debounce);
296
+ const [dispatcher] = useAsync(data, debounce, null, f);
252
297
  useOnWhispr(dispatcher.data, (data) => {
253
298
  if (!data.loading && !data.ok) {
254
299
  throw data.error;
@@ -432,7 +477,7 @@ function useAsyncInput(value, setValue, handler, setPending = () => { }) {
432
477
  setMeta(value._meta);
433
478
  }
434
479
  }, [value, meta, setMeta]);
435
- const result_d = useAsync(async ({ config, ts }) => {
480
+ const [result_d] = useAsync(meta, 0, null, async ({ config, ts }) => {
436
481
  const r = await handler(config);
437
482
  return {
438
483
  ...r,
@@ -441,7 +486,7 @@ function useAsyncInput(value, setValue, handler, setPending = () => { }) {
441
486
  config: config,
442
487
  },
443
488
  };
444
- }, meta, 0);
489
+ });
445
490
  const result = useWhisprValue(result_d.filtered);
446
491
  (0, react_1.useEffect)(() => {
447
492
  if (result === null)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.250",
3
+ "version": "0.0.252",
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": {