@js-data-tools/js-helpers
Version:
A set of JavaScript / TypeScript helper functions for parsing, converting, transforming and formatting data.
61 lines (60 loc) • 2.63 kB
TypeScript
/**
* @callback
*/
export type ProgressLogCallback = (duration: number, count: number, rate: number, completed: boolean) => void;
/**
* A helper class that can be used to monitor a progress of processing a stream of items,
* reporting it to the console (or provided callback)
*
* @since 0.1.2
* @category progress
*/
export declare class ProgressReporter {
startTime: number;
count: number;
duration: number;
private lastReportTime;
private reportEvery;
private log;
/**
* Constructs a new ProgressReporter
* @param {ProgressLogCallback} [logCallback] - The callback function for printing a progress log message
* @param {number} [reportPeriodMsec=1000] - The minimal amount of milliseconds to pass between two subsequent progress messages.
*/
constructor(logCallback?: ProgressLogCallback, reportPeriodMsec?: number);
/**
* Starts monitoring the progress of stream processing.
*/
start(): void;
/**
* Inform the progress monitor about another entry that was processed. It will probably trigger a log message if enough time was passed since the last message.
*/
entry(): void;
/**
* Stops monitoring the progress of stream processing. This method should be called when processing is completed.
*/
stop(): void;
/**
* Stops monitoring and reports the overall benchmarking. A shortcut for this.stop() and this.report(true)
*/
stopAndReport(): void;
/**
* Prints the current progress. Usually called internally
* @param [completed=false] true if processing is completed.
*/
report(completed?: boolean): void;
static logToConsole(duration: number, count: number, rate: number, completed: boolean): void;
static logToStdOut(duration: number, count: number, rate: number, completed: boolean): void;
static defaultLog: (duration: number, count: number, rate: number, completed: boolean) => void;
static formatMessage(duration: number, count: number, rate: number): string;
}
/**
* Wraps given async iterable with a progress monitor, reporting how many items were processed so far (once a second).
*
* @since 0.1.2
* @category progress
* @param source The iterable collection to monitor the progress of iteration (consumption) for.
* @param [report] Either the callback function for reporting a progress or an instance of ProgressReporter.
* @returns A new async iterable, monitoring the progress of the iteration.
*/
export declare function trackProgressAsync<T>(source: AsyncIterable<T>, report?: ProgressReporter | ProgressLogCallback): AsyncIterable<T>;