Skip to content

Commit

Permalink
fix(reactivity): force trigger computed
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Nov 7, 2024
1 parent 76c43c6 commit 4a0385e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
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 comments on commit 4a0385e

Please sign in to comment.