Skip to content

Commit 9cc3fac

Browse files
authored
Improved eoa timeout logic (#906)
* cleaner logic * feat: changeset
1 parent ee5b79f commit 9cc3fac

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

.changeset/calm-pets-begin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@relayprotocol/relay-kit-ui': patch
3+
---
4+
5+
Refactored timeout logic

packages/ui/src/hooks/useEOADetection.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,25 @@ const useEOADetection = (
133133
return
134134
}
135135

136-
const abortController = new AbortController()
137-
const timeoutId = setTimeout(() => {
138-
abortController.abort()
139-
}, 2500)
136+
const timeoutMs = 2500
137+
const timeoutError = new Error('EOA_DETECTION_TIMEOUT')
138+
let timeoutId: ReturnType<typeof setTimeout> | undefined
140139

141140
const startTime = performance.now()
142141

143142
try {
144143
const eoaResult = await Promise.race([
145144
wallet.isEOA(chainId!),
146145
new Promise<never>((_, reject) => {
147-
abortController.signal.addEventListener('abort', () => {
148-
reject(new Error('EOA_DETECTION_TIMEOUT'))
149-
})
146+
timeoutId = setTimeout(() => {
147+
reject(timeoutError)
148+
}, timeoutMs)
150149
})
151150
])
152151

153-
clearTimeout(timeoutId)
152+
if (timeoutId) {
153+
clearTimeout(timeoutId)
154+
}
154155
const { isEOA, isEIP7702Delegated } = eoaResult
155156
const explicitDepositValue = !isEOA || isEIP7702Delegated
156157

@@ -160,9 +161,11 @@ const useEOADetection = (
160161
: current
161162
)
162163
} catch (eoaError: any) {
163-
clearTimeout(timeoutId)
164+
if (timeoutId) {
165+
clearTimeout(timeoutId)
166+
}
164167
const duration = performance.now() - startTime
165-
const isTimeout = eoaError?.message === 'EOA_DETECTION_TIMEOUT'
168+
const isTimeout = eoaError === timeoutError
166169

167170
if (isTimeout) {
168171
console.error('[EOA Detection] timeout', {

0 commit comments

Comments
 (0)