Skip to content

Commit

Permalink
fix(computedAsync): keep evaluating true for concurrent changes (#1555)
Browse files Browse the repository at this point in the history
  • Loading branch information
bodograumann committed May 3, 2022
1 parent a4238a8 commit 23549c3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
55 changes: 55 additions & 0 deletions packages/core/computedAsync/index.test.ts
Expand Up @@ -93,6 +93,61 @@ describe('computedAsync', () => {
expect(double.value).toBe(4)
})

it('uses last result', async() => {
const evaluating = ref(false)
const resolutions: Array<() => void> = []

const counter = ref(1)
const double = computedAsync(() => {
const result = counter.value * 2
return new Promise(resolve => (resolutions.push(() => resolve(result))))
}, undefined, evaluating)

await nextTick()

expect(double.value).toBeUndefined()
expect(evaluating.value).toBe(true)
expect(resolutions).toHaveLength(1)

resolutions[0]()
await nextTick()
await nextTick()

expect(double.value).toBe(2)
expect(evaluating.value).toBe(false)

counter.value = 2
await nextTick()
counter.value = 3
await nextTick()
counter.value = 4
await nextTick()

expect(evaluating.value).toBe(true)
expect(resolutions).toHaveLength(4)

resolutions[1]()
await nextTick()
await nextTick()

expect(evaluating.value).toBe(true)
expect(double.value).toBe(2)

resolutions[3]()
await nextTick()
await nextTick()

expect(evaluating.value).toBe(false)
expect(double.value).toBe(8)

resolutions[2]()
await nextTick()
await nextTick()

expect(evaluating.value).toBe(false)
expect(double.value).toBe(8)
})

test('evaluating works', async() => {
const evaluating = ref(false)

Expand Down
2 changes: 1 addition & 1 deletion packages/core/computedAsync/index.ts
Expand Up @@ -97,7 +97,7 @@ export function computedAsync<T>(
onError(e)
}
finally {
if (evaluating)
if (evaluating && counterAtBeginning === counter)
evaluating.value = false

hasFinished = true
Expand Down

0 comments on commit 23549c3

Please sign in to comment.