diff --git a/packages/lib/src/Tools.ts b/packages/lib/src/Tools.ts index 955931a..9de59a4 100644 --- a/packages/lib/src/Tools.ts +++ b/packages/lib/src/Tools.ts @@ -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 + */ +export const poll = ({interval = 300, condition, signal}: PollOptions) => + new Promise(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 diff --git a/watch.sh b/watch.sh index d6d2306..7f5aaff 100755 --- a/watch.sh +++ b/watch.sh @@ -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"