10000 fix(reactivity): should not recompute if computed does not track reactive data by edison1105 · Pull Request #12341 · vuejs/core · GitHub
[go: up one dir, main page]

Skip to content

fix(reactivity): should not recompute if computed does not track reactive data #12341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(reactivity): force trigger computed
  • Loading branch information
edison1105 committed Nov 7, 2024
commit 4a0385ebbc94b0e07b9fefb430942ffc1b254ca1
8 changes: 5 additions & 3 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export enum EffectFlags {
DIRTY = 1 << 4,
ALLOW_RECURSE = 1 << 5,
PAUSED = 1 << 6,
FORCE_TRIGGER = 1 << 7,
}

/**
Expand Down Expand Up @@ -364,7 +365,8 @@ function isDirty(sub: Subscriber): boolean {
export function refreshComputed(computed: ComputedRefImpl): undefined {
if (
computed.flags & EffectFlags.TRACKING &&
!(computed.flags & EffectFlags.DIRTY)
!(computed.flags & EffectFlags.DIRTY) &&
!(computed.flags & EffectFlags.FORCE_TRIGGER)
) {
return
}
Expand All @@ -386,13 +388,12 @@ export function refreshComputed(computed: ComputedRefImpl): undefined {
if (
dep.version > 0 &&
!computed.isSSR &&
computed.deps &&
!(computed.flags & EffectFlags.FORCE_TRIGGER) &&
!isDirty(computed)
) {
computed.flags &= ~EffectFlags.RUNNING
return
}

const prevSub = activeSub
const prevShouldTrack = shouldTrack
activeSub = computed
Expand All @@ -413,6 +414,7 @@ export function refreshComputed(computed: ComputedRefImpl): undefined {
shouldTrack = prevShouldTrack
cleanupDeps(computed)
computed.flags &= ~EffectFlags.RUNNING
computed.flags &= ~EffectFlags.FORCE_TRIGGER
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import type { ComputedRef, WritableComputedRef } from './computed'
import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants'
import { warn } from './warning'
import { EffectFlags } from './effect'

declare const RefSymbol: unique symbol
export declare const RawSymbol: unique symbol
Expand Down Expand Up @@ -186,6 +187,9 @@ class RefImpl<T = any> {
export function triggerRef(ref: Ref): void {
// ref may be an instance of ObjectRefImpl
if ((ref as unknown as RefImpl).dep) {
if ((ref as unknown as RefImpl).dep.computed) {
;(ref as any).flags |= EffectFlags.FORCE_TRIGGER
}
if (__DEV__) {
;(ref as unknown as RefImpl).dep.trigger({
target: ref,
Expand Down
0