fix(reactivity): should delete observe value (#598)

fix #597
This commit is contained in:
likui 2020-01-14 06:11:49 +08:00 committed by Evan You
parent 30aae0b4df
commit 63a6563106
5 changed files with 60 additions and 0 deletions

View File

@ -26,6 +26,22 @@ describe('reactivity/collections', () => {
expect(dummy).toBe(undefined)
})
it('should observe mutations with observed value as key', () => {
let dummy
const key = reactive({})
const value = reactive({})
const map = reactive(new Map())
effect(() => {
dummy = map.get(key)
})
expect(dummy).toBe(undefined)
map.set(key, value)
expect(dummy).toBe(value)
map.delete(key)
expect(dummy).toBe(undefined)
})
it('should observe size mutations', () => {
let dummy
const map = reactive(new Map())

View File

@ -22,6 +22,19 @@ describe('reactivity/collections', () => {
expect(dummy).toBe(false)
})
it('should observe mutations with observed value', () => {
let dummy
const value = reactive({})
const set = reactive(new Set())
effect(() => (dummy = set.has(value)))
expect(dummy).toBe(false)
set.add(value)
expect(dummy).toBe(true)
set.delete(value)
expect(dummy).toBe(false)
})
it('should observe for of iteration', () => {
let dummy
const set = reactive(new Set() as Set<number>)

View File

@ -27,6 +27,22 @@ describe('reactivity/collections', () => {
expect(dummy).toBe(undefined)
})
it('should observe mutations with observed value as key', () => {
let dummy
const key = reactive({})
const value = reactive({})
const map = reactive(new WeakMap())
effect(() => {
dummy = map.get(key)
})
expect(dummy).toBe(undefined)
map.set(key, value)
expect(dummy).toBe(value)
map.delete(key)
expect(dummy).toBe(undefined)
})
it('should not observe custom property mutations', () => {
let dummy
const map: any = reactive(new WeakMap())

View File

@ -23,6 +23,19 @@ describe('reactivity/collections', () => {
expect(dummy).toBe(false)
})
it('should observe mutations with observed value', () => {
let dummy
const value = reactive({})
const set = reactive(new WeakSet())
effect(() => (dummy = set.has(value)))
expect(dummy).toBe(false)
set.add(value)
expect(dummy).toBe(true)
set.delete(value)
expect(dummy).toBe(false)
})
it('should not observe custom property mutations', () => {
let dummy
const set: any = reactive(new WeakSet())

View File

@ -63,6 +63,7 @@ function add(this: SetTypes, value: unknown) {
function set(this: MapTypes, key: unknown, value: unknown) {
value = toRaw(value)
key = toRaw(key)
const target = toRaw(this)
const proto = getProto(target)
const hadKey = proto.has.call(target, key)
@ -87,6 +88,7 @@ function set(this: MapTypes, key: unknown, value: unknown) {
}
function deleteEntry(this: CollectionTypes, key: unknown) {
key = toRaw(key)
const target = toRaw(this)
const proto = getProto(target)
const hadKey = proto.has.call(target, key)