Commit 76eb57f2 authored by isstuev's avatar isstuev

change socket model

parent f2e4419e
...@@ -49,9 +49,9 @@ export namespace SocketMessage { ...@@ -49,9 +49,9 @@ export namespace SocketMessage {
SocketMessageParamsGeneric<'current_coin_balance', { coin_balance: string; block_number: number; exchange_rate: string }>; SocketMessageParamsGeneric<'current_coin_balance', { coin_balance: string; block_number: number; exchange_rate: string }>;
export type AddressTokenBalance = SocketMessageParamsGeneric<'token_balance', { block_number: number }>; export type AddressTokenBalance = SocketMessageParamsGeneric<'token_balance', { block_number: number }>;
export type AddressCoinBalance = SocketMessageParamsGeneric<'coin_balance', { coin_balance: AddressCoinBalanceHistoryItem }>; export type AddressCoinBalance = SocketMessageParamsGeneric<'coin_balance', { coin_balance: AddressCoinBalanceHistoryItem }>;
export type AddressTxs = SocketMessageParamsGeneric<'transaction', { transaction: Transaction }>; export type AddressTxs = SocketMessageParamsGeneric<'transaction', { transactions: Array<Transaction> }>;
export type AddressTxsPending = SocketMessageParamsGeneric<'pending_transaction', { transaction: Transaction }>; export type AddressTxsPending = SocketMessageParamsGeneric<'pending_transaction', { transactions: Array<Transaction> }>;
export type AddressTokenTransfer = SocketMessageParamsGeneric<'token_transfer', { token_transfer: TokenTransfer }>; export type AddressTokenTransfer = SocketMessageParamsGeneric<'token_transfer', { token_transfers: Array<TokenTransfer> }>;
export type AddressChangedBytecode = SocketMessageParamsGeneric<'changed_bytecode', Record<string, never>>; export type AddressChangedBytecode = SocketMessageParamsGeneric<'changed_bytecode', Record<string, never>>;
export type TokenTransfers = SocketMessageParamsGeneric<'token_transfer', {token_transfer: number }>; export type TokenTransfers = SocketMessageParamsGeneric<'token_transfer', {token_transfer: number }>;
export type TokenTotalSupply = SocketMessageParamsGeneric<'total_supply', {total_supply: number }>; export type TokenTotalSupply = SocketMessageParamsGeneric<'total_supply', {total_supply: number }>;
......
...@@ -117,11 +117,21 @@ const AddressTokenTransfers = ({ scrollRef }: {scrollRef?: React.RefObject<HTMLD ...@@ -117,11 +117,21 @@ const AddressTokenTransfers = ({ scrollRef }: {scrollRef?: React.RefObject<HTMLD
const handleNewSocketMessage: SocketMessage.AddressTokenTransfer['handler'] = (payload) => { const handleNewSocketMessage: SocketMessage.AddressTokenTransfer['handler'] = (payload) => {
setSocketAlert(''); setSocketAlert('');
if (data?.items && data.items.length >= OVERLOAD_COUNT) { const newItems: Array<TokenTransfer> = [];
if (matchFilters(filters, payload.token_transfer, currentAddress)) {
setNewItemsCount(prev => prev + 1); payload.token_transfers.forEach(transfer => {
if (data?.items && data.items.length + newItems.length >= OVERLOAD_COUNT) {
if (matchFilters(filters, transfer, currentAddress)) {
setNewItemsCount(prev => prev + 1);
}
} else {
if (matchFilters(filters, transfer, currentAddress)) {
newItems.push(transfer);
}
} }
} else { });
if (newItems.length > 0) {
queryClient.setQueryData( queryClient.setQueryData(
getResourceKey('address_token_transfers', { pathParams: { hash: currentAddress }, queryParams: { ...filters } }), getResourceKey('address_token_transfers', { pathParams: { hash: currentAddress }, queryParams: { ...filters } }),
(prevData: AddressTokenTransferResponse | undefined) => { (prevData: AddressTokenTransferResponse | undefined) => {
...@@ -129,19 +139,17 @@ const AddressTokenTransfers = ({ scrollRef }: {scrollRef?: React.RefObject<HTMLD ...@@ -129,19 +139,17 @@ const AddressTokenTransfers = ({ scrollRef }: {scrollRef?: React.RefObject<HTMLD
return; return;
} }
if (!matchFilters(filters, payload.token_transfer, currentAddress)) {
return prevData;
}
return { return {
...prevData, ...prevData,
items: [ items: [
payload.token_transfer, ...newItems,
...prevData.items, ...prevData.items,
], ],
}; };
}); },
);
} }
}; };
const handleSocketClose = React.useCallback(() => { const handleSocketClose = React.useCallback(() => {
......
...@@ -62,16 +62,6 @@ const AddressTxs = ({ scrollRef }: {scrollRef?: React.RefObject<HTMLDivElement>} ...@@ -62,16 +62,6 @@ const AddressTxs = ({ scrollRef }: {scrollRef?: React.RefObject<HTMLDivElement>}
const handleNewSocketMessage: SocketMessage.AddressTxs['handler'] = (payload) => { const handleNewSocketMessage: SocketMessage.AddressTxs['handler'] = (payload) => {
setSocketAlert(''); setSocketAlert('');
if (addressTxsQuery.data?.items && addressTxsQuery.data.items.length >= OVERLOAD_COUNT) {
if (
!filterValue ||
(filterValue === 'from' && payload.transaction.from.hash === currentAddress) ||
(filterValue === 'to' && payload.transaction.to?.hash === currentAddress)
) {
setNewItemsCount(prev => prev + 1);
}
}
queryClient.setQueryData( queryClient.setQueryData(
getResourceKey('address_txs', { pathParams: { hash: currentAddress }, queryParams: { filter: filterValue } }), getResourceKey('address_txs', { pathParams: { hash: currentAddress }, queryParams: { filter: filterValue } }),
(prevData: AddressTransactionsResponse | undefined) => { (prevData: AddressTransactionsResponse | undefined) => {
...@@ -79,32 +69,30 @@ const AddressTxs = ({ scrollRef }: {scrollRef?: React.RefObject<HTMLDivElement>} ...@@ -79,32 +69,30 @@ const AddressTxs = ({ scrollRef }: {scrollRef?: React.RefObject<HTMLDivElement>}
return; return;
} }
const currIndex = prevData.items.findIndex((tx) => tx.hash === payload.transaction.hash); const newItems = [ ...prevData.items ];
if (currIndex > -1) { payload.transactions.forEach(tx => {
prevData.items[currIndex] = payload.transaction; const currIndex = newItems.findIndex((item) => item.hash === tx.hash);
return prevData;
} if (currIndex > -1) {
newItems[currIndex] = tx;
if (prevData.items.length >= OVERLOAD_COUNT) { } else {
return prevData; if (!filterValue ||
} (filterValue === 'from' && tx.from.hash === currentAddress) ||
(filterValue === 'to' && tx.to?.hash === currentAddress)
if (filterValue) { ) {
if ( if (newItems.length >= OVERLOAD_COUNT) {
(filterValue === 'from' && payload.transaction.from.hash !== currentAddress) || setNewItemsCount(prev => prev + 1);
(filterValue === 'to' && payload.transaction.to?.hash !== currentAddress) } else {
) { newItems.unshift(tx);
return prevData; }
}
} }
} });
return { return {
...prevData, ...prevData,
items: [ items: newItems,
payload.transaction,
...prevData.items,
],
}; };
}); });
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment