-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reactivity): add Vue.delete workaround (#571)
- Loading branch information
Showing
7 changed files
with
113 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { | ||
del, | ||
isReactive, | ||
isRef, | ||
isRaw, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { getVueConstructor } from '../runtimeContext' | ||
import { hasOwn, isPrimitive, isUndef, isValidArrayIndex } from '../utils' | ||
|
||
/** | ||
* Delete a property and trigger change if necessary. | ||
*/ | ||
export function del(target: any, key: any) { | ||
const Vue = getVueConstructor() | ||
const { warn } = Vue.util | ||
|
||
if (__DEV__ && (isUndef(target) || isPrimitive(target))) { | ||
warn( | ||
`Cannot delete reactive property on undefined, null, or primitive value: ${target}` | ||
) | ||
} | ||
if (Array.isArray(target) && isValidArrayIndex(key)) { | ||
target.splice(key, 1) | ||
return | ||
} | ||
const ob = target.__ob__ | ||
if (target._isVue || (ob && ob.vmCount)) { | ||
__DEV__ && | ||
warn( | ||
'Avoid deleting properties on a Vue instance or its root $data ' + | ||
'- just set it to null.' | ||
) | ||
return | ||
} | ||
if (!hasOwn(target, key)) { | ||
return | ||
} | ||
delete target[key] | ||
if (!ob) { | ||
return | ||
} | ||
ob.dep.notify() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ export { | |
ShallowUnwrapRef, | ||
} from './ref' | ||
export { set } from './set' | ||
export { del } from './del' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { del, reactive, ref, watch } from '../../../src' | ||
|
||
// Vue.delete workaround for triggering view updates on object property/array index deletion | ||
describe('reactivity/del', () => { | ||
it('should not trigger reactivity on native object member deletion', () => { | ||
const obj = reactive<{ a?: object }>({ | ||
a: {}, | ||
}) | ||
const spy = jest.fn() | ||
watch(obj, spy, { deep: true, flush: 'sync' }) | ||
delete obj.a // Vue 2 limitation | ||
expect(spy).not.toHaveBeenCalled() | ||
expect(obj).toStrictEqual({}) | ||
}) | ||
|
||
it('should trigger reactivity when using del on reactive object', () => { | ||
const obj = reactive<{ a?: object }>({ | ||
a: {}, | ||
}) | ||
const spy = jest.fn() | ||
watch(obj, spy, { deep: true, flush: 'sync' }) | ||
del(obj, 'a') | ||
expect(spy).toBeCalledTimes(1) | ||
expect(obj).toStrictEqual({}) | ||
}) | ||
|
||
it('should not remove element on array index and should not trigger reactivity', () => { | ||
const arr = ref([1, 2, 3]) | ||
const spy = jest.fn() | ||
watch(arr, spy, { flush: 'sync' }) | ||
delete arr.value[1] // Vue 2 limitation; workaround with .splice() | ||
expect(spy).not.toHaveBeenCalled() | ||
expect(arr.value).toEqual([1, undefined, 3]) | ||
}) | ||
|
||
it('should trigger reactivity when using del on array', () => { | ||
const arr = ref([1, 2, 3]) | ||
const spy = jest.fn() | ||
watch(arr, spy, { flush: 'sync' }) | ||
del(arr.value, 1) | ||
expect(spy).toBeCalledTimes(1) | ||
expect(arr.value).toEqual([1, 3]) | ||
}) | ||
}) |