Skip to content

Commit

Permalink
fix(reactivity): should delete observe value (#598)
Browse files Browse the repository at this point in the history
fix #597
  • Loading branch information
underfin authored and yyx990803 committed Jan 13, 2020
1 parent 30aae0b commit 63a6563
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
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

0 comments on commit 63a6563

Please sign in to comment.