From 63a656310676e3927b2e57d813fd6300c0a42590 Mon Sep 17 00:00:00 2001 From: likui <2218301630@qq.com> Date: Tue, 14 Jan 2020 06:11:49 +0800 Subject: [PATCH] fix(reactivity): should delete observe value (#598) fix #597 --- .../reactivity/__tests__/collections/Map.spec.ts | 16 ++++++++++++++++ .../reactivity/__tests__/collections/Set.spec.ts | 13 +++++++++++++ .../__tests__/collections/WeakMap.spec.ts | 16 ++++++++++++++++ .../__tests__/collections/WeakSet.spec.ts | 13 +++++++++++++ packages/reactivity/src/collectionHandlers.ts | 2 ++ 5 files changed, 60 insertions(+) diff --git a/packages/reactivity/__tests__/collections/Map.spec.ts b/packages/reactivity/__tests__/collections/Map.spec.ts index b190da32..4cfb9e6b 100644 --- a/packages/reactivity/__tests__/collections/Map.spec.ts +++ b/packages/reactivity/__tests__/collections/Map.spec.ts @@ -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()) diff --git a/packages/reactivity/__tests__/collections/Set.spec.ts b/packages/reactivity/__tests__/collections/Set.spec.ts index f01090d2..9e3efa16 100644 --- a/packages/reactivity/__tests__/collections/Set.spec.ts +++ b/packages/reactivity/__tests__/collections/Set.spec.ts @@ -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) diff --git a/packages/reactivity/__tests__/collections/WeakMap.spec.ts b/packages/reactivity/__tests__/collections/WeakMap.spec.ts index 6c82b82d..e3dde72e 100644 --- a/packages/reactivity/__tests__/collections/WeakMap.spec.ts +++ b/packages/reactivity/__tests__/collections/WeakMap.spec.ts @@ -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()) diff --git a/packages/reactivity/__tests__/collections/WeakSet.spec.ts b/packages/reactivity/__tests__/collections/WeakSet.spec.ts index 2427de12..182298cf 100644 --- a/packages/reactivity/__tests__/collections/WeakSet.spec.ts +++ b/packages/reactivity/__tests__/collections/WeakSet.spec.ts @@ -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()) diff --git a/packages/reactivity/src/collectionHandlers.ts b/packages/reactivity/src/collectionHandlers.ts index b5289844..cb90cf0e 100644 --- a/packages/reactivity/src/collectionHandlers.ts +++ b/packages/reactivity/src/collectionHandlers.ts @@ -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) @@ -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)