import type { Logger } from './logger'; /** Processor that synchronously transforms a single chunk into an output value. */ export type SyncProcessor = (chunk: InputChunk) => OutputChunk; /** * Processor that may return either a value (sync) or a Promise (async) for any * given chunk. The executor detects which by duck-typing the return value. */ export type MixedProcessor = (chunk: InputChunk) => OutputChunk | Promise; /** * Pair of processors describing a sync-transform-then-async-flush pipeline. * * `syncTransform` runs on each input chunk; intermediates accumulate into a batch. * `asyncPostProcess` receives the accumulated batch and may return any length of * output array (so it can filter, expand, or pass through). */ export type TwoPhaseProcessor = { syncTransform: (chunk: InputChunk) => IntermediateChunk; asyncPostProcess: (intermediates: IntermediateChunk[]) => Promise; }; /** Options shared across all chunked executors. */ export type ExecutionOptions = { /** Identifier surfaced in warning messages so you can attribute slowness to a call site. */ id: string; /** * Upper bound (in milliseconds) on a single synchronous burst before the executor * yields to the event loop via `setImmediate`. Set to `0` to yield after every * chunk. Must be a finite, non-negative number. Defaults to `15`. */ executeSynchronouslyThresholdInMsecs?: number; /** * When a synchronous burst exceeds this threshold (in milliseconds), the executor * emits one warning per burst describing the slowdown. Set to `0` to disable. * Must be a finite, non-negative number. Defaults to `30`. */ warningThresholdInMsecs?: number; /** Logger to receive warnings. Defaults to `defaultLogger` (which calls `console.warn`). */ logger?: Logger; }; /** Default values applied to {@link ExecutionOptions} fields that are left undefined. */ export declare const defaultExecutionOptions: { readonly warningThresholdInMsecs: 30; readonly executeSynchronouslyThresholdInMsecs: 15; readonly logger: Logger; }; /** * Executes chunks through a synchronous processor, yielding to the event loop via * `setImmediate` whenever a burst of work exceeds `executeSynchronouslyThresholdInMsecs`. * * Use this for CPU-bound transforms (parsing, validation, stemming) that would * otherwise block the event loop for tens or hundreds of milliseconds. Results * are returned in input order. */ export declare function executeSyncChunksSequentially(inputChunks: readonly InputChunk[], processor: SyncProcessor, options: ExecutionOptions): Promise; /** * Executes chunks through a processor that may return either a value or a Promise. * * Only the synchronous part of each call (the time until `processor` returns) counts * towards the burst thresholds: time spent waiting on a returned Promise does not * block the event loop, so it is neither accumulated nor reported in warnings. Once * the accumulated synchronous time reaches `executeSynchronouslyThresholdInMsecs` * the executor yields via `setImmediate`, regardless of whether the last chunk was * sync or async. This means a processor that returns already-settled promises * (`Promise.resolve(x)`, a cache wrapper) still yields correctly; the executor never * assumes that awaiting a Promise gave the event loop a turn. */ export declare function executeMixedChunksSequentially(inputChunks: readonly InputChunk[], processor: MixedProcessor, options: ExecutionOptions): Promise; /** * Executes chunks in two explicit phases: a synchronous transform that accumulates * intermediates into a batch, followed by an async post-processing step (e.g. a bulk * database insert). Sync transforms run back-to-back until * `executeSynchronouslyThresholdInMsecs` is exceeded or the last chunk is reached, * then the accumulated batch is flushed to `asyncPostProcess`. Each non-final flush * yields the event loop and resets the burst counters, so an `asyncPostProcess` that * resolves synchronously still lets timers and I/O run between batches. * * Setting `executeSynchronouslyThresholdInMsecs: 0` flushes after every sync transform * — useful when downstream ordering or backpressure dictates one-at-a-time processing. * Output order is preserved across batches (each `asyncPostProcess` call is awaited * before the next sync transform begins). * * `asyncPostProcess` may return an array of any length, allowing filtering or * expansion during post-processing. Resolving to anything other than an array is a * `TypeError` (the most common cause is a forgotten `return`). */ export declare function executeTwoPhaseChunksSequentially(inputChunks: readonly InputChunk[], processor: TwoPhaseProcessor, options: ExecutionOptions): Promise;