Skip to content

Commit 1a6c337

Browse files
committed
fix(reactivity): improve dev mode computed dependency tracking
Refine the dev mode dependency checking logic to prevent unnecessary recomputations while maintaining correctness. Replace version lag sync approach with direct version equality check for cleaner, more reliable dependency change detection.
1 parent 6c356a8 commit 1a6c337

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

packages/reactivity/src/effect.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,6 @@ function cleanupDeps(sub: Subscriber) {
327327
// The new head is the last node seen which wasn't removed
328328
// from the doubly-linked list
329329
head = link
330-
// Sync link version to maintain consistency for future dirty checks
331-
link.version = link.dep.version
332330
}
333331

334332
// restore previous active link if any
@@ -380,32 +378,31 @@ export function refreshComputed(computed: ComputedRefImpl): undefined {
380378
}
381379
computed.globalVersion = globalVersion
382380

383-
// Enhanced dependency check for development mode to ensure consistent behavior
384-
// with production mode by avoiding unnecessary recomputations due to version lag
381+
// In development mode, perform enhanced dependency tracking to prevent
382+
// unnecessary recomputations while preserving correct reactivity behavior
385383
if (__DEV__ && computed.flags & EffectFlags.EVALUATED && computed.deps) {
386-
let actuallyDirty = false
384+
let hasActualChanges = false
387385
let link: Link | undefined = computed.deps
386+
388387
while (link) {
389-
// Refresh nested computed dependencies first
388+
// Always refresh nested computed dependencies first
390389
if (link.dep.computed && link.dep.computed !== computed) {
391390
refreshComputed(link.dep.computed)
392391
}
393392

394-
// Conservative dirty check: only consider dirty if version difference > 1
395-
// This accounts for the cleanup lag and prevents false positives
396-
const versionDiff = link.dep.version - link.version
397-
if (versionDiff > 1) {
398-
actuallyDirty = true
393+
// Check if this dependency actually changed
394+
// Only skip recomputation if ALL dependencies are unchanged
395+
if (link.dep.version !== link.version) {
396+
hasActualChanges = true
399397
break
400-
} else if (versionDiff === 1) {
401-
// Sync version to prevent accumulating lag
402-
link.version = link.dep.version
403398
}
404399

405400
link = link.nextDep
406401
}
407402

408-
if (!actuallyDirty) {
403+
// If no dependencies actually changed, we can safely skip recomputation
404+
// This prevents the dev mode lag issue while preserving correctness
405+
if (!hasActualChanges) {
409406
return
410407
}
411408
}

0 commit comments

Comments
 (0)