UNPKG

@tldraw/sync-core

Version:

tldraw infinite canvas SDK (multiplayer sync).

8 lines (7 loc) 1.6 kB
{ "version": 3, "sources": ["../../src/lib/interval.ts"], "sourcesContent": ["/**\n * Creates a repeating timer that executes a callback at regular intervals and returns a cleanup function.\n *\n * This utility function wraps the standard `setInterval`/`clearInterval` pattern into a more convenient\n * interface that returns a dispose function for cleanup. It's commonly used in the sync system for\n * periodic tasks like health checks, ping operations, and session pruning.\n *\n * @param cb - The callback function to execute at each interval\n * @param timeout - The time interval in milliseconds between callback executions\n * @returns A cleanup function that stops the interval when called\n *\n * @example\n * ```ts\n * // Create a periodic health check\n * const stopHealthCheck = interval(() => {\n * console.log('Checking server health...')\n * checkServerConnection()\n * }, 5000)\n *\n * // Later, stop the health check\n * stopHealthCheck()\n * ```\n *\n * @example\n * ```ts\n * // Use in a disposables array for cleanup management\n * class MyClass {\n * private disposables = [\n * interval(() => this.sendPing(), 30000),\n * interval(() => this.pruneSessions(), 2000)\n * ]\n *\n * dispose() {\n * this.disposables.forEach(dispose => dispose())\n * }\n * }\n * ```\n *\n * @public\n */\nexport function interval(cb: () => void, timeout: number) {\n\tconst i = setInterval(cb, timeout)\n\treturn () => clearInterval(i)\n}\n"], "mappings": "AAwCO,SAAS,SAAS,IAAgB,SAAiB;AACzD,QAAM,IAAI,YAAY,IAAI,OAAO;AACjC,SAAO,MAAM,cAAc,CAAC;AAC7B;", "names": [] }