@mastra/core
Version:
77 lines • 2.94 kB
TypeScript
import type { Event, SubscribeBatchOptions } from '../types.js';
/**
* Opaque timer handle. We don't care whether the runtime returns a number
* (browser) or a `Timeout` (Node) — we only ever hand it back to
* `clearTimeout`. Branding it keeps the type honest (you can't pass an
* arbitrary number) while staying erasure-free at runtime.
*/
declare const batchPolicyTimerHandleBrand: unique symbol;
export type BatchPolicyTimerHandle = {
readonly [batchPolicyTimerHandleBrand]: true;
};
/**
* Injectable dependencies for `BatchPolicy`. Tests pass fake timers /
* controllable clocks; production uses Node's `Date.now` / `setTimeout` /
* `clearTimeout`.
*/
export interface BatchPolicyDeps {
now: () => number;
setTimeout: (cb: () => void, ms: number) => BatchPolicyTimerHandle;
clearTimeout: (handle: BatchPolicyTimerHandle) => void;
}
export type EnqueueDecision = 'flush-now' | 'wait';
export declare const DEFAULT_MAX_BUFFER_SIZE = 256;
export declare const DEFAULT_OVERFLOW: NonNullable<SubscribeBatchOptions['overflow']>;
/**
* Internal to `EventEmitterPubSub`. Embedded by `AckHandleBuffer` to decide
* when a batched subscription should flush (size, time, coalesce, overflow).
*
* Not part of the public API — users configure batching via
* `SubscribeBatchOptions` on `subscribe`.
*/
export declare class BatchPolicy {
private readonly opts;
private readonly deps;
private readonly maxBufferSize;
private readonly overflow;
private firstQueuedAt;
private lastDeliveredAt;
private size;
private timer;
private flushHandler;
constructor(opts: SubscribeBatchOptions, deps?: BatchPolicyDeps);
/** Bind the function invoked when the deadline timer fires. */
bindFlushHandler(fn: () => void | Promise<void>): void;
/**
* Called by the integrator each time an event is enqueued.
* Returns whether the integrator should flush immediately.
*/
onEnqueue(event: Event): EnqueueDecision;
/**
* Called by the integrator after a successful flush has delivered
* `deliveredCount` events. Resets timer + firstQueuedAt.
*/
onFlushed(deliveredCount: number): void;
/**
* Pure helper. Given the caller-owned queue contents, applies `coalesce`
* and `overflow` to decide what to deliver and what to drop.
* Order-preserving for kept events.
*/
prepareBatch(events: Event[]): {
delivered: Event[];
dropped: Event[];
};
/** Stop the timer and clear policy state. */
dispose(): void;
private scheduleDeadline;
private scheduleAt;
private cancelTimer;
/**
* Drop `count` non-immediate items from the start (or end) of `items`.
* Immediate items are never dropped — if every candidate is immediate,
* fewer than `count` items are dropped.
*/
private takeWithoutDropping;
}
export {};
//# sourceMappingURL=batch-policy.d.ts.map