Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | 13x 13x 13x 13x 13x 14x 14x 14x 14x 14x 14x 14x 43x 3x 40x 40x 1x 1x 1x 39x 39x 39x 37x 12x 12x 9x 9x 2x 2x 11x 12x 25x 25x 2x 2x 2x 14x 14x 13x 13x 13x 13x 14x 14x 3x 3x 14x 14x 4x 4x 3x 1x 3x 3x 14x | import type { Logger } from '../../logger/definition.ts'; import { OFF_LOGGER } from '../../logger/off-logger.ts'; import { withPrefix } from '../../logger/prefixed-logger.ts'; import { createDeferredValue } from './deferred-value.ts'; import { delay } from './delay.ts'; /** * Configuration options for polling operations. * * @template T The type of value that the polling operation returns * @template V The type of value to return when a timeout occurs */ export type PollingOptions<T, V> = { /** * Whether to invoke the operation immediately or instead wait for the first interval. */ readonly invokeImmediately?: boolean; /** * Maximum time in milliseconds to poll before auto-stopping. */ readonly timeout?: number; /** * Value to return when a timeout occurs. */ readonly timeoutValue?: V; /** * Maximum number of times to call the operation before auto-stopping. */ readonly retryLimit?: number; /** * Function that evaluates each result and returns true if polling should stop. * * @param result The result returned by the operation * @param invocationIndex The current invocation count (0-based) * @returns `true` to stop polling, `false` to continue */ readonly interrupt?: (result: T, invocationIndex: number) => boolean; /** * Logger to capture polling events and errors. */ readonly logger?: Logger; }; /** * Polls a function at regular intervals until a condition is met, a timeout occurs, maximum retries are reached, or * it's manually stopped. Returns both a method to stop polling and a promise that resolves with the last result. * * The polling operation handles both synchronous and asynchronous (Promise-returning) functions. If the operation * throws an error or rejects, polling continues but the error is logged. If a previous asynchronous operation is still * resolving when the next interval occurs, that polling iteration is skipped. * * @example Simple polling at regular intervals * * ```ts * // Poll every 5 seconds until manually stopped * const closeable = startPolling(() => fetchLatestData(), 5_000); * * // Stop polling after 30 seconds * await delay(30_000).then(close); * * // Get the final result * const finalData = await wait; * ``` * * @example Basic polling until a condition is met * * ```ts * const { wait, close } = startPolling(() => fetchStatus(), 1000, { * interrupt: (status) => status === 'completed', * }); * * // Later get the final result * const finalStatus = await wait; * ``` * * @example Polling with timeout * * ```ts * const { wait } = startPolling(() => checkJobStatus(jobId), 2000, { * timeout: 30000, // Stop after 30 seconds * interrupt: (status) => ['completed', 'failed'].includes(status), * logger: console, * }); * * const finalStatus = await wait; * if (finalStatus === 'completed') { * // Job finished successfully * } else { * // Either timed out or job failed * } * ``` * * @example Polling with maximum retries * * ```ts * const { wait } = startPolling(() => checkJobStatus(jobId), 2000, { * retryLimit: 5, // Stop after 5 attempts * logger: console, * }); * * const finalStatus = await wait; * ``` * * @example Manual control of polling * * ```ts * const poll = startPolling(() => fetchDataPoints(), 5000); * * // Stop polling after some external event * eventEmitter.on('stop-polling', () => { * poll.close(); * }); * * // Get the last result when polling stops * const lastDataPoints = await poll.wait; * ``` * * @param operation Function to execute on each poll interval. Can return a value or a Promise. * @param interval Time in milliseconds between poll attempts * @param options Optional configuration for polling behavior * @returns An object with a `close()` method to manually stop polling and a `wait` Promise that resolves with the last * result when polling stops */ export const startPolling = <T, const V = undefined>( operation: () => T | Promise<T>, interval: number, options?: PollingOptions<T, V>, ): { readonly wait: Promise<T | V | undefined>; readonly close: () => Promise<void> } => { const deferred = createDeferredValue<T | V | undefined>(); interval = Math.max(0, Math.ceil(interval)); let resolving = false; let invocationIndex = -1; let active = true; let lastResult: T | V | undefined; const logger = withPrefix(options?.logger ?? OFF_LOGGER, 'poll', { fallbackPrefix: 'emitnlog' }); const polledOperation = (): void => { if (resolving || !active) { return; } invocationIndex++; // Check if we've reached the maximum number of retries if (options?.retryLimit !== undefined && invocationIndex >= options.retryLimit) { logger.d`reached maximum retries (${options.retryLimit})`; void close(); return; } try { logger.d`invoking the operation for the ${invocationIndex + 1} time`; const result = operation(); if (result instanceof Promise) { resolving = true; void result .then((value) => { lastResult = value; if (options?.interrupt && options.interrupt(value, invocationIndex)) { void close(); } }) .catch((error: unknown) => { logger.args(error).e`the operation rejected with an error: ${error}`; }) .finally(() => { resolving = false; }); return; } lastResult = result; if (options?.interrupt && options.interrupt(result, invocationIndex)) { void close(); return; } } catch (error) { logger.args(error).e`the operation threw an error: ${error}`; } }; const close = async (): Promise<void> => { if (active) { active = false; logger.d`closing the poll after ${invocationIndex + 1} invocations`; clearInterval(intervalId); deferred.resolve(lastResult); } await deferred.promise; }; if (options?.invokeImmediately) { polledOperation(); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition Iif (!active) { return { close, wait: deferred.promise }; } } const intervalId = setInterval(polledOperation, interval); if (options?.timeout && options.timeout >= 0) { void delay(options.timeout).then(() => { if (active) { if ('timeoutValue' in options) { lastResult = options.timeoutValue; } logger.d`timeout for the operation reached after ${options.timeout}ms`; void close(); } }); } return { close, wait: deferred.promise }; }; |