Skip to content

Commit

Permalink
Remove logs
Browse files Browse the repository at this point in the history
  • Loading branch information
arhtudormorar committed Sep 13, 2024
1 parent a96bd3b commit d319e8b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@ import { checkBatch } from './checkBatch';
import { getPendingStoreTransactions } from '../getPendingStoreTransactions';
import { TransactionsTrackerType } from '../../trackTransactions.types';

export function checkTransactionStatus() {
export async function checkTransactionStatus(
props: TransactionsTrackerType & {
shouldRefreshBalance?: boolean;
}
) {
const { pendingSessions } = getPendingStoreTransactions();

async function checkStatus(
props: TransactionsTrackerType & {
shouldRefreshBalance?: boolean;
}
) {
if (Object.keys(pendingSessions).length > 0) {
for (const [sessionId, { transactions }] of Object.entries(
pendingSessions
)) {
await checkBatch({
sessionId,
transactionBatch: transactions,
...props
});
}
}

if (props.shouldRefreshBalance) {
await refreshAccount();
if (Object.keys(pendingSessions).length > 0) {
for (const [sessionId, { transactions }] of Object.entries(
pendingSessions
)) {
await checkBatch({
sessionId,
transactionBatch: transactions,
...props
});
}
}

return checkStatus;
if (props.shouldRefreshBalance) {
await refreshAccount();
}
}
22 changes: 5 additions & 17 deletions src/core/methods/trackTransactions/trackTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
WebsocketConnectionStatusEnum
} from '../initApp/websocket/websocket.constants';
import { getStore } from 'store/store';
import { websocketEventSelector } from 'store/selectors/accountSelectors';

export async function trackTransactions(props?: TransactionsTrackerType) {
const store = getStore();
const checkStatus = checkTransactionStatus();
const pollingInterval = getPollingInterval();
let pollingIntervalTimer: NodeJS.Timeout | null = null;
let timestamp = websocketEventSelector(store.getState())?.timestamp;

// Check if websocket is completed
const isWebsocketCompleted =
Expand All @@ -24,35 +25,22 @@ export async function trackTransactions(props?: TransactionsTrackerType) {

// Function that handles message (checking transaction status)
const onMessage = () => {
checkStatus({
checkTransactionStatus({
shouldRefreshBalance: isWebsocketCompleted,
getTransactionsByHash,
...props
});
};

console.log({
isWebsocketCompleted
});

if (isWebsocketCompleted) {
console.log('\x1b[42m%s\x1b[0m', 'setting up subscribe');

// Do not set polling interval if websocket is complete
if (pollingIntervalTimer) {
clearInterval(pollingIntervalTimer);
pollingIntervalTimer = null;
}
store.subscribe(async ({ account: { websocketEvent } }) => {
console.log('websocketEvent', websocketEvent);

if (websocketEvent?.message) {
console.log(
'\x1b[42m%s\x1b[0m',
'trackTransactions -> websocketEvent.message',
websocketEvent.message
);

if (websocketEvent?.message && timestamp !== websocketEvent.timestamp) {
timestamp = websocketEvent.timestamp;
onMessage();
}
});
Expand Down
4 changes: 4 additions & 0 deletions src/store/selectors/accountSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export const accountSelector = ({

export const addressSelector = ({ account: { address } }: StoreType) => address;

export const websocketEventSelector = ({
account: { websocketEvent }
}: StoreType) => websocketEvent;

export const accountNonceSelector = (store: StoreType) =>
accountSelector(store)?.nonce || 0;

Expand Down

0 comments on commit d319e8b

Please sign in to comment.