-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to close transaction toasts on timeout (#1352)
* Added ability to close transaction toasts on timeout * Refactor * Refactor * Added WS fallback mechanisms for transactions tracker * Lint * Refactor * Refactor * Refactor
- Loading branch information
1 parent
e7c49a7
commit dd29454
Showing
20 changed files
with
186 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useTransactionsTracker'; |
56 changes: 56 additions & 0 deletions
56
src/hooks/transactions/useTransactionsTracker/useCheckHangingTransactionsFallback.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { getTransactionsByHashes as defaultGetTxByHash } from 'apiCalls/transactions'; | ||
import { TRANSACTIONS_STATUS_POLLING_INTERVAL_MS } from 'constants/transactionStatus'; | ||
import { TransactionsTrackerType } from 'types/transactionsTracker.types'; | ||
import { timestampIsOlderThan } from '../helpers/timestampIsOlderThan'; | ||
import { useGetPendingTransactions } from '../useGetPendingTransactions'; | ||
import { useCheckTransactionStatus } from './useCheckTransactionStatus'; | ||
|
||
/** | ||
* Fallback mechanism to check hanging transactions | ||
* Resolves the toast and set the status to failed for each transaction after a certain time (90 seconds) | ||
* */ | ||
export const useCheckHangingTransactionsFallback = ( | ||
props?: TransactionsTrackerType | ||
) => { | ||
const { pendingTransactionsArray } = useGetPendingTransactions(); | ||
const checkTransactionStatus = useCheckTransactionStatus(); | ||
const pollingIntervalTimer = useRef<NodeJS.Timeout | null>(null); | ||
|
||
const getTransactionsByHash = | ||
props?.getTransactionsByHash ?? defaultGetTxByHash; | ||
|
||
const checkHangingTransactions = async () => { | ||
for (const [sessionId] of pendingTransactionsArray) { | ||
if ( | ||
!timestampIsOlderThan( | ||
Number(sessionId), | ||
TRANSACTIONS_STATUS_POLLING_INTERVAL_MS | ||
) | ||
) { | ||
continue; | ||
} | ||
|
||
await checkTransactionStatus({ | ||
getTransactionsByHash, | ||
...props | ||
}); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (pollingIntervalTimer.current) { | ||
return; | ||
} | ||
|
||
pollingIntervalTimer.current = setInterval(() => { | ||
checkHangingTransactions(); | ||
}, TRANSACTIONS_STATUS_POLLING_INTERVAL_MS); | ||
|
||
return () => { | ||
if (pollingIntervalTimer.current) { | ||
clearInterval(pollingIntervalTimer.current); | ||
} | ||
}; | ||
}, []); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/hooks/transactions/useTransactionsTracker/useTransactionsTracker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useEffect } from 'react'; | ||
import { getTransactionsByHashes as defaultGetTxByHash } from 'apiCalls/transactions'; | ||
import { TransactionsTrackerType } from 'types/transactionsTracker.types'; | ||
import { useRegisterWebsocketListener } from '../../websocketListener'; | ||
import { useCheckHangingTransactionsFallback } from './useCheckHangingTransactionsFallback'; | ||
import { useCheckTransactionOnWsFailureFallback } from './useCheckTransactionOnWsFailureFallback'; | ||
import { useCheckTransactionStatus } from './useCheckTransactionStatus'; | ||
|
||
export function useTransactionsTracker(props?: TransactionsTrackerType) { | ||
const checkTransactionStatus = useCheckTransactionStatus(); | ||
|
||
const getTransactionsByHash = | ||
props?.getTransactionsByHash ?? defaultGetTxByHash; | ||
|
||
const onMessage = () => { | ||
checkTransactionStatus({ | ||
getTransactionsByHash, | ||
...props | ||
}); | ||
}; | ||
|
||
// register ws listener | ||
useRegisterWebsocketListener(onMessage); | ||
|
||
// Fallbacks | ||
useCheckTransactionOnWsFailureFallback(props); | ||
|
||
useCheckHangingTransactionsFallback(props); | ||
|
||
useEffect(() => { | ||
onMessage(); | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.