MCPcopy Create free account
hub / github.com/tiann/hapi / flush

Function flush

web/src/hooks/useVisibilityReporter.ts:53–106  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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()

Callers 1

reportFunction · 0.70

Calls 3

clearRetryFunction · 0.85
errorMethod · 0.80
setVisibilityMethod · 0.45

Tested by

no test coverage detected