Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

756 B
Raw Blame
import { LiveEvent } from '~/types'

/** Prepend new events to existing state, deduplicating by uuid and capping at `limit`. */
export function deduplicateEvents(state: LiveEvent[], incoming: LiveEvent[], limit: number): LiveEvent[] {
    const seen = new Set(state.map((e) => e.uuid).filter(Boolean))
    const newEvents: LiveEvent[] = []

    for (const event of incoming) {
        if (!seen.has(event.uuid)) {
            newEvents.push(event)
            seen.add(event.uuid)
        }
    }

    // duplicate-only batches keep the existing array identity so downstream memoization holds
    if (newEvents.length === 0) {
        return state.length > limit ? state.slice(0, limit) : state
    }

    return [...newEvents, ...state].slice(0, limit)
}