/
githubmirror
/
tldraw
Обзор
Документация
Войти
/
githubmirror
/
tldraw
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/sync-core/src/lib/interval.ts
44 строки
1 KB
Steve Ruiz
Automate docs, sort of (#6722)
06 окт 2025, 12:57
Не верифицирован
06 окт 2025, 12:57
4197e7a
Код
Авторство
О чём код?
/** * Creates a repeating timer that executes a callback at regular intervals and returns a cleanup function. * * This utility function wraps the standard `setInterval`/`clearInterval` pattern into a more convenient * interface that returns a dispose function for cleanup. It's commonly used in the sync system for * periodic tasks like health checks, ping operations, and session pruning. * * @param cb - The callback function to execute at each interval * @param timeout - The time interval in milliseconds between callback executions * @returns A cleanup function that stops the interval when called * * @example * ```ts * // Create a periodic health check * const stopHealthCheck = interval(() => { * console.log('Checking server health...') * checkServerConnection() * }, 5000) * * // Later, stop the health check * stopHealthCheck() * ``` * * @example * ```ts * // Use in a disposables array for cleanup management * class MyClass { * private disposables = [ * interval(() => this.sendPing(), 30000), * interval(() => this.pruneSessions(), 2000) * ] * * dispose() { * this.disposables.forEach(dispose => dispose()) * } * } * ``` * * @public */ export function interval(cb: () => void, timeout: number) { const i = setInterval(cb, timeout) return () => clearInterval(i) }