Skip to content

The Open Standard for Generative UI

5.8 KB
Raw Blame
import { EventQueue } from "./queue";
import { sendEnvelope, sendEnvelopeBeacon, type TransportConfig } from "./transport";
import { SDK_VERSION, type WireEnvelope, type WireEvent } from "./wire";

const FLUSH_INTERVAL_MS = 5000;
const BATCH_SIZE = 50;
const DEFAULT_FLUSH_TIMEOUT_MS = 10_000;

function buildEnvelope(
  events: WireEvent[],
  droppedEvents: number,
  capture: "full" | "minimal",
): WireEnvelope {
  return {
    v: 1,
    sentAt: Date.now(),
    sdk: { name: "observability-cloud", version: SDK_VERSION },
    capture,
    ...(droppedEvents > 0 ? { droppedEvents } : {}),
    events,
  };
}

export interface BatcherOptions extends TransportConfig {
  capture: "full" | "minimal";
}

type BatchSendResult = { accepted: boolean; timedOut: boolean };

export class Batcher {
  private readonly queue = new EventQueue();
  private readonly inFlight = new Set<Promise<BatchSendResult>>();
  private droppedEvents = 0;
  private intervalId: ReturnType<typeof setInterval> | null = null;
  private closed = false;
  private readonly onPageHide: () => void;
  private readonly onVisibilityChange: () => void;

  private readonly transport: TransportConfig;
  private readonly capture: "full" | "minimal";

  constructor(options: BatcherOptions) {
    this.transport = { endpoint: options.endpoint, apiKey: options.apiKey, debug: options.debug };
    this.capture = options.capture;
    this.onPageHide = () => {
      this.flushBeaconSync();
    };
    this.onVisibilityChange = () => {
      if (document.visibilityState === "hidden") {
        this.flushBeaconSync();
      }
    };

    if (typeof window !== "undefined" && typeof document !== "undefined") {
      window.addEventListener("pagehide", this.onPageHide);
      document.addEventListener("visibilitychange", this.onVisibilityChange);
    }
  }

  enqueue(event: WireEvent): void {
    if (this.closed) return;
    this.queue.enqueue(event);
    if (this.queue.size >= BATCH_SIZE) {
      void this.flushNextBatch();
    }
    this.ensureInterval();
  }

  async flush(timeoutMs = DEFAULT_FLUSH_TIMEOUT_MS): Promise<boolean> {
    if (this.closed && this.queue.size === 0 && this.inFlight.size === 0) return true;

    const deadline = Date.now() + timeoutMs;
    let allAccepted = true;

    while (this.queue.size > 0) {
      if (Date.now() >= deadline) return false;

      const result = await this.flushNextBatch(deadline);
      if (result.timedOut) return false;
      if (!result.accepted) allAccepted = false;
    }

    while (this.inFlight.size > 0) {
      if (Date.now() >= deadline) return false;

      const pending = Promise.all([...this.inFlight]).then((results): BatchSendResult => ({
        accepted: results.every((result) => result.accepted),
        timedOut: false,
      }));
      const result = await this.raceDeadline(pending, deadline);
      if (result.timedOut) return false;
      if (!result.accepted) allAccepted = false;
    }

    this.stopInterval();
    return allAccepted;
  }

  close(): Promise<boolean> {
    this.closed = true;
    this.detachPageListeners();
    this.stopInterval();
    return this.flush();
  }

  private ensureInterval(): void {
    if (this.intervalId !== null || this.closed) return;
    this.intervalId = setInterval(() => {
      if (this.queue.size === 0) {
        this.stopInterval();
        return;
      }
      void this.flushNextBatch();
    }, FLUSH_INTERVAL_MS);
  }

  private stopInterval(): void {
    if (this.intervalId === null) return;
    clearInterval(this.intervalId);
    this.intervalId = null;
  }

  private detachPageListeners(): void {
    if (typeof window === "undefined" || typeof document === "undefined") return;
    window.removeEventListener("pagehide", this.onPageHide);
    document.removeEventListener("visibilitychange", this.onVisibilityChange);
  }

  private flushBeaconSync(): void {
    while (this.queue.size > 0) {
      const droppedEvents = this.takeDroppedEvents();
      const events = this.queue.drain(BATCH_SIZE);
      if (events.length === 0) break;
      sendEnvelopeBeacon(buildEnvelope(events, droppedEvents, this.capture), this.transport);
    }
    this.stopInterval();
  }

  private takeDroppedEvents(): number {
    const count = this.droppedEvents + this.queue.readAndResetDropped();
    this.droppedEvents = 0;
    return count;
  }

  private async flushNextBatch(deadline?: number): Promise<BatchSendResult> {
    if (this.queue.size === 0) return { accepted: true, timedOut: false };

    const droppedEvents = this.takeDroppedEvents();
    const events = this.queue.drain(BATCH_SIZE);
    if (events.length === 0) {
      this.droppedEvents += droppedEvents;
      return { accepted: true, timedOut: false };
    }

    const sendPromise = sendEnvelope(
      buildEnvelope(events, droppedEvents, this.capture),
      this.transport,
    )
      .catch(() => false)
      .then((accepted): BatchSendResult => {
        if (!accepted) this.droppedEvents += events.length + droppedEvents;
        return { accepted, timedOut: false };
      });

    this.inFlight.add(sendPromise);
    void sendPromise.then(() => {
      this.inFlight.delete(sendPromise);
    });

    return this.raceDeadline(sendPromise, deadline);
  }

  private async raceDeadline(
    sendPromise: Promise<BatchSendResult>,
    deadline?: number,
  ): Promise<BatchSendResult> {
    if (deadline === undefined) {
      return sendPromise;
    }

    const remainingMs = deadline - Date.now();
    if (remainingMs <= 0) {
      return { accepted: false, timedOut: true };
    }

    let timeoutId: ReturnType<typeof setTimeout> | undefined;
    const timeoutPromise = new Promise<BatchSendResult>((resolve) => {
      timeoutId = setTimeout(() => resolve({ accepted: false, timedOut: true }), remainingMs);
    });

    try {
      return await Promise.race([sendPromise, timeoutPromise]);
    } finally {
      if (timeoutId !== undefined) clearTimeout(timeoutId);
    }
  }
}