Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/poll/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# @repo/poll

Generic async polling helper.

```ts
import { poll } from '@repo/poll';

const result = await poll(() => checkJobStatus(jobId), {
maxAttempts: 20,
delayMs: (attempt) => Math.min(1000 * 2 ** attempt, 30_000),
until: (status) => status === 'done',
});
```

## Options

- `maxAttempts` — max number of calls to the polled function before `poll` rejects with `PollMaxAttemptsError`.
- `delayMs` — wait between attempts, either a fixed number or `(attempt: number) => number`, `attempt` being the 1-based iteration that just ran.
- `until` — predicate over the latest result; once it returns `true`, `poll` resolves with that result.
- `stopEmitter` — optional. Called once with a `stop` callback. Invoking `stop()` cancels any pending delay, runs the polled function one last time, and resolves with that result regardless of `until`.
- `onTick` — optional. Called with `(result, attempt)` after every attempt, including the one that satisfies `until` or is exhausted by `maxAttempts`.
4 changes: 4 additions & 0 deletions packages/poll/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { config } from "@repo/eslint-config/base";

/** @type {import("eslint").Linter.Config} */
export default config;
34 changes: 34 additions & 0 deletions packages/poll/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@repo/poll",
"version": "0.0.0",
"type": "module",
"files": [
"dist"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"license": "MIT",
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"check-types": "tsc --noEmit",
"lint": "eslint src --max-warnings 0",
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage --coverage.reporter=json-summary --coverage.reporter=text"
},
"devDependencies": {
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/node": "^26.0.1",
"@vitest/coverage-v8": "^4.1.9",
"eslint": "^9.39.1",
"typescript": "6.0.3",
"vite-tsconfig-paths": "^6.1.1",
"vitest": "^4.1.9"
}
}
Loading
Loading