handle appview polling errors

Fixes APP-T4QA
This commit is contained in:
Samuel Newman
2026-08-28 13:57:33 +01:00
parent e08bcd0228
commit 7de7c2cb72
3 changed files with 39 additions and 16 deletions
-5
View File
@@ -549,11 +549,6 @@
"count": 2
}
},
"src/lib/async/until.ts": {
"typescript/no-explicit-any": {
"count": 2
}
},
"src/lib/broadcast/stub.ts": {
"typescript/no-explicit-any": {
"count": 1
+29
View File
@@ -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()
})
})
+10 -11
View File
@@ -3,21 +3,20 @@ import {timeout} from './timeout'
export async function until<T>(
retries: number,
delay: number,
cond: (v: T, err: any) => boolean,
cond: (v: T) => boolean,
fn: () => Promise<T>,
): Promise<boolean> {
while (retries > 0) {
let v: T
try {
const v = await fn()
if (cond(v, undefined)) {
return true
}
} catch (e: any) {
// 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
}
v = await fn()
} catch {
await timeout(delay)
retries--
continue
}
if (cond(v)) {
return true
}
await timeout(delay)
retries--