Skip to content

Commit

Permalink
fix: urgent assessment edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Apr 5, 2023
1 parent fc84803 commit 88d91db
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,31 @@ describe('reactivity/computed', () => {
expect(c1Spy).toHaveBeenCalledTimes(104)
expect(c2Spy).toHaveBeenCalledTimes(2)
})

it('chained computed value urgent assessment edge case', () => {
const cSpy = vi.fn()

const a = ref<null | { v: number }>({
v: 1
})
const b = computed(() => {
return a.value
})
const c = computed(() => {
cSpy()
return b.value?.v
})
const d = computed(() => {
if (b.value) {
return c.value
}
return 0
})

d.value
a.value!.v = 2
a.value = null
d.value
expect(cSpy).toHaveBeenCalledTimes(1)
})
})
7 changes: 7 additions & 0 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export class ComputedRefImpl<T> {
const self = toRaw(this)
if (!self._dirty && self._computedsToAskDirty.length) {
pauseTracking()
if (self._computedsToAskDirty.length >= 2) {
self._computedsToAskDirty = self._computedsToAskDirty.sort((a, b) => {
const aIndex = self.effect.deps.indexOf(a.dep!)
const bIndex = self.effect.deps.indexOf(b.dep!)
return aIndex - bIndex
})
}
for (const computedToAskDirty of self._computedsToAskDirty) {
computedToAskDirty.value
if (self._dirty) {
Expand Down

0 comments on commit 88d91db

Please sign in to comment.