@data-fair/lib-vue 1.25.2 → 1.25.4
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 +8 -0
- package/deep-diff.js +8 -0
- package/edit-fetch.d.ts +1 -0
- package/edit-fetch.js +22 -9
- package/package.json +1 -1
- package/vite.d.ts +1 -0
- package/vite.js +1 -0
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,26 +1,38 @@
|
|
|
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';
|
|
5
5
|
import equal from 'fast-deep-equal';
|
|
6
|
+
import clone from '@data-fair/lib-utils/clone.js';
|
|
6
7
|
export function useEditFetch(url, options = {}) {
|
|
7
8
|
const fetch = useFetch(url, options);
|
|
8
9
|
const serverData = ref(null);
|
|
9
10
|
const data = ref(null);
|
|
10
11
|
watch(fetch.data, () => {
|
|
11
12
|
// TODO: check for local changes before overwriting ?
|
|
12
|
-
serverData.value = fetch.data.value;
|
|
13
|
-
data.value = fetch.data.value;
|
|
13
|
+
serverData.value = clone(fetch.data.value);
|
|
14
|
+
data.value = clone(fetch.data.value);
|
|
14
15
|
});
|
|
16
|
+
const hasDiff = computed(() => !equal(data.value, serverData.value));
|
|
17
|
+
const getPatch = () => {
|
|
18
|
+
if (!data.value || !serverData.value)
|
|
19
|
+
return null;
|
|
20
|
+
const patch = {};
|
|
21
|
+
for (const key of Object.keys(data.value)) {
|
|
22
|
+
if (!equal(data.value[key], serverData.value[key]))
|
|
23
|
+
patch[key] = data.value[key];
|
|
24
|
+
}
|
|
25
|
+
for (const key of Object.keys(serverData.value)) {
|
|
26
|
+
if (!(key in data.value))
|
|
27
|
+
patch[key] = null;
|
|
28
|
+
}
|
|
29
|
+
return patch;
|
|
30
|
+
};
|
|
15
31
|
const save = useAsyncAction(async () => {
|
|
16
32
|
if (!data.value || !serverData.value || !fetch.data.value)
|
|
17
33
|
throw new Error('cannot save data that has not been fetched yet');
|
|
18
34
|
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
|
-
}
|
|
35
|
+
const patch = getPatch();
|
|
24
36
|
if (!Object.keys(patch).length)
|
|
25
37
|
return;
|
|
26
38
|
serverData.value = await ofetch(fetch.fullUrl.value, { method: 'PATCH', body: patch });
|
|
@@ -29,12 +41,13 @@ export function useEditFetch(url, options = {}) {
|
|
|
29
41
|
// TODO: add if-unmodified-since header ?
|
|
30
42
|
serverData.value = await ofetch(fetch.fullUrl.value, { method: 'PUT', body: data.value });
|
|
31
43
|
}
|
|
32
|
-
serverData.value = data.value;
|
|
44
|
+
serverData.value = clone(data.value);
|
|
33
45
|
}, options.saveOptions);
|
|
34
46
|
return {
|
|
35
47
|
fetch,
|
|
36
48
|
data,
|
|
37
49
|
serverData,
|
|
50
|
+
hasDiff,
|
|
38
51
|
save
|
|
39
52
|
};
|
|
40
53
|
}
|
package/package.json
CHANGED
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'],
|