@milaboratories/uikit 2.2.65 → 2.2.66

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milaboratories/uikit",
3
- "version": "2.2.65",
3
+ "version": "2.2.66",
4
4
  "type": "module",
5
5
  "main": "dist/pl-uikit.umd.js",
6
6
  "module": "dist/pl-uikit.js",
@@ -35,8 +35,8 @@
35
35
  "svgo": "^3.3.2",
36
36
  "@types/d3": "^7.4.3",
37
37
  "@milaboratories/eslint-config": "^1.0.4",
38
- "@milaboratories/helpers": "^1.6.11",
39
- "@platforma-sdk/model": "^1.28.1"
38
+ "@platforma-sdk/model": "^1.28.1",
39
+ "@milaboratories/helpers": "^1.6.11"
40
40
  },
41
41
  "scripts": {
42
42
  "dev": "vite",
@@ -0,0 +1,68 @@
1
+ import type { WatchSource, WatchOptions } from 'vue';
2
+ import { reactive, watch, ref, computed } from 'vue';
3
+ import { exclusiveRequest } from '@milaboratories/helpers';
4
+
5
+ export type FetchResult<V, E = unknown> = {
6
+ loading: boolean;
7
+ value: V | undefined;
8
+ error: E;
9
+ };
10
+
11
+ // TODO Should we keep the old value while fetching the new value?
12
+
13
+ /**
14
+ * Watch any reactive source and perform an asynchronous operation
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const v = useWatchFetch(
19
+ * watchSource,
20
+ * async (sourceValue) => {
21
+ * return await fetchDataFromApi(sourceValue);
22
+ * }
23
+ * );
24
+ *
25
+ * // Usage in a template
26
+ * <template>
27
+ * <div v-if="v.loading">Loading...</div>
28
+ * <div v-else-if="v.error">Error: {{ v.error.message }}</div>
29
+ * <div v-else>Data: {{ v.value }}</div>
30
+ * </template>
31
+ * ```
32
+ */
33
+ export function useWatchFetch<S, V>(watchSource: WatchSource<S>, doFetch: (s: S) => Promise<V>, watchOptions?: WatchOptions): FetchResult<V> {
34
+ const loadingRef = ref(0);
35
+
36
+ const data = reactive({
37
+ loading: computed(() => loadingRef.value > 0),
38
+ loadingRef,
39
+ value: undefined as V,
40
+ error: undefined,
41
+ }) as FetchResult<V>;
42
+
43
+ const exclusive = exclusiveRequest(doFetch);
44
+
45
+ watch(
46
+ watchSource,
47
+ async (s) => {
48
+ data.error = undefined;
49
+ loadingRef.value++;
50
+ exclusive(s)
51
+ .then((res) => {
52
+ if (res.ok) {
53
+ data.value = res.value;
54
+ }
55
+ })
56
+ .catch((err) => {
57
+ data.value = undefined;
58
+ data.error = err;
59
+ })
60
+ .finally(() => {
61
+ loadingRef.value--;
62
+ });
63
+ },
64
+ Object.assign({ immediate: true, deep: true }, watchOptions ?? {}),
65
+ );
66
+
67
+ return data;
68
+ }
package/src/index.ts CHANGED
@@ -96,6 +96,7 @@ export { useFormState } from './composition/useFormState';
96
96
  export { useQuery } from './composition/useQuery.ts';
97
97
  export { useDraggable } from './composition/useDraggable';
98
98
  export { useComponentProp } from './composition/useComponentProp';
99
+ export * from './composition/useWatchFetch';
99
100
 
100
101
  /**
101
102
  * Utils/Partials