|
| 1 | +import { lazy } from '../helpers/lazy'; |
| 2 | +import { getLogger } from '../logger'; |
| 3 | +import { TimerWorkerEvent, TimerWorkerRequest } from './types'; |
| 4 | +import { timerWorker } from './worker.build'; |
| 5 | + |
| 6 | +class TimerWorker { |
| 7 | + private currentTimerId = 1; |
| 8 | + private callbacks = new Map<number, () => void>(); |
| 9 | + private worker: Worker | undefined; |
| 10 | + private fallback = false; |
| 11 | + |
| 12 | + setup(): void { |
| 13 | + try { |
| 14 | + const source = timerWorker.src; |
| 15 | + const blob = new Blob([source], { |
| 16 | + type: 'application/javascript; charset=utf-8', |
| 17 | + }); |
| 18 | + const script = URL.createObjectURL(blob); |
| 19 | + this.worker = new Worker(script, { name: 'str-timer-worker' }); |
| 20 | + this.worker.addEventListener('message', (event) => { |
| 21 | + const { type, id } = event.data as TimerWorkerEvent; |
| 22 | + if (type === 'tick') { |
| 23 | + this.callbacks.get(id)?.(); |
| 24 | + } |
| 25 | + }); |
| 26 | + } catch (err: any) { |
| 27 | + getLogger(['timer-worker'])('error', err); |
| 28 | + this.fallback = true; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + destroy(): void { |
| 33 | + this.callbacks.clear(); |
| 34 | + this.worker?.terminate(); |
| 35 | + this.worker = undefined; |
| 36 | + this.fallback = false; |
| 37 | + } |
| 38 | + |
| 39 | + get ready() { |
| 40 | + return this.fallback || Boolean(this.worker); |
| 41 | + } |
| 42 | + |
| 43 | + setInterval(callback: () => void, timeout: number): number { |
| 44 | + return this.setTimer('setInterval', callback, timeout); |
| 45 | + } |
| 46 | + |
| 47 | + clearInterval(id?: number): void { |
| 48 | + this.clearTimer('clearInterval', id); |
| 49 | + } |
| 50 | + |
| 51 | + setTimeout(callback: () => void, timeout: number): number { |
| 52 | + return this.setTimer('setTimeout', callback, timeout); |
| 53 | + } |
| 54 | + |
| 55 | + clearTimeout(id?: number): void { |
| 56 | + this.clearTimer('clearTimeout', id); |
| 57 | + } |
| 58 | + |
| 59 | + private setTimer( |
| 60 | + type: 'setTimeout' | 'setInterval', |
| 61 | + callback: () => void, |
| 62 | + timeout: number, |
| 63 | + ) { |
| 64 | + if (!this.ready) { |
| 65 | + this.setup(); |
| 66 | + } |
| 67 | + |
| 68 | + if (this.fallback) { |
| 69 | + return (type === 'setTimeout' ? setTimeout : setInterval)( |
| 70 | + callback, |
| 71 | + timeout, |
| 72 | + ) as unknown as number; |
| 73 | + } |
| 74 | + |
| 75 | + const id = this.getTimerId(); |
| 76 | + this.callbacks.set(id, callback); |
| 77 | + this.sendMessage({ type, id, timeout }); |
| 78 | + return id; |
| 79 | + } |
| 80 | + |
| 81 | + private clearTimer(type: 'clearTimeout' | 'clearInterval', id?: number) { |
| 82 | + if (!id) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (!this.ready) { |
| 87 | + this.setup(); |
| 88 | + } |
| 89 | + |
| 90 | + if (this.fallback) { |
| 91 | + this.clearInterval(id); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + this.callbacks.delete(id); |
| 96 | + this.sendMessage({ type, id }); |
| 97 | + } |
| 98 | + |
| 99 | + private getTimerId() { |
| 100 | + return this.currentTimerId++; |
| 101 | + } |
| 102 | + |
| 103 | + private sendMessage(message: TimerWorkerRequest) { |
| 104 | + if (!this.worker) { |
| 105 | + throw new Error("Cannot use timer worker before it's set up"); |
| 106 | + } |
| 107 | + |
| 108 | + this.worker.postMessage(message); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +export const getTimers = lazy(() => { |
| 113 | + const instance = new TimerWorker(); |
| 114 | + instance.setup(); |
| 115 | + return instance; |
| 116 | +}); |
0 commit comments