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): should delete observe value (#597) #598

Merged
merged 2 commits into from
Jan 13, 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
16 changes: 16 additions & 0 deletions packages/reactivity/__tests__/collections/Map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
13 changes: 13 additions & 0 deletions packages/reactivity/__tests__/collections/Set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>)
Expand Down
16 changes: 16 additions & 0 deletions packages/reactivity/__tests__/collections/WeakMap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
13 changes: 13 additions & 0 deletions packages/reactivity/__tests__/collections/WeakSet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions packages/reactivity/src/collectionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down