Skip to content

Commit

Permalink
feat(blocklist): renamed socket files
Browse files Browse the repository at this point in the history
  • Loading branch information
BLuEScioN committed Apr 24, 2024
1 parent ab3f712 commit da76fb7
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 123 deletions.
4 changes: 2 additions & 2 deletions src/app/_components/BlockList/LayoutA/Paginated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Section } from '../../../../common/components/Section';
import { ExplorerErrorBoundary } from '../../ErrorBoundary';
import { useBlockListContext } from '../BlockListContext';
import { BlockListProvider } from '../BlockListProvider';
import { useBlockListWebSocket } from '../Sockets/useBlockListWebSocket';
import { useBlockListWebSocketUIBlock } from '../Sockets/useBlockListWebSocketUIBlock';
import { FADE_DURATION } from '../consts';

Check warning on line 10 in src/app/_components/BlockList/LayoutA/Paginated.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/LayoutA/Paginated.tsx#L7-L10

Added lines #L7 - L10 were not covered by tests
import { UISingleBlock } from '../types';
import { BlockListWithControls } from './BlockListWithControls';
Expand All @@ -33,7 +33,7 @@ function PaginatedBlockListLayoutABase() {
return new Set(Object.keys(initialBurnBlocks));
}, [initialBurnBlocks]);

const { latestUIBlocks, latestBlocksCount, clearLatestBlocks } = useBlockListWebSocket(
const { latestUIBlocks, latestBlocksCount, clearLatestBlocks } = useBlockListWebSocketUIBlock(

Check warning on line 36 in src/app/_components/BlockList/LayoutA/Paginated.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/LayoutA/Paginated.tsx#L36

Added line #L36 was not covered by tests
initialBlockHashes,
initialBurnBlockHashes
);
Expand Down
4 changes: 2 additions & 2 deletions src/app/_components/BlockList/LayoutA/useBlockList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BurnBlock } from '@stacks/blockchain-api-client';
import { NakamotoBlock } from '@stacks/blockchain-api-client/src/generated/models';

import { useBlockListContext } from '../BlockListContext';
import { useBlockListWebSocket } from '../Sockets/useBlockListWebSocket';
import { useBlockListWebSocketUIBlock } from '../Sockets/useBlockListWebSocketUIBlock';
import { FADE_DURATION } from '../consts';
import { UIBlock, UIBlockType } from '../types';
import { useInitialBlockList } from './useInitialBlockList';
Expand Down Expand Up @@ -78,7 +78,7 @@ export function useBlockList(length: number) {
[lastBurnBlock, secondToLastBurnBlock]
);

