]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(reactivity): fix tracking for readonly + reactive Map (#3604)
authorHcySunYang <HcySunYang@outlook.com>
Fri, 7 May 2021 22:49:11 +0000 (06:49 +0800)
committerGitHub <noreply@github.com>
Fri, 7 May 2021 22:49:11 +0000 (18:49 -0400)
fix #3602

packages/reactivity/__tests__/effect.spec.ts
packages/reactivity/src/collectionHandlers.ts

index 34a0976030ac32032ae74ca2f377758de99f9f51..589c69195bf53e9d96f61be27d6e9dc75f956087 100644 (file)
@@ -7,7 +7,8 @@ import {
   TriggerOpTypes,
   DebuggerEvent,
   markRaw,
-  shallowReactive
+  shallowReactive,
+  readonly
 } from '../src/index'
 import { ITERATE_KEY } from '../src/effect'
 
@@ -832,4 +833,32 @@ describe('reactivity/effect', () => {
     observed2.obj2 = obj2
     expect(fnSpy2).toHaveBeenCalledTimes(1)
   })
+
+  describe('readonly + reactive for Map', () => {
+    test('should work with readonly(reactive(Map))', () => {
+      const m = reactive(new Map())
+      const roM = readonly(m)
+      const fnSpy = jest.fn(() => roM.get(1))
+
+      effect(fnSpy)
+      expect(fnSpy).toHaveBeenCalledTimes(1)
+      m.set(1, 1)
+      expect(fnSpy).toHaveBeenCalledTimes(2)
+    })
+
+    test('should work with observed value as key', () => {
+      const key = reactive({})
+      const m = reactive(new Map())
+      m.set(key, 1)
+      const roM = readonly(m)
+      const fnSpy = jest.fn(() => roM.get(key))
+
+      effect(fnSpy)
+      expect(fnSpy).toHaveBeenCalledTimes(1)
+      m.set(key, 1)
+      expect(fnSpy).toHaveBeenCalledTimes(1)
+      m.set(key, 2)
+      expect(fnSpy).toHaveBeenCalledTimes(2)
+    })
+  })
 })
index 4eb57bf6c67f194566fbf758045bf8fcc54e975d..a0ca8d080d418ebc9fda26e3ab578d60105fad6c 100644 (file)
@@ -49,6 +49,10 @@ function get(
     return wrap(target.get(key))
   } else if (has.call(rawTarget, rawKey)) {
     return wrap(target.get(rawKey))
+  } else if (target !== rawTarget) {
+    // #3602 readonly(reactive(Map))
+    // ensure that the nested reactive `Map` can do tracking for itself
+    target.get(key)
   }
 }