// ██████╗ ██████╗ ██████╗ ██╗ ██╗███████╗███████╗██████╗ ███████╗ // ██╔══██╗██╔══██╗██╔═══██╗██║ ██║██╔════╝██╔════╝██╔══██╗██╔════╝ // ██████╔╝██████╔╝██║ ██║██║ █╗ ██║███████╗█████╗ ██████╔╝███████╗ // ██╔══██╗██╔══██╗██║ ██║██║███╗██║╚════██║██╔══╝ ██╔══██╗╚════██║ // ██████╔╝██║ ██║╚██████╔╝╚███╔███╔╝███████║███████╗██║ ██║███████║ // ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚══╝╚══╝ ╚══════╝╚══════╝╚═╝ ╚═╝╚══════╝ // MIT License (c) 2020–present Cezar Augusto & the Extension.js authors, presence implies inheritance type BoundedOutcome = | {state: 'resolved'; value: T} | {state: 'rejected'; error: unknown} | {state: 'timeout'} // Work awaited inside the compiler's done hook holds every later rebuild, so // the hook stops waiting at the bound. The work itself keeps running. export async function waitAtMost( work: Promise, ms: number ): Promise> { let timer: ReturnType | undefined const timeout = new Promise>((resolve) => { timer = setTimeout(() => resolve({state: 'timeout'}), ms) }) try { return await Promise.race([ work.then( (value): BoundedOutcome => ({state: 'resolved', value}), (error: unknown): BoundedOutcome => ({state: 'rejected', error}) ), timeout ]) } finally { clearTimeout(timer) } }