Skip to content

Commit dce8954

Browse files
edison1105camc314
authored andcommitted
fix(reactivity): should not recompute if computed does not track reactive data (vuejs#12341)
close vuejs#12337
1 parent e8ecfc9 commit dce8954

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

packages/reactivity/__tests__/computed.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,17 @@ describe('reactivity/computed', () => {
10171017
expect(cValue.value).toBe(1)
10181018
})
10191019

1020+
test('should not recompute if computed does not track reactive data', async () => {
1021+
const spy = vi.fn()
1022+
const c1 = computed(() => spy())
1023+
1024+
c1.value
1025+
ref(0).value++ // update globalVersion
1026+
c1.value
1027+
1028+
expect(spy).toBeCalledTimes(1)
1029+
})
1030+
10201031
test('computed should remain live after losing all subscribers', () => {
10211032
const state = reactive({ a: 1 })
10221033
const p = computed(() => state.a + 1)

packages/reactivity/src/effect.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ export enum EffectFlags {
4646
/**
4747
* ReactiveEffect only
4848
*/
49-
ALLOW_RECURSE = 1 << 7,
50-
PAUSED = 1 << 8,
51-
NOTIFIED = 1 << 9,
49+
ACTIVE = 1 << 0,
50+
RUNNING = 1 << 1,
51+
TRACKING = 1 << 2,
52+
NOTIFIED = 1 << 3,
53+
DIRTY = 1 << 4,
54+
ALLOW_RECURSE = 1 << 5,
55+
PAUSED = 1 << 6,
56+
EVALUATED = 1 << 7,
5257
STOP = 1 << 10,
5358
}
5459

@@ -160,6 +165,11 @@ export class ReactiveEffect<T = any> implements ReactiveEffectOptions {
160165
) {
161166
return true
162167
}
168+
// @ts-expect-error only for backwards compatibility where libs manually set
169+
// this flag - e.g. Pinia's testing module
170+
if ((this as any)._dirty) {
171+
return true
172+
}
163173
return false
164174
}
165175
}
@@ -168,11 +178,6 @@ if (__DEV__) {
168178
setupOnTrigger(ReactiveEffect)
169179
}
170180

171-
export interface ReactiveEffectRunner<T = any> {
172-
(): T
173-
effect: ReactiveEffect
174-
}
175-
176181
export function effect<T = any>(
177182
fn: () => T,
178183
options?: ReactiveEffectOptions,

0 commit comments

Comments
 (0)