fix(desktop): wait for first-run bootstrap before connection timeout
This commit is contained in:
@@ -28,6 +28,43 @@ export function isTimeoutError(error: unknown): error is TimeoutError {
|
||||
return error instanceof TimeoutError
|
||||
}
|
||||
|
||||
/** First-run downloads and the user's setup choice are outside the normal
|
||||
* backend cold-start budget. Keep one connection request alive while main
|
||||
* reports that work; a stalled IPC probe and ordinary boots remain bounded. */
|
||||
export async function withBootstrapAwareTimeout<T>(
|
||||
promise: Promise<T>,
|
||||
ms: number,
|
||||
message: string,
|
||||
bootstrapState?: () => Promise<{ active?: boolean; setupChoice?: unknown } | null>,
|
||||
cancelled: () => boolean = () => false
|
||||
): Promise<T> {
|
||||
let settled = false
|
||||
const request = promise.then(
|
||||
value => {
|
||||
settled = true
|
||||
return value
|
||||
},
|
||||
error => {
|
||||
settled = true
|
||||
throw error
|
||||
}
|
||||
)
|
||||
|
||||
for (;;) {
|
||||
try {
|
||||
return await withTimeout(request, ms, message)
|
||||
} catch (error) {
|
||||
if (settled || cancelled() || !isTimeoutError(error) || !bootstrapState) {
|
||||
throw error
|
||||
}
|
||||
const state = await withTimeout(bootstrapState(), Math.min(ms, 5_000), message)
|
||||
if (!state?.active && !state?.setupChoice) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Settle with `promise`, or reject with a TimeoutError after `ms`.
|
||||
* `onTimeout` runs synchronously before the rejection is published so callers
|
||||
* can revoke ownership of work that would otherwise keep running unowned. If
|
||||
|
||||
Reference in New Issue
Block a user