@cripty2001/utils 0.0.135 → 0.0.137

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.
@@ -56,7 +56,7 @@ export declare function useSynced<T extends any>(def: T, value: T | undefined, s
56
56
  * Wraps an async function into a reactable data.
57
57
  *
58
58
  * @param f The async function to call. It should return a promise that resolves to the data. It is not reactive
59
- * @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.
59
+ * @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.
60
60
  * @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
61
61
  * @returns The dispatcher
62
62
  *
@@ -139,7 +139,7 @@ function useSynced(def, value, setValue) {
139
139
  * Wraps an async function into a reactable data.
140
140
  *
141
141
  * @param f The async function to call. It should return a promise that resolves to the data. It is not reactive
142
- * @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.
142
+ * @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.
143
143
  * @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
144
144
  * @returns The dispatcher
145
145
  *
@@ -148,10 +148,12 @@ function useSynced(def, value, setValue) {
148
148
  */
149
149
  function useAsync(f, data, debouce = 200) {
150
150
  // Initing reactive input
151
- const [input, setInput] = (0, react_1.useRef)(whispr_1.Whispr.create(data)).current;
152
- (0, react_1.useEffect)(() => {
153
- setInput(data); // Debouncing already handled by dispatcher
154
- }, [data, setInput]);
151
+ const [input, setInput] = (0, react_1.useRef)(whispr_1.Whispr.create(data ?? (0, _1.getRandomId)())).current;
152
+ if (data !== null) {
153
+ (0, react_1.useEffect)(() => {
154
+ setInput(data); // Debouncing already handled by dispatcher
155
+ }, [data, setInput]);
156
+ }
155
157
  // Initing dispatcher
156
158
  const dispatcher = (0, react_1.useRef)(new Dispatcher_1.Dispatcher(input, f, debouce)).current;
157
159
  // Returning dispatcher
@@ -169,20 +171,36 @@ function useRelTime(refresh = 1000) {
169
171
  const getFormat = (_diff) => {
170
172
  const diff = Math.abs(_diff);
171
173
  const breakpoints = [
172
- { limit: 60, unit: "second" },
173
- { limit: 3600, unit: "minute" },
174
- { limit: 86400, unit: "hour" },
175
- { limit: 604800, unit: "day" },
174
+ {
175
+ base: 1,
176
+ limit: 60,
177
+ unit: "second"
178
+ },
179
+ {
180
+ base: 60,
181
+ limit: 60,
182
+ unit: "minute"
183
+ },
184
+ {
185
+ base: 60 * 60,
186
+ limit: 24,
187
+ unit: "hour"
188
+ },
189
+ {
190
+ base: 60 * 60 * 24,
191
+ limit: 45,
192
+ unit: "day"
193
+ },
176
194
  ];
177
- for (const { limit, unit } of breakpoints) {
178
- if (diff < limit)
195
+ for (const { base, limit, unit } of breakpoints) {
196
+ if (diff < limit * base)
179
197
  return {
180
- limit,
198
+ base,
181
199
  unit
182
200
  };
183
201
  }
184
202
  return {
185
- limit: 2592000,
203
+ base: 60 * 60 * 24 * 7,
186
204
  unit: "week"
187
205
  };
188
206
  };
@@ -191,10 +209,10 @@ function useRelTime(refresh = 1000) {
191
209
  const then = ts instanceof Date ? ts.getTime() : ts;
192
210
  const delta = then - now;
193
211
  const seconds = Math.round(delta / 1000);
194
- const { limit, unit } = getFormat(seconds);
212
+ const { base, unit } = getFormat(seconds);
195
213
  const rounded = seconds > 0 ?
196
- Math.floor(seconds / limit) :
197
- Math.ceil(seconds / limit);
214
+ Math.floor(seconds / base) :
215
+ Math.ceil(seconds / base);
198
216
  return rtf.format(rounded, unit);
199
217
  }, [currTs, rtf]);
200
218
  return cb;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.135",
3
+ "version": "0.0.137",
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": {