UNPKG

@message-in-the-middle/core

Version:

Framework-agnostic middleware pattern for message queue processing. Core package with all middlewares.

31 lines (30 loc) 1.17 kB
import { InboundMiddleware, MessageContext } from '../types'; export type BatchProcessor<T = any> = (contexts: MessageContext<T>[]) => Promise<void> | void; export interface BatchingOptions<T = any> { batchSize?: number; flushInterval?: number; processor: BatchProcessor<T>; batchKeyExtractor?: (context: MessageContext<T>) => string; flushOnDestroy?: boolean; onFlush?: (batchSize: number, key?: string) => void | Promise<void>; onError?: (error: Error, contexts: MessageContext<T>[]) => void | Promise<void>; } export declare class BatchingInboundMiddleware<T = any> implements InboundMiddleware<T> { private batches; private readonly DEFAULT_BATCH_KEY; private totalBatches; private totalMessages; private readonly options; constructor(options: BatchingOptions<T>); process(context: MessageContext<T>, next: () => Promise<void>): Promise<void>; private flushBatch; flush(): Promise<void>; getStats(): { totalBatches: number; totalMessages: number; averageBatchSize: number; pendingMessages: number; activeBatches: number; }; destroy(): Promise<void>; }