const { latestBlock, latestBlocksCount, clearLatestBlocks } = useBlockListWebSocket(
const { latestBlock, latestBlocksCount, clearLatestBlocks } = useBlockListWebSocketUIBlock(
initialBlockHashes,
initialBurnBlockHashes
);
Expand Down
80 changes: 25 additions & 55 deletions src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,39 @@
import { useCallback, useRef, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';

Check warning on line 1 in src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts#L1

Added line #L1 was not covered by tests

import { Block } from '@stacks/blockchain-api-client';
import { NakamotoBlock } from '@stacks/blockchain-api-client/src/generated/models';

import { UIBlockType, UISingleBlock } from '../types';
import { useSubscribeBlocks } from './useSubscribeBlocks';

Check warning on line 6 in src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts#L6

Added line #L6 was not covered by tests

export function useBlockListWebSocket(
initialBlockHashes: Set<string>,
initialBurnBlockHashes: Set<string>
) {
const [latestBlocks, setLatestBlocks] = useState<UISingleBlock[]>([]);
const [latestBlock, setLatestBlock] = useState<NakamotoBlock>();
const latestBlockHashes = useRef(new Set<string>());
const latestBurnBlockHashes = useRef(new Set<string>());
export function useBlockListWebSocket(liveUpdates: boolean, initialStxBlockHashes: Set<string>) {
const [latestStxBlocks, setLatestStxBlocks] = useState<(NakamotoBlock | Block)[]>([]);

Check warning on line 9 in src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts#L8-L9

Added lines #L8 - L9 were not covered by tests

const handleBlock = useCallback(
(block: NakamotoBlock) => {
function updateLatestBlocks() {
if (latestBlockHashes.current.has(block.hash) || initialBlockHashes.has(block.hash)) {
return;
}
setLatestBlock(block);
latestBlockHashes.current.add(block.hash);
const isNewBurnBlock =
!initialBurnBlockHashes.has(block.burn_block_hash) &&
!latestBurnBlockHashes.current.has(block.burn_block_hash);
if (isNewBurnBlock) {
latestBurnBlockHashes.current.add(block.burn_block_hash);
setLatestBlocks(prevLatestBlocks => [
{
type: UIBlockType.BurnBlock,
height: block.burn_block_height,
hash: block.burn_block_hash,
timestamp: block.burn_block_time,
},
...prevLatestBlocks,
]);
}
setLatestBlocks(prevLatestBlocks => [
{
type: UIBlockType.Block,
height: block.height,
hash: block.hash,
timestamp: block.burn_block_time,
txsCount: block?.tx_count,
},
...prevLatestBlocks,
]);
}
const stxBlockHashes = useRef(new Set<string>(initialStxBlockHashes));

Check warning on line 11 in src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts#L11

Added line #L11 was not covered by tests
// update ref when initialStxBlockHashes changes
useEffect(() => {
stxBlockHashes.current = new Set(initialStxBlockHashes);

Check warning on line 14 in src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts#L13-L14

Added lines #L13 - L14 were not covered by tests
}, [initialStxBlockHashes]);

updateLatestBlocks();
},
[initialBurnBlockHashes, initialBlockHashes]
);
const handleBlock = useCallback((stxBlock: NakamotoBlock | Block) => {

Check warning on line 17 in src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts#L17

Added line #L17 was not covered by tests
// If the block is already in the list, don't add it again
if (stxBlockHashes.current.has(stxBlock.hash)) {
return;

Check warning on line 20 in src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts#L20

Added line #L20 was not covered by tests
}

useSubscribeBlocks(handleBlock);
// Otherwise, add it to the list
setLatestStxBlocks(prevLatestStxBlocks => [stxBlock, ...prevLatestStxBlocks]);
stxBlockHashes.current.add(stxBlock.hash);

Check warning on line 25 in src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts#L24-L25

Added lines #L24 - L25 were not covered by tests
}, []);

const clearLatestBlocks = () => {
setLatestBlocks([]);
useSubscribeBlocks(liveUpdates, handleBlock);

Check warning on line 28 in src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts#L28

Added line #L28 was not covered by tests

const clearLatestStxBlocks = () => {
setLatestStxBlocks([]);

Check warning on line 31 in src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts#L30-L31

Added lines #L30 - L31 were not covered by tests
};

return {

Check warning on line 34 in src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useBlockListWebSocket.ts#L34

Added line #L34 was not covered by tests
latestUIBlocks: latestBlocks,
latestBlock,
latestBlocksCount: latestBlocks.filter(block => block.type === UIBlockType.Block).length,
clearLatestBlocks,
latestStxBlocks,
latestStxBlocksCount: latestStxBlocks.length,
clearLatestStxBlocks,
};
}
39 changes: 0 additions & 39 deletions src/app/_components/BlockList/Sockets/useBlockListWebSocket2.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useCallback, useRef, useState } from 'react';

import { NakamotoBlock } from '@stacks/blockchain-api-client/src/generated/models';

import { UIBlockType, UISingleBlock } from '../types';
import { useSubscribeBlocksUIBlock } from './useSubscribeBlocksUIBlock';

export function useBlockListWebSocketUIBlock(
initialBlockHashes: Set<string>,
initialBurnBlockHashes: Set<string>
) {
const [latestBlocks, setLatestBlocks] = useState<UISingleBlock[]>([]);
const [latestBlock, setLatestBlock] = useState<NakamotoBlock>();
const latestBlockHashes = useRef(new Set<string>());
const latestBurnBlockHashes = useRef(new Set<string>());

const handleBlock = useCallback(
(block: NakamotoBlock) => {
function updateLatestBlocks() {
if (latestBlockHashes.current.has(block.hash) || initialBlockHashes.has(block.hash)) {
return;
}
setLatestBlock(block);
latestBlockHashes.current.add(block.hash);
const isNewBurnBlock =
!initialBurnBlockHashes.has(block.burn_block_hash) &&
!latestBurnBlockHashes.current.has(block.burn_block_hash);
if (isNewBurnBlock) {
latestBurnBlockHashes.current.add(block.burn_block_hash);
setLatestBlocks(prevLatestBlocks => [
{
type: UIBlockType.BurnBlock,
height: block.burn_block_height,
hash: block.burn_block_hash,
timestamp: block.burn_block_time,
},
...prevLatestBlocks,
]);
}
setLatestBlocks(prevLatestBlocks => [
{
type: UIBlockType.Block,
height: block.height,
hash: block.hash,
timestamp: block.burn_block_time,
txsCount: block?.tx_count,
},
...prevLatestBlocks,
]);
}

updateLatestBlocks();
},
[initialBurnBlockHashes, initialBlockHashes]
);

useSubscribeBlocksUIBlock(handleBlock);

const clearLatestBlocks = () => {
setLatestBlocks([]);
};

return {
latestUIBlocks: latestBlocks,
latestBlock,
latestBlocksCount: latestBlocks.filter(block => block.type === UIBlockType.Block).length,
clearLatestBlocks,
};
}
16 changes: 11 additions & 5 deletions src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useRef } from 'react';

Check warning on line 1 in src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts#L1

Added line #L1 was not covered by tests

import { StacksApiSocketClient } from '@stacks/blockchain-api-client';
import { Block, StacksApiSocketClient } from '@stacks/blockchain-api-client';
import { NakamotoBlock } from '@stacks/blockchain-api-client/src/generated/models';

import { useGlobalContext } from '../../../../common/context/useAppContext';

Check warning on line 6 in src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts#L6

Added line #L6 was not covered by tests
Expand All @@ -9,7 +9,10 @@ interface Subscription {
unsubscribe(): void;
}

export function useSubscribeBlocks(handleBlock: (block: NakamotoBlock) => void) {
export function useSubscribeBlocks(

Check warning on line 12 in src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts#L12

Added line #L12 was not covered by tests
liveUpdates: boolean,
handleBlock: (block: NakamotoBlock | Block) => void

Check warning on line 14 in src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts#L14

Added line #L14 was not covered by tests
) {
const subscription = useRef<Subscription | undefined>(undefined);
const { stacksApiSocketClientInfo } = useGlobalContext();

Check warning on line 17 in src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts#L16-L17

Added lines #L16 - L17 were not covered by tests
const { client, connect, disconnect } = stacksApiSocketClientInfo || {};
Expand All @@ -18,19 +21,22 @@ export function useSubscribeBlocks(handleBlock: (block: NakamotoBlock) => void)
const subscribe = async (client: StacksApiSocketClient) => {
subscription.current = client?.subscribeBlocks(block => {
handleBlock({

Check warning on line 23 in src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts#L20-L23

Added lines #L20 - L23 were not covered by tests
...(block as unknown as NakamotoBlock),
...(block as Block),
parent_index_block_hash: '',
tx_count: 0,
});
});
};

if (!client?.socket.connected) {
if (liveUpdates && !client?.socket.connected) {
connect?.(client => subscribe(client));

Check warning on line 32 in src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts#L32

Added line #L32 was not covered by tests
}
if (!liveUpdates && client?.socket.connected) {
disconnect?.();

Check warning on line 35 in src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts#L35

Added line #L35 was not covered by tests
}
return () => {
disconnect?.();

Check warning on line 38 in src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts#L37-L38

Added lines #L37 - L38 were not covered by tests
};
}, [client, handleBlock, connect, disconnect]);
}, [client, handleBlock, connect, liveUpdates, disconnect]);
return subscription;

Check warning on line 41 in src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts#L41

Added line #L41 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useRef } from 'react';

import { Block, StacksApiSocketClient } from '@stacks/blockchain-api-client';
import { StacksApiSocketClient } from '@stacks/blockchain-api-client';
import { NakamotoBlock } from '@stacks/blockchain-api-client/src/generated/models';

import { useGlobalContext } from '../../../../common/context/useAppContext';
Expand All @@ -9,10 +9,7 @@ interface Subscription {
unsubscribe(): void;
}

export function useSubscribeBlocks2(
liveUpdates: boolean,
handleBlock: (block: NakamotoBlock | Block) => void
) {
export function useSubscribeBlocksUIBlock(handleBlock: (block: NakamotoBlock) => void) {
const subscription = useRef<Subscription | undefined>(undefined);
const { stacksApiSocketClientInfo } = useGlobalContext();
const { client, connect, disconnect } = stacksApiSocketClientInfo || {};
Expand All @@ -21,22 +18,19 @@ export function useSubscribeBlocks2(
const subscribe = async (client: StacksApiSocketClient) => {
subscription.current = client?.subscribeBlocks(block => {
handleBlock({

Check warning on line 20 in src/app/_components/BlockList/Sockets/useSubscribeBlocksUIBlock.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocksUIBlock.ts#L19-L20

Added lines #L19 - L20 were not covered by tests
...(block as Block),
...(block as unknown as NakamotoBlock),
parent_index_block_hash: '',
tx_count: 0,
});
});
};

if (liveUpdates && !client?.socket.connected) {
if (!client?.socket.connected) {
connect?.(client => subscribe(client));
}
if (!liveUpdates && client?.socket.connected) {
disconnect?.();
}
return () => {
disconnect?.();
};
}, [client, handleBlock, connect, liveUpdates, disconnect]);
}, [client, handleBlock, connect, disconnect]);
return subscription;
}
4 changes: 2 additions & 2 deletions src/app/_components/BlockList/UpdatedBlockList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { StxIcon } from '../../../ui/icons';
import { ExplorerErrorBoundary } from '../ErrorBoundary';
import { BurnBlock } from './LayoutA/BurnBlock';
import { StxBlock } from './LayoutA/StxBlock';
import { useSubscribeBlocks2 } from './Sockets/useSubscribeBlocks2';
import { useSubscribeBlocks } from './Sockets/useSubscribeBlocks';

