Skip to content

Commit 24680f2

Browse files
committed
fix: Check new condition and pre-condition is equivalent before fetch data.
1 parent 7d39066 commit 24680f2

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/useConditionWatcher.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { reactive, ref, watch, inject, onMounted, onUnmounted } from 'vue-demi'
22
import { ConditionsType, Config, QueryOptions, ResultInterface } from './types'
3-
import { filterNoneValueObject, createParams, stringifyQuery, syncQuery2Conditions } from './utils'
3+
import { filterNoneValueObject, createParams, stringifyQuery, syncQuery2Conditions, isEquivalent } from './utils'
44
import { useFetchData } from './useFetchData'
55
import { useParseQuery } from './useParseQuery'
66
import clone from 'rfdc'
@@ -81,7 +81,8 @@ export default function useConditionWatcher<T extends Config, E extends QueryOpt
8181
// initial conditions by window.location.search. just do once.
8282
syncConditionsByQuery()
8383
// watch query changed
84-
watch(query, async () => {
84+
watch(query, async (newQuery, prevQuery) => {
85+
if (isEquivalent(newQuery, prevQuery)) return
8586
const path: string = router.currentRoute.value ? router.currentRoute.value.path : router.currentRoute.path
8687
const queryString = stringifyQuery(query.value, queryOptions.ignore || [])
8788
await router.push(path + '?' + queryString).catch((e) => e)

src/utils.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,42 @@ export function syncQuery2Conditions(conditions: ConditionsType, query: Conditio
7171
}
7272
})
7373
}
74+
75+
export function isEquivalent(x, y) {
76+
if (x === null || x === undefined || y === null || y === undefined) {
77+
return x === y
78+
}
79+
if (x.constructor !== y.constructor) {
80+
return false
81+
}
82+
if (x instanceof Function) {
83+
return x === y
84+
}
85+
if (x instanceof RegExp) {
86+
return x === y
87+
}
88+
if (x === y || x.valueOf() === y.valueOf()) {
89+
return true
90+
}
91+
if (Array.isArray(x) && x.length !== y.length) {
92+
return false
93+
}
94+
if (x instanceof Date) {
95+
return false
96+
}
97+
if (!(x instanceof Object)) {
98+
return false
99+
}
100+
if (!(y instanceof Object)) {
101+
return false
102+
}
103+
let p = Object.keys(x)
104+
return (
105+
Object.keys(y).every((i) => {
106+
return p.indexOf(i) !== -1
107+
}) &&
108+
p.every((i) => {
109+
return isEquivalent(x[i], y[i])
110+
})
111+
)
112+
}

test/utils.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
import { filterNoneValueObject, createParams, stringifyQuery, syncQuery2Conditions } from '../src/utils'
1+
import { filterNoneValueObject, createParams, stringifyQuery, syncQuery2Conditions, isEquivalent } from '../src/utils'
2+
3+
describe('utils: isEquivalent', () => {
4+
it(`Check Object Equality`, () => {
5+
const current = {
6+
name: '',
7+
tags: [],
8+
phone: undefined,
9+
address: null,
10+
data: new Date(),
11+
}
12+
const old = {
13+
name: '',
14+
tags: [],
15+
phone: undefined,
16+
address: null,
17+
data: new Date(),
18+
}
19+
expect(isEquivalent(current, old)).toBeTruthy()
20+
})
21+
})
222

323
describe('utils: filterNoneValueObject', () => {
424
it(`Should be return empty object`, () => {

0 commit comments

Comments
 (0)