/** * Request Queue - Event Batching (PostHog-style) * * Batches multiple events together and sends them at configurable intervals. * This reduces the number of HTTP requests significantly for active users. * * Features: * - Configurable flush interval (default 3 seconds) * - Batches events by URL/batchKey * - Uses sendBeacon on page unload for reliable delivery * - Converts absolute timestamps to relative offsets before sending */ import type { TrackingEvent } from "./types"; export declare const DEFAULT_FLUSH_INTERVAL_MS = 3000; export interface QueuedRequest { url: string; event: TrackingEvent; batchKey?: string; transport?: "xhr" | "sendBeacon"; } export interface BatchedRequest { url: string; events: TrackingEvent[]; batchKey?: string; transport?: "xhr" | "sendBeacon"; } export interface RequestQueueConfig { flush_interval_ms?: number; } export declare class RequestQueue { private _isPaused; private _queue; private _flushTimeout?; private _flushTimeoutMs; private _sendRequest; constructor(sendRequest: (req: BatchedRequest) => void, config?: RequestQueueConfig); /** * Get the current queue length */ get length(): number; /** * Enqueue an event for batched sending */ enqueue(req: QueuedRequest): void; /** * Flush all queued events immediately using sendBeacon * Called on page unload to ensure events are delivered */ unload(): void; /** * Enable the queue and start flushing */ enable(): void; /** * Pause the queue (stops flushing but keeps events) */ pause(): void; /** * Force an immediate flush */ flush(): void; /** * Set up the flush timeout */ private _setFlushTimeout; /** * Clear the flush timeout */ private _clearFlushTimeout; /** * Flush all queued events now */ private _flushNow; /** * Format the queue into batched requests by URL/batchKey */ private _formatQueue; }