()
| 93 | } |
| 94 | |
| 95 | function start() { |
| 96 | if (cleanup) return |
| 97 | lastEventTs = Date.now() |
| 98 | cleanup = opts.subscribe({ apply }) |
| 99 | |
| 100 | visibilityHandler = () => { |
| 101 | if (document.visibilityState !== 'visible') return |
| 102 | if (Date.now() - lastEventTs > stale) { |
| 103 | // Connection might have died silently while backgrounded — take |
| 104 | // the optimistic stance and reconnect. Worst case we drop a few |
| 105 | // duplicate events; best case we recover from a zombie socket. |
| 106 | reconnect() |
| 107 | } |
| 108 | } |
| 109 | document.addEventListener('visibilitychange', visibilityHandler) |
| 110 | } |
| 111 | |
| 112 | function stop() { |
| 113 | cleanup?.() |
no test coverage detected