Skip to content

Commit 1784ac2

Browse files
chore: Document and reexport types (#50)
1 parent 1b47076 commit 1784ac2

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

lib/asyncbox.ts

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import B from 'bluebird';
22
import _ from 'lodash';
3-
import type {LongSleepOptions, WaitForConditionOptions} from './types.js';
3+
import type {
4+
LongSleepOptions,
5+
WaitForConditionOptions,
6+
} from './types.js';
47

58
const LONG_SLEEP_THRESHOLD = 5000; // anything over 5000ms will turn into a spin
69

710
/**
811
* An async/await version of setTimeout
12+
* @param ms - The number of milliseconds to wait
913
*/
1014
export async function sleep(ms: number): Promise<void> {
1115
return await B.delay(ms);
@@ -20,10 +24,16 @@ export async function sleep(ms: number): Promise<void> {
2024
* receives an object with the properties `elapsedMs`, `timeLeft`, and
2125
* `progress`. This will be called on every wait interval so you can do your
2226
* wait logging or whatever.
27+
* @param ms - The number of milliseconds to wait
28+
* @param options - Options for controlling the long sleep behavior
2329
*/
2430
export async function longSleep(
2531
ms: number,
26-
{thresholdMs = LONG_SLEEP_THRESHOLD, intervalMs = 1000, progressCb = null}: LongSleepOptions = {},
32+
{
33+
thresholdMs = LONG_SLEEP_THRESHOLD,
34+
intervalMs = 1000,
35+
progressCb = null,
36+
}: LongSleepOptions = {},
2737
): Promise<void> {
2838
if (ms < thresholdMs) {
2939
return await sleep(ms);
@@ -45,6 +55,9 @@ export async function longSleep(
4555

4656
/**
4757
* An async/await way of running a method until it doesn't throw an error
58+
* @param times - The maximum number of times to retry the function
59+
* @param fn - The async function to retry
60+
* @param args - Arguments to pass to the function
4861
*/
4962
export async function retry<T = any>(
5063
times: number,
@@ -71,6 +84,10 @@ export async function retry<T = any>(
7184
/**
7285
* You can also use `retryInterval` to add a sleep in between retries. This can
7386
* be useful if you want to throttle how fast we retry.
87+
* @param times - The maximum number of times to retry the function
88+
* @param sleepMs - The number of milliseconds to wait between retries
89+
* @param fn - The async function to retry
90+
* @param args - Arguments to pass to the function
7491
*/
7592
export async function retryInterval<T = any>(
7693
times: number,
@@ -101,6 +118,8 @@ export const parallel = B.all;
101118
/**
102119
* Export async functions (Promises) and import this with your ES5 code to use
103120
* it with Node.
121+
* @param promisey - A Promise or promise-like value to convert to a callback
122+
* @param cb - The callback function to call with the result or error
104123
*/
105124
// eslint-disable-next-line promise/prefer-await-to-callbacks
106125
export function nodeify<R = any>(promisey: any, cb: (err: any, value?: R) => void): Promise<R> {
@@ -109,6 +128,7 @@ export function nodeify<R = any>(promisey: any, cb: (err: any, value?: R) => voi
109128

110129
/**
111130
* Node-ify an entire object of `Promise`-returning functions
131+
* @param promiseyMap - An object containing functions that return Promises
112132
*/
113133
export function nodeifyAll<T extends Record<string, (...args: any[]) => any>>(
114134
promiseyMap: T,
@@ -126,13 +146,18 @@ export function nodeifyAll<T extends Record<string, (...args: any[]) => any>>(
126146

127147
/**
128148
* Fire and forget async function execution
149+
* @param fn - The function to execute asynchronously
150+
* @param args - Arguments to pass to the function
129151
*/
130152
export function asyncify(fn: (...args: any[]) => any | Promise<any>, ...args: any[]): void {
131153
B.resolve(fn(...args)).done();
132154
}
133155

134156
/**
135157
* Similar to `Array.prototype.map`; runs in serial or parallel
158+
* @param coll - The collection to map over
159+
* @param mapper - The function to apply to each element
160+
* @param runInParallel - Whether to run operations in parallel (default: true)
136161
*/
137162
export async function asyncmap<T, R>(
138163
coll: T[],
@@ -152,6 +177,9 @@ export async function asyncmap<T, R>(
152177

153178
/**
154179
* Similar to `Array.prototype.filter`
180+
* @param coll - The collection to filter
181+
* @param filter - The function to test each element
182+
* @param runInParallel - Whether to run operations in parallel (default: true)
155183
*/
156184
export async function asyncfilter<T>(
157185
coll: T[],
@@ -188,6 +216,8 @@ export async function asyncfilter<T>(
188216
* error then this exception will be immediately passed through.
189217
*
190218
* The default options are: `{ waitMs: 5000, intervalMs: 500 }`
219+
* @param condFn - The condition function to evaluate
220+
* @param options - Options for controlling the wait behavior
191221
*/
192222
export async function waitForCondition<T>(
193223
condFn: () => Promise<T> | T,
@@ -215,11 +245,18 @@ export async function waitForCondition<T>(
215245
return await spin();
216246
}
217247
// if there is an error option, it is either a string message or an error itself
218-
throw error
219-
? _.isString(error)
220-
? new Error(error)
221-
: error
222-
: new Error(`Condition unmet after ${waited} ms. Timing out.`);
248+
if (error) {
249+
throw _.isString(error) ? new Error(error) : error;
250+
}
251+
throw new Error(`Condition unmet after ${waited} ms. Timing out.`);
223252
};
224253
return await spin();
225254
}
255+
256+
// Re-export types
257+
export type {
258+
Progress,
259+
ProgressCallback,
260+
LongSleepOptions,
261+
WaitForConditionOptions,
262+
} from './types.js';

lib/types.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
* Parameter provided to a progress callback
33
*/
44
export interface Progress {
5+
/** Elapsed time in milliseconds since the operation started */
56
elapsedMs: number;
7+
/** Remaining time in milliseconds until the operation completes */
68
timeLeft: number;
9+
/** Progress as a number between 0 and 1 (0 = not started, 1 = complete) */
710
progress: number;
811
}
912

@@ -16,19 +19,36 @@ export type ProgressCallback = (progress: Progress) => void;
1619
* Options for {@link longSleep}
1720
*/
1821
export interface LongSleepOptions {
22+
/** Minimum duration in milliseconds before using long sleep behavior.
23+
* @default 5000
24+
*/
1925
thresholdMs?: number;
26+
/** Interval in milliseconds between progress callbacks.
27+
* @default 1000
28+
*/
2029
intervalMs?: number;
30+
/** Optional callback function to receive progress updates during the sleep */
2131
progressCb?: ProgressCallback | null;
2232
}
2333

2434
/**
2535
* Options for {@link waitForCondition}
2636
*/
2737
export interface WaitForConditionOptions {
38+
/** Maximum time to wait in milliseconds.
39+
* @default 5000
40+
*/
2841
waitMs?: number;
42+
/** Interval in milliseconds between condition checks.
43+
* @default 500
44+
*/
2945
intervalMs?: number;
46+
/** Optional logger object with a debug method for logging wait progress */
3047
logger?: {
3148
debug: (...args: any[]) => void;
3249
};
50+
/** Custom error message (string) or Error object to throw if condition is not met.
51+
* If not provided, a default error is thrown
52+
*/
3353
error?: string | Error;
3454
}

0 commit comments

Comments
 (0)