handle appview polling errors
Fixes APP-T4QA
This commit is contained in:
@@ -549,11 +549,6 @@
|
|||||||
"count": 2
|
"count": 2
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"src/lib/async/until.ts": {
|
|
||||||
"typescript/no-explicit-any": {
|
|
||||||
"count": 2
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"src/lib/broadcast/stub.ts": {
|
"src/lib/broadcast/stub.ts": {
|
||||||
"typescript/no-explicit-any": {
|
"typescript/no-explicit-any": {
|
||||||
"count": 1
|
"count": 1
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import {describe, expect, it, jest} from '@jest/globals'
|
||||||
|
|
||||||
|
import {until} from './until'
|
||||||
|
|
||||||
|
describe('until', () => {
|
||||||
|
it('does not invoke the condition when an attempt rejects', async () => {
|
||||||
|
const fn = jest
|
||||||
|
.fn<() => Promise<string>>()
|
||||||
|
.mockRejectedValueOnce(new Error('failed'))
|
||||||
|
.mockResolvedValue('ready')
|
||||||
|
const cond = jest.fn((value: string) => value === 'ready')
|
||||||
|
|
||||||
|
await expect(until(2, 0, cond, fn)).resolves.toBe(true)
|
||||||
|
expect(fn).toHaveBeenCalledTimes(2)
|
||||||
|
expect(cond).toHaveBeenCalledTimes(1)
|
||||||
|
expect(cond).toHaveBeenCalledWith('ready')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns false when every attempt rejects', async () => {
|
||||||
|
const fn = jest
|
||||||
|
.fn<() => Promise<string>>()
|
||||||
|
.mockRejectedValue(new Error('failed'))
|
||||||
|
const cond = jest.fn((_value: string) => true)
|
||||||
|
|
||||||
|
await expect(until(2, 0, cond, fn)).resolves.toBe(false)
|
||||||
|
expect(fn).toHaveBeenCalledTimes(2)
|
||||||
|
expect(cond).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -3,22 +3,21 @@ import {timeout} from './timeout'
|
|||||||
export async function until<T>(
|
export async function until<T>(
|
||||||
retries: number,
|
retries: number,
|
||||||
delay: number,
|
delay: number,
|
||||||
cond: (v: T, err: any) => boolean,
|
cond: (v: T) => boolean,
|
||||||
fn: () => Promise<T>,
|
fn: () => Promise<T>,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
while (retries > 0) {
|
while (retries > 0) {
|
||||||
|
let v: T
|
||||||
try {
|
try {
|
||||||
const v = await fn()
|
v = await fn()
|
||||||
if (cond(v, undefined)) {
|
} catch {
|
||||||
return true
|
await timeout(delay)
|
||||||
|
retries--
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
if (cond(v)) {
|
||||||
// TODO: change the type signature of cond to accept undefined
|
|
||||||
// however this breaks every existing usage of until -sfn
|
|
||||||
if (cond(undefined as unknown as T, e)) {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
|
||||||
await timeout(delay)
|
await timeout(delay)
|
||||||
retries--
|
retries--
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user