From 8b355ada17973634eeaa93c7333c8adf24071ee9 Mon Sep 17 00:00:00 2001 From: daiwei Date: Fri, 23 Aug 2024 15:37:19 +0800 Subject: [PATCH 1/8] fix(reactivity): handle a ref directly nested in reactive --- packages/reactivity/__tests__/reactive.spec.ts | 8 ++++++++ packages/reactivity/src/baseHandlers.ts | 3 +++ 2 files changed, 11 insertions(+) diff --git a/packages/reactivity/__tests__/reactive.spec.ts b/packages/reactivity/__tests__/reactive.spec.ts index 1c25c49121b..1fb9857480b 100644 --- a/packages/reactivity/__tests__/reactive.spec.ts +++ b/packages/reactivity/__tests__/reactive.spec.ts @@ -314,6 +314,14 @@ describe('reactivity/reactive', () => { expect(isReactive(observed)).toBe(false) }) + test('a ref nested in a reactive', () => { + const obj = reactive(ref(1)) + const spy1 = vi.fn(() => obj.value) + effect(spy1) + obj.value = 2 + expect(isReactive(obj)).toBe(true) + }) + test('hasOwnProperty edge case: Symbol values', () => { const key = Symbol() const obj = reactive({ [key]: 1 }) as { [key]?: 1 } diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 8f21e354002..90f8dd1bc5a 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -82,6 +82,9 @@ class BaseReactiveHandler implements ProxyHandler { return } + // only track its value if target is a ref + if (isRef(target) && key !== 'value') return (target as any)[key] + const targetIsArray = isArray(target) if (!isReadonly) { From c03ec198bcb73056acc965ab1fbea68311e581cd Mon Sep 17 00:00:00 2001 From: daiwei Date: Fri, 23 Aug 2024 15:38:50 +0800 Subject: [PATCH 2/8] Revert "fix(reactivity): handle a ref directly nested in reactive" This reverts commit 8b355ada17973634eeaa93c7333c8adf24071ee9. --- packages/reactivity/__tests__/reactive.spec.ts | 8 -------- packages/reactivity/src/baseHandlers.ts | 3 --- 2 files changed, 11 deletions(-) diff --git a/packages/reactivity/__tests__/reactive.spec.ts b/packages/reactivity/__tests__/reactive.spec.ts index 1fb9857480b..1c25c49121b 100644 --- a/packages/reactivity/__tests__/reactive.spec.ts +++ b/packages/reactivity/__tests__/reactive.spec.ts @@ -314,14 +314,6 @@ describe('reactivity/reactive', () => { expect(isReactive(observed)).toBe(false) }) - test('a ref nested in a reactive', () => { - const obj = reactive(ref(1)) - const spy1 = vi.fn(() => obj.value) - effect(spy1) - obj.value = 2 - expect(isReactive(obj)).toBe(true) - }) - test('hasOwnProperty edge case: Symbol values', () => { const key = Symbol() const obj = reactive({ [key]: 1 }) as { [key]?: 1 } diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 90f8dd1bc5a..8f21e354002 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -82,9 +82,6 @@ class BaseReactiveHandler implements ProxyHandler { return } - // only track its value if target is a ref - if (isRef(target) && key !== 'value') return (target as any)[key] - const targetIsArray = isArray(target) if (!isReadonly) { From fc31326354c779370a08e94b23044d8fe285d06b Mon Sep 17 00:00:00 2001 From: zhangenming <282126346@qq.com> Date: Fri, 23 Aug 2024 15:49:31 +0800 Subject: [PATCH 3/8] Update dep.ts --- packages/reactivity/src/dep.ts | 60 ++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/packages/reactivity/src/dep.ts b/packages/reactivity/src/dep.ts index 4ce73ac9954..e2884ef18b5 100644 --- a/packages/reactivity/src/dep.ts +++ b/packages/reactivity/src/dep.ts @@ -211,6 +211,9 @@ export function track(target: object, type: TrackOpTypes, key: unknown): void { if (!depsMap) { targetMap.set(target, (depsMap = new Map())) } + + // if (depsMap.get(ITERATE_KEY) && key !== ITERATE_KEY) return + let dep = depsMap.get(key) if (!dep) { depsMap.set(key, (dep = new Dep())) @@ -250,11 +253,29 @@ export function trigger( return } - let deps: Dep[] = [] + const run = (dep: Dep | undefined) => { + if (!dep) return + + if (__DEV__) { + dep.trigger({ + target, + type, + key, + newValue, + oldValue, + oldTarget, + }) + } else { + dep.trigger() + } + } + + startBatch() + if (type === TriggerOpTypes.CLEAR) { // collection being cleared // trigger all effects for target - deps = [...depsMap.values()] + depsMap.forEach(run) } else { const targetIsArray = isArray(target) const isArrayIndex = targetIsArray && isIntegerKey(key) @@ -267,67 +288,50 @@ export function trigger( key === ARRAY_ITERATE_KEY || (!isSymbol(key) && key >= newLength) ) { - deps.push(dep) + run(dep) } }) } else { - const push = (dep: Dep | undefined) => dep && deps.push(dep) - // schedule runs for SET | ADD | DELETE if (key !== void 0) { - push(depsMap.get(key)) + run(depsMap.get(key)) } // schedule ARRAY_ITERATE for any numeric key change (length is handled above) if (isArrayIndex) { - push(depsMap.get(ARRAY_ITERATE_KEY)) + run(depsMap.get(ARRAY_ITERATE_KEY)) } // also run for iteration key on ADD | DELETE | Map.SET switch (type) { case TriggerOpTypes.ADD: if (!targetIsArray) { - push(depsMap.get(ITERATE_KEY)) + run(depsMap.get(ITERATE_KEY)) if (isMap(target)) { - push(depsMap.get(MAP_KEY_ITERATE_KEY)) + run(depsMap.get(MAP_KEY_ITERATE_KEY)) } } else if (isArrayIndex) { // new index added to array -> length changes - push(depsMap.get('length')) + run(depsMap.get('length')) } break case TriggerOpTypes.DELETE: if (!targetIsArray) { - push(depsMap.get(ITERATE_KEY)) + run(depsMap.get(ITERATE_KEY)) if (isMap(target)) { - push(depsMap.get(MAP_KEY_ITERATE_KEY)) + run(depsMap.get(MAP_KEY_ITERATE_KEY)) } } break case TriggerOpTypes.SET: if (isMap(target)) { - push(depsMap.get(ITERATE_KEY)) + run(depsMap.get(ITERATE_KEY)) } break } } } - startBatch() - for (const dep of deps) { - if (__DEV__) { - dep.trigger({ - target, - type, - key, - newValue, - oldValue, - oldTarget, - }) - } else { - dep.trigger() - } - } endBatch() } From ea95b7f67b9e3e4cf0cbf335bc173424ca28d36a Mon Sep 17 00:00:00 2001 From: zhangenming <282126346@qq.com> Date: Fri, 23 Aug 2024 15:55:22 +0800 Subject: [PATCH 4/8] Update dep.ts --- packages/reactivity/src/dep.ts | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/packages/reactivity/src/dep.ts b/packages/reactivity/src/dep.ts index e2884ef18b5..d8eb9e1aae4 100644 --- a/packages/reactivity/src/dep.ts +++ b/packages/reactivity/src/dep.ts @@ -212,8 +212,6 @@ export function track(target: object, type: TrackOpTypes, key: unknown): void { targetMap.set(target, (depsMap = new Map())) } - // if (depsMap.get(ITERATE_KEY) && key !== ITERATE_KEY) return - let dep = depsMap.get(key) if (!dep) { depsMap.set(key, (dep = new Dep())) @@ -254,20 +252,14 @@ export function trigger( } const run = (dep: Dep | undefined) => { - if (!dep) return - - if (__DEV__) { - dep.trigger({ - target, - type, - key, - newValue, - oldValue, - oldTarget, - }) - } else { - dep.trigger() - } + dep && dep.trigger(__DEV__ && { + target, + type, + key, + newValue, + oldValue, + oldTarget, + }) } startBatch() From 91960f84343874b60d65d933688cf9e7dac107cb Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Fri, 23 Aug 2024 07:56:16 +0000 Subject: [PATCH 5/8] [autofix.ci] apply automated fixes --- packages/reactivity/src/dep.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/reactivity/src/dep.ts b/packages/reactivity/src/dep.ts index d8eb9e1aae4..17a78888e10 100644 --- a/packages/reactivity/src/dep.ts +++ b/packages/reactivity/src/dep.ts @@ -252,14 +252,17 @@ export function trigger( } const run = (dep: Dep | undefined) => { - dep && dep.trigger(__DEV__ && { - target, - type, - key, - newValue, - oldValue, - oldTarget, - }) + dep && + dep.trigger( + __DEV__ && { + target, + type, + key, + newValue, + oldValue, + oldTarget, + }, + ) } startBatch() From c135fe4dd874e7cf09b6e7b3f28156a4fa52cc49 Mon Sep 17 00:00:00 2001 From: zhangenming <282126346@qq.com> Date: Fri, 23 Aug 2024 16:01:26 +0800 Subject: [PATCH 6/8] Update dep.ts --- packages/reactivity/src/dep.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/reactivity/src/dep.ts b/packages/reactivity/src/dep.ts index 17a78888e10..ea822c91ff2 100644 --- a/packages/reactivity/src/dep.ts +++ b/packages/reactivity/src/dep.ts @@ -211,7 +211,6 @@ export function track(target: object, type: TrackOpTypes, key: unknown): void { if (!depsMap) { targetMap.set(target, (depsMap = new Map())) } - let dep = depsMap.get(key) if (!dep) { depsMap.set(key, (dep = new Dep())) @@ -254,14 +253,14 @@ export function trigger( const run = (dep: Dep | undefined) => { dep && dep.trigger( - __DEV__ && { + __DEV__ ? { target, type, key, newValue, oldValue, oldTarget, - }, + } : undefined, ) } From 547248f593c828bf20072cad096b6e525f8124d3 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Fri, 23 Aug 2024 08:02:18 +0000 Subject: [PATCH 7/8] [autofix.ci] apply automated fixes --- packages/reactivity/src/dep.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/reactivity/src/dep.ts b/packages/reactivity/src/dep.ts index ea822c91ff2..12edb566b9c 100644 --- a/packages/reactivity/src/dep.ts +++ b/packages/reactivity/src/dep.ts @@ -253,14 +253,16 @@ export function trigger( const run = (dep: Dep | undefined) => { dep && dep.trigger( - __DEV__ ? { - target, - type, - key, - newValue, - oldValue, - oldTarget, - } : undefined, + __DEV__ + ? { + target, + type, + key, + newValue, + oldValue, + oldTarget, + } + : undefined, ) } From 8eda1cc3ecbd20a265dc5b4cfd3d2438c91fb01c Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 10 Sep 2024 15:46:45 +0800 Subject: [PATCH 8/8] Update dep.ts --- packages/reactivity/src/dep.ts | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/reactivity/src/dep.ts b/packages/reactivity/src/dep.ts index 12edb566b9c..c56a951ac78 100644 --- a/packages/reactivity/src/dep.ts +++ b/packages/reactivity/src/dep.ts @@ -251,19 +251,20 @@ export function trigger( } const run = (dep: Dep | undefined) => { - dep && - dep.trigger( - __DEV__ - ? { - target, - type, - key, - newValue, - oldValue, - oldTarget, - } - : undefined, - ) + if (dep) { + if (__DEV__) { + dep.trigger({ + target, + type, + key, + newValue, + oldValue, + oldTarget, + }) + } else { + dep.trigger() + } + } } startBatch()