fix(reactivity): ensure add/set on reactive collections return the proxy (#2534)

fix #2530
This commit is contained in:
Thorsten Lünborg 2020-11-27 20:16:00 +01:00 committed by GitHub
parent 0ff2a4f1c1
commit 6e46a574ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 4 deletions

View File

@ -467,5 +467,11 @@ describe('reactivity/collections', () => {
proxy.clear()
expect(spy).toBeCalledTimes(1)
})
it('should return proxy from Map.set call', () => {
const map = reactive(new Map())
const result = map.set('a', 'a')
expect(result).toBe(map)
})
})
})

View File

@ -454,5 +454,11 @@ describe('reactivity/collections', () => {
proxy.clear()
expect(spy).toBeCalledTimes(1)
})
it('should return proxy from Set.add call', () => {
const set = reactive(new Set())
const result = set.add('a')
expect(result).toBe(set)
})
})
})

View File

@ -133,5 +133,10 @@ describe('reactivity/collections', () => {
map.set(key, NaN)
expect(mapSpy).toHaveBeenCalledTimes(1)
})
it('should return proxy from WeakMap.set call', () => {
const map = reactive(new WeakMap())
const result = map.set({}, 'a')
expect(result).toBe(map)
})
})
})

View File

@ -99,5 +99,11 @@ describe('reactivity/collections', () => {
expect(observed.has(value)).toBe(true)
expect(set.has(value)).toBe(false)
})
it('should return proxy from WeakSet.add call', () => {
const set = reactive(new WeakSet())
const result = set.add({})
expect(result).toBe(set)
})
})
})

View File

@ -76,11 +76,11 @@ function add(this: SetTypes, value: unknown) {
const target = toRaw(this)
const proto = getProto(target)
const hadKey = proto.has.call(target, value)
const result = target.add(value)
target.add(value)
if (!hadKey) {
trigger(target, TriggerOpTypes.ADD, value, value)
}
return result
return this
}
function set(this: MapTypes, key: unknown, value: unknown) {
@ -97,13 +97,13 @@ function set(this: MapTypes, key: unknown, value: unknown) {
}
const oldValue = get.call(target, key)
const result = target.set(key, value)
target.set(key, value)
if (!hadKey) {
trigger(target, TriggerOpTypes.ADD, key, value)
} else if (hasChanged(value, oldValue)) {
trigger(target, TriggerOpTypes.SET, key, value, oldValue)
}
return result
return this
}
function deleteEntry(this: CollectionTypes, key: unknown) {