()
| 51 | } |
| 52 | |
| 53 | const flush = () => { |
| 54 | if (lastSubscriptionRef.current !== subscriptionId) { |
| 55 | return |
| 56 | } |
| 57 | const desired = pendingStateRef.current |
| 58 | if (!desired) { |
| 59 | return |
| 60 | } |
| 61 | if (inFlightRef.current) { |
| 62 | return |
| 63 | } |
| 64 | if (retryTimerRef.current) { |
| 65 | return |
| 66 | } |
| 67 | if (lastStateRef.current === desired) { |
| 68 | pendingStateRef.current = null |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | inFlightRef.current = true |
| 73 | let hadError = false |
| 74 | const activeSubscription = subscriptionId |
| 75 | void api.setVisibility({ |
| 76 | subscriptionId, |
| 77 | visibility: desired |
| 78 | }).then(() => { |
| 79 | if (lastSubscriptionRef.current !== activeSubscription) { |
| 80 | return |
| 81 | } |
| 82 | lastStateRef.current = desired |
| 83 | pendingStateRef.current = null |
| 84 | clearRetry() |
| 85 | }).catch((error) => { |
| 86 | if (lastSubscriptionRef.current !== activeSubscription) { |
| 87 | return |
| 88 | } |
| 89 | hadError = true |
| 90 | console.error('Failed to update visibility:', error) |
| 91 | if (!retryTimerRef.current) { |
| 92 | retryTimerRef.current = setTimeout(() => { |
| 93 | retryTimerRef.current = null |
| 94 | flush() |
| 95 | }, 2000) |
| 96 | } |
| 97 | }).finally(() => { |
| 98 | inFlightRef.current = false |
| 99 | if (hadError || retryTimerRef.current) { |
| 100 | return |
| 101 | } |
| 102 | if (pendingStateRef.current && pendingStateRef.current !== lastStateRef.current) { |
| 103 | flush() |
| 104 | } |
| 105 | }) |
| 106 | } |
| 107 | |
| 108 | const report = () => { |
| 109 | const state = getVisibilityState() |
no test coverage detected