Skip to content

Commit cae3ea7

Browse files
committed
feat: remove rfdc
1 parent fde0246 commit cae3ea7

File tree

5 files changed

+59
-16
lines changed

5 files changed

+59
-16
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@
5353
]
5454
},
5555
"dependencies": {
56-
"vue-demi": "latest",
57-
"rfdc": "^1.1.4"
56+
"vue-demi": "latest"
5857
},
5958
"devDependencies": {
6059
"@types/jest": "24.0.20",

src/useConditionWatcher.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
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, isEquivalentString } from './utils'
3+
import {
4+
filterNoneValueObject,
5+
createParams,
6+
stringifyQuery,
7+
syncQuery2Conditions,
8+
isEquivalent,
9+
deepClone,
10+
} from './utils'
411
import { useFetchData } from './useFetchData'
512
import { useParseQuery } from './useParseQuery'
6-
import clone from 'rfdc'
713

814
export default function useConditionWatcher<T extends Config, E extends QueryOptions<E>>(
915
config: T,
1016
queryOptions?: E
1117
): ResultInterface {
1218
let router = null
13-
const backupIntiConditions = clone({ proto: true })(config.conditions)
19+
const backupIntiConditions = deepClone(config.conditions)
1420
const _conditions = reactive(config.conditions)
1521
const loading = ref(false)
1622
const data = ref(null)
@@ -45,7 +51,7 @@ export default function useConditionWatcher<T extends Config, E extends QueryOpt
4551
const conditionChangeHandler = (conditions) => {
4652
const conditions2Object: ConditionsType = conditions
4753
let customConditions: ConditionsType = {}
48-
const deepCopyCondition: ConditionsType = clone({ proto: true })(conditions2Object)
54+
const deepCopyCondition: ConditionsType = deepClone(conditions2Object)
4955

5056
if (typeof config.beforeFetch === 'function') {
5157
customConditions = config.beforeFetch(deepCopyCondition)
@@ -72,17 +78,20 @@ export default function useConditionWatcher<T extends Config, E extends QueryOpt
7278

7379
watch(
7480
() => ({ ..._conditions }),
75-
(conditions) => conditionChangeHandler(conditions)
81+
(nc, oc) => {
82+
if (completeInitialConditions.value && isEquivalent(nc, oc)) return
83+
conditionChangeHandler(nc)
84+
}
7685
)
7786

7887
if (queryOptions && typeof queryOptions.sync === 'string' && queryOptions.sync.length) {
7988
router = inject(queryOptions.sync)
8089
if (router) {
8190
// initial conditions by window.location.search. just do once.
8291
syncConditionsByQuery()
83-
// watch query changed
84-
watch(query, async (newQuery, prevQuery) => {
85-
if (isEquivalentString(newQuery, prevQuery, queryOptions.ignore || [])) return
92+
conditionChangeHandler({ ..._conditions })
93+
// watch query changed to push
94+
watch(query, async () => {
8695
const path: string = router.currentRoute.value ? router.currentRoute.value.path : router.currentRoute.path
8796
const queryString = stringifyQuery(query.value, queryOptions.ignore || [])
8897
await router.push(path + '?' + queryString).catch((e) => e)

src/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,21 @@ export function isEquivalent(x, y) {
114114
})
115115
)
116116
}
117+
118+
export function deepClone(obj) {
119+
if (obj === null) return null
120+
let clone = Object.assign({}, obj)
121+
Object.keys(clone).forEach((key) => {
122+
if (obj[key] instanceof Date) {
123+
const original = obj[key]
124+
clone[key] = new Date(original)
125+
return
126+
}
127+
clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]
128+
})
129+
return Array.isArray(obj) && obj.length
130+
? (clone.length = obj.length) && Array.from(clone)
131+
: Array.isArray(obj)
132+
? Array.from(obj)
133+
: clone
134+
}

test/utils.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { filterNoneValueObject, createParams, stringifyQuery, syncQuery2Conditions, isEquivalent } from '../src/utils'
1+
import {
2+
filterNoneValueObject,
3+
createParams,
4+
stringifyQuery,
5+
syncQuery2Conditions,
6+
isEquivalent,
7+
deepClone,
8+
} from '../src/utils'
29

310
describe('utils: isEquivalent', () => {
411
it(`Check Object Equality`, () => {
@@ -20,6 +27,21 @@ describe('utils: isEquivalent', () => {
2027
})
2128
})
2229

30+
describe('utils: deepClone', () => {
31+
it(`Check Object deepClone`, () => {
32+
const current = {
33+
name: '',
34+
tags: [],
35+
phone: undefined,
36+
address: null,
37+
data: new Date(),
38+
}
39+
const newObj = deepClone(current)
40+
expect(newObj !== current).toBeTruthy()
41+
expect(newObj.data !== current.data).toBeTruthy()
42+
})
43+
})
44+
2345
describe('utils: filterNoneValueObject', () => {
2446
it(`Should be return empty object`, () => {
2547
const conditions = {

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3798,11 +3798,6 @@ ret@~0.1.10:
37983798
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
37993799
integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==
38003800

3801-
rfdc@^1.1.4:
3802-
version "1.1.4"
3803-
resolved "https://registry.yarnpkg.com/rfdc/-/rfdc-1.1.4.tgz#ba72cc1367a0ccd9cf81a870b3b58bd3ad07f8c2"
3804-
integrity sha512-5C9HXdzK8EAqN7JDif30jqsBzavB7wLpaubisuQIGHWf2gUXSpzy6ArX/+Da8RjFpagWsCn+pIgxTMAmKw9Zug==
3805-
38063801
rimraf@2.6.3:
38073802
version "2.6.3"
38083803
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab"

0 commit comments

Comments
 (0)