Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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