Skip to content

Commit eb0f8fa

Browse files
johnsoncodehkyyx990803
authored andcommitted
fix: urgent assessment edge case
1 parent 014dcd5 commit eb0f8fa

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

packages/reactivity/__tests__/computed.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,31 @@ describe('reactivity/computed', () => {
341341
expect(c1Spy).toHaveBeenCalledTimes(104)
342342
expect(c2Spy).toHaveBeenCalledTimes(2)
343343
})
344+
345+
it('chained computed value urgent assessment edge case', () => {
346+
const cSpy = vi.fn()
347+
348+
const a = ref<null | { v: number }>({
349+
v: 1
350+
})
351+
const b = computed(() => {
352+
return a.value
353+
})
354+
const c = computed(() => {
355+
cSpy()
356+
return b.value?.v
357+
})
358+
const d = computed(() => {
359+
if (b.value) {
360+
return c.value
361+
}
362+
return 0
363+
})
364+
365+
d.value
366+
a.value!.v = 2
367+
a.value = null
368+
d.value
369+
expect(cSpy).toHaveBeenCalledTimes(1)
370+
})
344371
})

packages/reactivity/src/computed.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ export class ComputedRefImpl<T> {
7272
const self = toRaw(this)
7373
if (!self._dirty && self._computedsToAskDirty.length) {
7474
pauseTracking()
75+
if (self._computedsToAskDirty.length >= 2) {
76+
self._computedsToAskDirty = self._computedsToAskDirty.sort((a, b) => {
77+
const aIndex = self.effect.deps.indexOf(a.dep!)
78+
const bIndex = self.effect.deps.indexOf(b.dep!)
79+
return aIndex - bIndex
80+
})
81+
}
7582
for (const computedToAskDirty of self._computedsToAskDirty) {
7683
computedToAskDirty.value
7784
if (self._dirty) {

0 commit comments

Comments
 (0)