@cripty2001/utils 0.0.169 → 0.0.170

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.
@@ -29,6 +29,7 @@ export declare class Dispatcher<I, O> {
29
29
  *
30
30
  * @remarks If the debounce interval is 0, the function will be called synchronously, as a Whispr listener would do.
31
31
  * @remarks The value is deep checked for equality. The function will be called only if the value changed deeply
32
+ * @remarks Data updating flag is set in a synchronous way. This means that in the event your DEBOUNCE_INTERVAL is near infinity, you still get the data set to updating true immediately when you update the value. It just wait like this forever
32
33
  */
33
34
  constructor(value: Whispr<I>, f: DispatcherFunction<I, O>, DEBOUNCE_INTERVAL?: number);
34
35
  private reset;
@@ -21,6 +21,7 @@ class Dispatcher {
21
21
  *
22
22
  * @remarks If the debounce interval is 0, the function will be called synchronously, as a Whispr listener would do.
23
23
  * @remarks The value is deep checked for equality. The function will be called only if the value changed deeply
24
+ * @remarks Data updating flag is set in a synchronous way. This means that in the event your DEBOUNCE_INTERVAL is near infinity, you still get the data set to updating true immediately when you update the value. It just wait like this forever
24
25
  */
25
26
  constructor(value, f, DEBOUNCE_INTERVAL = 200) {
26
27
  // Initing state
@@ -1,4 +1,3 @@
1
- import { Whispr } from "@cripty2001/whispr";
2
1
  export type SearcherData<T> = {
3
2
  queries: string[];
4
3
  doc: T;
@@ -9,4 +8,3 @@ export declare class Searcher<T> {
9
8
  search(query: string): SearcherData<T>[];
10
9
  updateData(data: SearcherData<T>[]): void;
11
10
  }
12
- export declare function useSearcher_w<T>(data: Whispr<SearcherData<T>[]>, query: Whispr<string>): Whispr<SearcherData<T>[]>;
@@ -1,8 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Searcher = void 0;
4
- exports.useSearcher_w = useSearcher_w;
5
- const whispr_1 = require("@cripty2001/whispr");
6
4
  class Searcher {
7
5
  data = [];
8
6
  constructor(data) {
@@ -18,20 +16,20 @@ class Searcher {
18
16
  }
19
17
  }
20
18
  exports.Searcher = Searcher;
21
- function useSearcher_w(data, query) {
22
- const searcher = new Searcher(data.value);
23
- let unsubscribe_q = () => { };
24
- let unsubscribe_d = () => { };
25
- const [toReturn, setToReturn] = whispr_1.Whispr.create([], () => {
26
- unsubscribe_q();
27
- unsubscribe_d();
28
- });
29
- unsubscribe_q = query.subscribe((q) => {
30
- let result = searcher.search(q);
31
- setToReturn(result);
32
- }, true);
33
- unsubscribe_d = data.subscribe((d) => {
34
- searcher.updateData(d);
35
- }, true);
36
- return toReturn;
37
- }
19
+ // export function useSearcher_w<T>(data: Whispr<SearcherData<T>[]>, query: Whispr<string>): Whispr<SearcherData<T>[]> {
20
+ // const searcher = new Searcher<T>(data.value);
21
+ // let unsubscribe_q: () => void = () => { }
22
+ // let unsubscribe_d: () => void = () => { }
23
+ // const [toReturn, setToReturn] = Whispr.create<SearcherData<T>[]>([], () => {
24
+ // unsubscribe_q();
25
+ // unsubscribe_d();
26
+ // })
27
+ // unsubscribe_q = query.subscribe((q) => {
28
+ // let result = searcher.search(q);
29
+ // setToReturn(result);
30
+ // }, true)
31
+ // unsubscribe_d = data.subscribe((d) => {
32
+ // searcher.updateData(d);
33
+ // }, true)
34
+ // return toReturn
35
+ // }
@@ -119,11 +119,13 @@ export declare function useRelTime(_data: number | Date): string;
119
119
  /**
120
120
  * React shorthand for the Searcher
121
121
  * @param data The data to search on
122
- * @param q The query to search for
123
- * @return The filtered data
124
- *
122
+ * @return An arra containing
123
+ * - The current query (updated synchronously with the user input)
124
+ * - The function to set the query (aka the one to put onto the input element)
125
+ * - The filtered data
126
+ * - A boolean indicating if the search is pending
125
127
  */
126
- export declare function useSearcher<T>(data: SearcherData<T>[], q: string): SearcherData<T>[];
128
+ export declare function useSearcher<T extends JSONEncodable>(data: SearcherData<T>[]): [string, (q: string) => void, SearcherData<T>[], boolean];
127
129
  /**
128
130
  * A react ref hook with safe lazy initialization, ready for safe side effects.
129
131
  * @remarks The initialization function will only be called once, and the result will be stored in the ref.
@@ -274,15 +274,36 @@ function getRelTimeFormat(_diff) {
274
274
  /**
275
275
  * React shorthand for the Searcher
276
276
  * @param data The data to search on
277
- * @param q The query to search for
278
- * @return The filtered data
279
- *
277
+ * @return An arra containing
278
+ * - The current query (updated synchronously with the user input)
279
+ * - The function to set the query (aka the one to put onto the input element)
280
+ * - The filtered data
281
+ * - A boolean indicating if the search is pending
280
282
  */
281
- function useSearcher(data, q) {
282
- const q_w = useWhispr(q);
283
- const data_w = useWhispr(data);
284
- const searcher = (0, Searcher_1.useSearcher_w)(data_w, q_w);
285
- return useWhisprValue(searcher);
283
+ function useSearcher(data) {
284
+ const [pending, setPending] = (0, react_1.useState)(false);
285
+ const [results, setResults] = (0, react_1.useState)({
286
+ results: [],
287
+ _meta: {
288
+ ts: 0,
289
+ config: { q: "" }
290
+ }
291
+ });
292
+ const searcher = (0, react_1.useRef)(new Searcher_1.Searcher(data));
293
+ (0, react_1.useEffect)(() => {
294
+ searcher.current.updateData(data);
295
+ }, [data]);
296
+ const [q, setQ] = useAsyncInput(results, setResults, async ({ q }) => {
297
+ return {
298
+ results: searcher.current.search(q)
299
+ };
300
+ }, setPending);
301
+ return [
302
+ q.q,
303
+ (q) => setQ(draft => { draft.q = q; }),
304
+ results.results,
305
+ pending
306
+ ];
286
307
  }
287
308
  /**
288
309
  * A react ref hook with safe lazy initialization, ready for safe side effects.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.169",
3
+ "version": "0.0.170",
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": {