Skip to content

Commit

Permalink
fix(reactivity): should delete observe value (vuejs#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Jan 9, 2020
1 parent 8f616a8 commit 6368613
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
17 changes: 16 additions & 1 deletion packages/reactivity/__tests__/collections/Map.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive, effect, toRaw, isReactive } from '../../src'
import { effect, isReactive, reactive, toRaw } from '../../src'

describe('reactivity/collections', () => {
describe('Map', () => {
Expand Down Expand Up @@ -26,6 +26,21 @@ describe('reactivity/collections', () => {
expect(dummy).toBe(undefined)
})

it('should observe mutations with observed value', () => {
let dummy
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
15 changes: 14 additions & 1 deletion packages/reactivity/__tests__/collections/Set.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive, effect, isReactive, toRaw } from '../../src'
import { effect, isReactive, reactive, toRaw } from '../../src'

describe('reactivity/collections', () => {
describe('Set', () => {
Expand All @@ -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
18 changes: 17 additions & 1 deletion packages/reactivity/__tests__/collections/WeakMap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive, effect, toRaw, isReactive } from '../../src'
import { effect, isReactive, reactive, toRaw } from '../../src'

describe('reactivity/collections', () => {
describe('WeakMap', () => {
Expand Down Expand Up @@ -27,6 +27,22 @@ describe('reactivity/collections', () => {
expect(dummy).toBe(undefined)
})

it('should observe mutations with observed value', () => {
let dummy
const key = {}
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
15 changes: 14 additions & 1 deletion packages/reactivity/__tests__/collections/WeakSet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive, isReactive, effect, toRaw } from '../../src'
import { effect, isReactive, reactive, toRaw } from '../../src'

describe('reactivity/collections', () => {
describe('WeakSet', () => {
Expand All @@ -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
7 changes: 4 additions & 3 deletions packages/reactivity/src/collectionHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { toRaw, reactive, readonly } from './reactive'
import { track, trigger, ITERATE_KEY } from './effect'
import { reactive, readonly, toRaw } from './reactive'
import { ITERATE_KEY, track, trigger } from './effect'
import { TrackOpTypes, TriggerOpTypes } from './operations'
import { LOCKED } from './lock'
import { isObject, capitalize, hasOwn, hasChanged } from '@vue/shared'
import { capitalize, hasChanged, hasOwn, isObject } from '@vue/shared'

export type CollectionTypes = IterableCollections | WeakCollections

Expand Down Expand Up @@ -87,6 +87,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 6368613

Please sign in to comment.