@clerk/shared
Version:
Internal package utils used by the Clerk SDKs
1 lines • 1.46 kB
Source Map (JSON)
{"version":3,"file":"poller.mjs","names":[],"sources":["../src/poller.ts"],"sourcesContent":["import { createWorkerTimers } from './workerTimers';\n\nexport type PollerStop = () => void;\nexport type PollerCallback = (stop: PollerStop) => Promise<unknown>;\nexport type PollerRun = (cb: PollerCallback) => Promise<void>;\n\ntype PollerOptions = {\n delayInMs: number;\n};\n\nexport type Poller = {\n run: PollerRun;\n stop: PollerStop;\n};\n\n/**\n *\n */\nexport function Poller({ delayInMs }: PollerOptions = { delayInMs: 1000 }): Poller {\n const workerTimers = createWorkerTimers();\n\n let timerId: number | undefined;\n let stopped = false;\n\n const stop: PollerStop = () => {\n if (timerId) {\n workerTimers.clearTimeout(timerId);\n workerTimers.cleanup();\n }\n stopped = true;\n };\n\n const run: PollerRun = async cb => {\n stopped = false;\n await cb(stop);\n if (stopped) {\n return;\n }\n\n timerId = workerTimers.setTimeout(() => {\n void run(cb);\n }, delayInMs) as any as number;\n };\n\n return { run, stop };\n}\n"],"mappings":";;;;;;AAkBA,SAAgB,OAAO,EAAE,cAA6B,EAAE,WAAW,IAAK,GAAW;CACjF,MAAM,eAAe,mBAAmB;CAExC,IAAI;CACJ,IAAI,UAAU;CAEd,MAAM,aAAyB;EAC7B,IAAI,SAAS;GACX,aAAa,aAAa,OAAO;GACjC,aAAa,QAAQ;EACvB;EACA,UAAU;CACZ;CAEA,MAAM,MAAiB,OAAM,OAAM;EACjC,UAAU;EACV,MAAM,GAAG,IAAI;EACb,IAAI,SACF;EAGF,UAAU,aAAa,iBAAiB;GACtC,AAAK,IAAI,EAAE;EACb,GAAG,SAAS;CACd;CAEA,OAAO;EAAE;EAAK;CAAK;AACrB"}