Add poll utility

This commit is contained in:
Jon Staab
2025-04-09 13:47:59 -07:00
parent 45b59c1a4c
commit 1bcc57d695
2 changed files with 27 additions and 1 deletions
+26
View File
@@ -890,6 +890,32 @@ export const randomId = (): string => Math.random().toString().slice(2)
*/
export const sleep = (t: number) => new Promise(resolve => setTimeout(resolve, t))
export type PollOptions = {
signal: AbortSignal
condition: () => boolean
interval?: number
}
/**
* Creates a promise that resolves after the condition completes or timeout
* @param options - PollOptions
* @returns Promise<void>
*/
export const poll = ({interval = 300, condition, signal}: PollOptions) =>
new Promise<void>(resolve => {
const int = setInterval(() => {
if (condition()) {
resolve()
clearInterval(int)
}
}, interval)
signal.addEventListener('abort', () => {
resolve()
clearInterval(int)
})
})
/**
* Creates a microtask that yields to other tasks in the event loop
* @returns Promise that resolves after yielding
+1 -1
View File
@@ -1,7 +1,7 @@
#!/bin/bash
for package in $(ls packages); do
npx onchange packages/$package -e '**/dist/**' -k -- pnpm run --filter $package build &
npx onchange packages/$package -e '**/dist/**' -e '**/*.tsbuildinfo' -k -- pnpm run --filter $package build &
done
echo "Watching for changes"