@data-fair/lib-vue 1.25.2 → 1.25.3

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/deep-diff.d.ts CHANGED
@@ -1,3 +1,11 @@
1
1
  import { type ComputedGetter, type ComputedOptions, type WatchSource, type WatchCallback, type WatchOptions } from 'vue';
2
+ /**
3
+ * a computed replacement that returns the old value if the new value only has a superficial "reference" change but is actually identitical
4
+ * prevents unnecessary triggers or reactivity
5
+ */
2
6
  export declare const computedDeepDiff: <Type>(getter: ComputedGetter<Type>, options?: ComputedOptions) => import("vue").ComputedRef<Type>;
7
+ /**
8
+ * a watch replacement the triggers the callback only if the new value is actually different from the old value
9
+ * not just for superficial "reference" changes
10
+ */
3
11
  export declare const watchDeepDiff: (source: WatchSource, callback: WatchCallback, options?: WatchOptions) => import("vue").WatchHandle;
package/deep-diff.js CHANGED
@@ -1,11 +1,19 @@
1
1
  import { computed, watch } from 'vue';
2
2
  import equal from 'fast-deep-equal';
3
+ /**
4
+ * a computed replacement that returns the old value if the new value only has a superficial "reference" change but is actually identitical
5
+ * prevents unnecessary triggers or reactivity
6
+ */
3
7
  export const computedDeepDiff = (getter, options) => {
4
8
  return computed((oldValue) => {
5
9
  const newValue = getter();
6
10
  return (oldValue !== undefined && equal(newValue, oldValue)) ? oldValue : newValue;
7
11
  }, options);
8
12
  };
13
+ /**
14
+ * a watch replacement the triggers the callback only if the new value is actually different from the old value
15
+ * not just for superficial "reference" changes
16
+ */
9
17
  export const watchDeepDiff = (source, callback, options) => {
10
18
  return watch(source, (newValue, oldValue, onCleanup) => {
11
19
  if (!equal(newValue, oldValue))
package/edit-fetch.d.ts CHANGED
@@ -17,6 +17,7 @@ export declare function useEditFetch<T extends Record<string, any>>(url: Optiona
17
17
  };
18
18
  data: [T | null] extends [Ref<any, any>] ? import("@vue/shared").IfAny<Ref<any, any> & T, Ref<Ref<any, any> & T, Ref<any, any> & T>, Ref<any, any> & T> : Ref<import("vue").UnwrapRef<T> | null, T | import("vue").UnwrapRef<T> | null>;
19
19
  serverData: [T | null] extends [Ref<any, any>] ? import("@vue/shared").IfAny<Ref<any, any> & T, Ref<Ref<any, any> & T, Ref<any, any> & T>, Ref<any, any> & T> : Ref<import("vue").UnwrapRef<T> | null, T | import("vue").UnwrapRef<T> | null>;
20
+ hasDiff: import("vue").ComputedRef<boolean>;
20
21
  save: {
21
22
  execute: () => Promise<void>;
22
23
  notif: Ref<import("./ui-notif.js").UiNotif | undefined>;
package/edit-fetch.js CHANGED
@@ -1,4 +1,4 @@
1
- import { ref, watch } from 'vue';
1
+ import { ref, watch, computed } from 'vue';
2
2
  import { ofetch } from 'ofetch';
3
3
  import { useFetch } from './fetch.js';
4
4
  import useAsyncAction from './async-action.js';
@@ -12,15 +12,26 @@ export function useEditFetch(url, options = {}) {
12
12
  serverData.value = fetch.data.value;
13
13
  data.value = fetch.data.value;
14
14
  });
15
+ const hasDiff = computed(() => !equal(data.value, serverData.value));
16
+ const getPatch = () => {
17
+ if (!data.value || !serverData.value)
18
+ return null;
19
+ const patch = {};
20
+ for (const key of Object.keys(data.value)) {
21
+ if (!equal(data.value[key], serverData.value[key]))
22
+ patch[key] = data.value[key];
23
+ }
24
+ for (const key of Object.keys(serverData.value)) {
25
+ if (!(key in data.value))
26
+ patch[key] = null;
27
+ }
28
+ return patch;
29
+ };
15
30
  const save = useAsyncAction(async () => {
16
31
  if (!data.value || !serverData.value || !fetch.data.value)
17
32
  throw new Error('cannot save data that has not been fetched yet');
18
33
  if (options.patch) {
19
- const patch = {};
20
- for (const key of Object.keys(data.value)) {
21
- if (!equal(data.value[key], serverData.value[key]))
22
- patch[key] = data.value[key];
23
- }
34
+ const patch = getPatch();
24
35
  if (!Object.keys(patch).length)
25
36
  return;
26
37
  serverData.value = await ofetch(fetch.fullUrl.value, { method: 'PATCH', body: patch });
@@ -35,6 +46,7 @@ export function useEditFetch(url, options = {}) {
35
46
  fetch,
36
47
  data,
37
48
  serverData,
49
+ hasDiff,
38
50
  save
39
51
  };
40
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-vue",
3
- "version": "1.25.2",
3
+ "version": "1.25.3",
4
4
  "description": "Composables and other utilities for Vue applications in the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
package/vite.d.ts CHANGED
@@ -5,6 +5,7 @@ export declare const autoImports: (string | {
5
5
  '@data-fair/lib-vue/concept-filters.js': string[];
6
6
  '@data-fair/lib-vue/ui-notif.js': string[];
7
7
  '@data-fair/lib-vue/fetch.js': string[];
8
+ '@data-fair/lib-vue/edit-fetch.js': string[];
8
9
  '@data-fair/lib-vue/ws.js': string[];
9
10
  '@data-fair/lib-vue/async-action.js': string[];
10
11
  '@data-fair/lib-vue/deep-diff.js': string[];
package/vite.js CHANGED
@@ -10,6 +10,7 @@ export const autoImports = [
10
10
  '@data-fair/lib-vue/concept-filters.js': ['useConceptFilters'],
11
11
  '@data-fair/lib-vue/ui-notif.js': ['useUiNotif', 'withUiNotif'],
12
12
  '@data-fair/lib-vue/fetch.js': ['useFetch'],
13
+ '@data-fair/lib-vue/edit-fetch.js': ['useEditFetch'],
13
14
  '@data-fair/lib-vue/ws.js': ['useWS'],
14
15
  '@data-fair/lib-vue/async-action.js': ['useAsyncAction'],
15
16
  '@data-fair/lib-vue/deep-diff.js': ['computedDeepDiff', 'watchDeepDiff'],