fix(reactivity): Map.set should trigger when adding new entry with undefined value (#364)

This commit is contained in:
Cr 2019-10-23 23:00:55 +08:00 committed by Evan You
parent 09c248ae87
commit 246cad7459
3 changed files with 32 additions and 28 deletions

View File

@ -185,21 +185,24 @@ describe('reactivity/collections', () => {
expect(dummy).toBe(undefined) expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(1) expect(mapSpy).toHaveBeenCalledTimes(1)
map.set('key', 'value') map.set('key', undefined)
expect(dummy).toBe('value') expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(2) expect(mapSpy).toHaveBeenCalledTimes(2)
map.set('key', 'value') map.set('key', 'value')
expect(dummy).toBe('value') expect(dummy).toBe('value')
expect(mapSpy).toHaveBeenCalledTimes(2) expect(mapSpy).toHaveBeenCalledTimes(3)
map.delete('key') map.set('key', 'value')
expect(dummy).toBe(undefined) expect(dummy).toBe('value')
expect(mapSpy).toHaveBeenCalledTimes(3) expect(mapSpy).toHaveBeenCalledTimes(3)
map.delete('key') map.delete('key')
expect(dummy).toBe(undefined) expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(3) expect(mapSpy).toHaveBeenCalledTimes(4)
map.delete('key')
expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(4)
map.clear() map.clear()
expect(dummy).toBe(undefined) expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(3) expect(mapSpy).toHaveBeenCalledTimes(4)
}) })
it('should not observe raw data', () => { it('should not observe raw data', () => {

View File

@ -46,18 +46,21 @@ describe('reactivity/collections', () => {
expect(dummy).toBe(undefined) expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(1) expect(mapSpy).toHaveBeenCalledTimes(1)
map.set(key, 'value') map.set(key, undefined)
expect(dummy).toBe('value') expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(2) expect(mapSpy).toHaveBeenCalledTimes(2)
map.set(key, 'value') map.set(key, 'value')
expect(dummy).toBe('value') expect(dummy).toBe('value')
expect(mapSpy).toHaveBeenCalledTimes(2) expect(mapSpy).toHaveBeenCalledTimes(3)
map.delete(key) map.set(key, 'value')
expect(dummy).toBe(undefined) expect(dummy).toBe('value')
expect(mapSpy).toHaveBeenCalledTimes(3) expect(mapSpy).toHaveBeenCalledTimes(3)
map.delete(key) map.delete(key)
expect(dummy).toBe(undefined) expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(3) expect(mapSpy).toHaveBeenCalledTimes(4)
map.delete(key)
expect(dummy).toBe(undefined)
expect(mapSpy).toHaveBeenCalledTimes(4)
}) })
it('should not observe raw data', () => { it('should not observe raw data', () => {

View File

@ -68,21 +68,19 @@ function set(this: MapTypes, key: unknown, value: unknown) {
const hadKey = proto.has.call(target, key) const hadKey = proto.has.call(target, key)
const oldValue = proto.get.call(target, key) const oldValue = proto.get.call(target, key)
const result = proto.set.call(target, key, value) const result = proto.set.call(target, key, value)
if (value !== oldValue) { /* istanbul ignore else */
/* istanbul ignore else */ if (__DEV__) {
if (__DEV__) { const extraInfo = { oldValue, newValue: value }
const extraInfo = { oldValue, newValue: value } if (!hadKey) {
if (!hadKey) { trigger(target, OperationTypes.ADD, key, extraInfo)
trigger(target, OperationTypes.ADD, key, extraInfo) } else if (value !== oldValue) {
} else { trigger(target, OperationTypes.SET, key, extraInfo)
trigger(target, OperationTypes.SET, key, extraInfo) }
} } else {
} else { if (!hadKey) {
if (!hadKey) { trigger(target, OperationTypes.ADD, key)
trigger(target, OperationTypes.ADD, key) } else if (value !== oldValue) {
} else { trigger(target, OperationTypes.SET, key)
trigger(target, OperationTypes.SET, key)
}
} }
} }
return result return result