Check warning on line 27 in src/app/_components/BlockList/UpdatedBlockList.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/UpdatedBlockList.tsx#L27

Added line #L27 was not covered by tests
import { EnhancedBlock } from './types';

export const animationDuration = 0.8;
Expand Down Expand Up @@ -124,7 +124,7 @@ function UpdatedBlocksListBase({
...prevLatestBlocks,
]);
}, []);
useSubscribeBlocks2(isLive, handleBlock);
useSubscribeBlocks(isLive, handleBlock);
useEffect(() => {

Check warning on line 128 in src/app/_components/BlockList/UpdatedBlockList.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/UpdatedBlockList.tsx#L127-L128

Added lines #L127 - L128 were not covered by tests
if (!isLive) return;
setLatestBlocks([]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback, useEffect, useRef, useState } from 'react';

Check warning on line 1 in src/app/_components/BlockList/data/useBlocksPageBlockListGrouped.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/data/useBlocksPageBlockListGrouped.tsx#L1

Added line #L1 was not covered by tests

import { useBlockListContext } from '../BlockListContext';
import { useBlockListWebSocket2 } from '../Sockets/useBlockListWebSocket2';
import { useBlockListWebSocket } from '../Sockets/useBlockListWebSocket';
import { BlockListData, generateBlockList, mergeBlockLists, waitForFadeAnimation } from '../utils';
import { useBlocksGroupedInitialBlockList } from './useBlocksPageGroupedInitialBlockList';

Check warning on line 6 in src/app/_components/BlockList/data/useBlocksPageBlockListGrouped.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/data/useBlocksPageBlockListGrouped.tsx#L3-L6

Added lines #L3 - L6 were not covered by tests

Expand All @@ -28,7 +28,7 @@ export function useBlocksPageBlockListGrouped(btcBlockLimit: number = 3) {
latestStxBlocks: latestStxBlocksFromWebSocket,
latestStxBlocksCount: latestStxBlocksCountFromWebSocket,
clearLatestStxBlocks: clearLatestStxBlocksFromWebSocket,
} = useBlockListWebSocket2(liveUpdates, initialStxBlockHashes);
} = useBlockListWebSocket(liveUpdates, initialStxBlockHashes);

Check warning on line 31 in src/app/_components/BlockList/data/useBlocksPageBlockListGrouped.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/data/useBlocksPageBlockListGrouped.tsx#L31

Added line #L31 was not covered by tests

// manually update the block list with block list updates from the websocket
const updateBlockListManually = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useCallback, useEffect, useRef, useState } from 'react';

