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.

1.0 KB
Raw Blame
import { WIDGET_TILE_REFRESH_DEBOUNCE_MS } from './widgets/constants'

export type DashboardWidgetTileRefreshScheduler = {
    schedule: (tileId: number) => void
    cancelAll: () => void
}

export function createDashboardWidgetTileRefreshScheduler(
    refreshTile: (tileId: number) => void,
    debounceMs: number = WIDGET_TILE_REFRESH_DEBOUNCE_MS
): DashboardWidgetTileRefreshScheduler {
    const timeouts = new Map<number, ReturnType<typeof setTimeout>>()

    return {
        schedule(tileId: number): void {
            const existing = timeouts.get(tileId)
            if (existing) {
                clearTimeout(existing)
            }
            timeouts.set(
                tileId,
                setTimeout(() => {
                    timeouts.delete(tileId)
                    refreshTile(tileId)
                }, debounceMs)
            )
        },
        cancelAll(): void {
            for (const timeout of timeouts.values()) {
                clearTimeout(timeout)
            }
            timeouts.clear()
        },
    }
}