Skip to content

Commit

Permalink
fix(reactivity): ensure add/set on reactive collections return the pr…
Browse files Browse the repository at this point in the history
…oxy (#2534)

fix #2530
  • Loading branch information
LinusBorg authored Nov 27, 2020
1 parent 0ff2a4f commit 6e46a57
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/reactivity/__tests__/collections/Map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
6 changes: 6 additions & 0 deletions packages/reactivity/__tests__/collections/Set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
5 changes: 5 additions & 0 deletions packages/reactivity/__tests__/collections/WeakMap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
6 changes: 6 additions & 0 deletions packages/reactivity/__tests__/collections/WeakSet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
8 changes: 4 additions & 4 deletions packages/reactivity/src/collectionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 6e46a57

Please sign in to comment.