Check warning on line 3 in src/app/_components/BlockList/data/useBlocksPageBlockListUngrouped.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/data/useBlocksPageBlockListUngrouped.ts#L3

Added line #L3 was not covered by tests

import { useBlockListContext } from '../BlockListContext';
import { useBlockListWebSocket2 } from '../Sockets/useBlockListWebSocket2';
import { useBlockListWebSocket } from '../Sockets/useBlockListWebSocket';
import { BlockListData, generateBlockList, mergeBlockLists, waitForFadeAnimation } from '../utils';
import { useBlocksPageUngroupedInitialBlockList } from './useBlocksPageUngroupedInitialBlockList';

Check warning on line 8 in src/app/_components/BlockList/data/useBlocksPageBlockListUngrouped.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/data/useBlocksPageBlockListUngrouped.ts#L5-L8

Added lines #L5 - L8 were not covered by tests

Expand All @@ -30,7 +30,7 @@ export function useBlocksPageBlockListUngrouped(btcBlockLimit: number = 3) {
latestStxBlocks: latestStxBlocksFromWebSocket,
latestStxBlocksCount: latestStxBlocksCountFromWebSocket,
clearLatestStxBlocks: clearLatestStxBlocksFromWebSocket,
} = useBlockListWebSocket2(liveUpdates, initialStxBlocksHashes);
} = useBlockListWebSocket(liveUpdates, initialStxBlocksHashes);

Check warning on line 33 in src/app/_components/BlockList/data/useBlocksPageBlockListUngrouped.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/data/useBlocksPageBlockListUngrouped.ts#L33

Added line #L33 was not covered by tests

// manually update the block list with block list updates from the websocket
const updateBlockListManually = useCallback(
Expand Down
Loading

0 comments on commit da76fb7

Please sign in to comment.