@cripty2001/utils 0.0.139 → 0.0.141
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.
- package/dist/react-whispr.d.ts +7 -0
- package/dist/react-whispr.js +21 -1
- package/package.json +1 -1
package/dist/react-whispr.d.ts
CHANGED
|
@@ -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;
|
package/dist/react-whispr.js
CHANGED
|
@@ -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,7 +149,11 @@ function useSynced(def, value, setValue) {
|
|
|
148
149
|
*/
|
|
149
150
|
function useAsync(f, data, debouce = 200) {
|
|
150
151
|
// Initing reactive input
|
|
151
|
-
const
|
|
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
157
|
if (data !== null) {
|
|
153
158
|
(0, react_1.useEffect)(() => {
|
|
154
159
|
setInput(data); // Debouncing already handled by dispatcher
|
|
@@ -230,3 +235,18 @@ function useSearcher(data, q) {
|
|
|
230
235
|
const searcher = (0, Searcher_1.useSearcher_w)(data_w, q_w);
|
|
231
236
|
return useWhisprValue(searcher);
|
|
232
237
|
}
|
|
238
|
+
/**
|
|
239
|
+
* A react ref hook with safe laxy initialization, ready for safe effects.
|
|
240
|
+
* @remarks The initialization function will only be called once, and the result will be stored in the ref.
|
|
241
|
+
* @param value The value to initialize the ref with. If a function is provided, it will be called to initialize the ref.
|
|
242
|
+
* @returns
|
|
243
|
+
*/
|
|
244
|
+
function useSafeRef(value) {
|
|
245
|
+
const ref = (0, react_1.useRef)(null);
|
|
246
|
+
(0, react_1.useEffect)(() => {
|
|
247
|
+
if (ref.current !== null)
|
|
248
|
+
return;
|
|
249
|
+
ref.current = value();
|
|
250
|
+
}, []);
|
|
251
|
+
return ref.current;
|
|
252
|
+
}
|
package/package.json
CHANGED