mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
* refactor: consolidate duplicate utility functions - Add escapeRegExp to src/utils.ts and remove 10 local duplicates - Rename bash-tools clampNumber to clampWithDefault (different signature) - Centralize formatError calls to use formatErrorMessage from infra/errors.ts - Re-export formatErrorMessage from cli/cli-utils.ts to preserve API * refactor: consolidate remaining escapeRegExp duplicates * refactor: consolidate sleep, stripAnsi, and clamp duplicates
26 lines
576 B
TypeScript
26 lines
576 B
TypeScript
import { sleep } from "../../src/utils.js";
|
|
|
|
export type PollOptions = {
|
|
timeoutMs?: number;
|
|
intervalMs?: number;
|
|
};
|
|
|
|
export async function pollUntil<T>(
|
|
fn: () => Promise<T | null | undefined>,
|
|
opts: PollOptions = {},
|
|
): Promise<T | undefined> {
|
|
const timeoutMs = opts.timeoutMs ?? 2000;
|
|
const intervalMs = opts.intervalMs ?? 25;
|
|
const start = Date.now();
|
|
|
|
while (Date.now() - start < timeoutMs) {
|
|
const value = await fn();
|
|
if (value !== null && value !== undefined) {
|
|
return value;
|
|
}
|
|
await sleep(intervalMs);
|
|
}
|
|
|
|
return undefined;
|
|
}
|