generated from stacks-network/.github
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blocklist): renamed socket files
- Loading branch information
Showing
12 changed files
with
123 additions
and
123 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
80 changes: 25 additions & 55 deletions
80
src/app/_components/BlockList/Sockets/useBlockListWebSocket.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 |
---|---|---|
@@ -1,69 +1,39 @@ | ||
import { useCallback, useRef, useState } from 'react'; | ||
import { useCallback, useEffect, useRef, useState } from 'react'; | ||
|
||
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'; | ||
|
||
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)[]>([]); | ||
|
||
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)); | ||
// update ref when initialStxBlockHashes changes | ||
useEffect(() => { | ||
stxBlockHashes.current = new Set(initialStxBlockHashes); | ||
}, [initialStxBlockHashes]); | ||
|
||
updateLatestBlocks(); | ||
}, | ||
[initialBurnBlockHashes, initialBlockHashes] | ||
); | ||
const handleBlock = useCallback((stxBlock: NakamotoBlock | Block) => { | ||
// If the block is already in the list, don't add it again | ||
if (stxBlockHashes.current.has(stxBlock.hash)) { | ||
return; | ||
} | ||
|
||
useSubscribeBlocks(handleBlock); | ||
// Otherwise, add it to the list | ||
setLatestStxBlocks(prevLatestStxBlocks => [stxBlock, ...prevLatestStxBlocks]); | ||
stxBlockHashes.current.add(stxBlock.hash); | ||
}, []); | ||
|
||
const clearLatestBlocks = () => { | ||
setLatestBlocks([]); | ||
useSubscribeBlocks(liveUpdates, handleBlock); | ||
|
||
const clearLatestStxBlocks = () => { | ||
setLatestStxBlocks([]); | ||
}; | ||
|
||
return { | ||
latestUIBlocks: latestBlocks, | ||
latestBlock, | ||
latestBlocksCount: latestBlocks.filter(block => block.type === UIBlockType.Block).length, | ||
clearLatestBlocks, | ||
latestStxBlocks, | ||
latestStxBlocksCount: latestStxBlocks.length, | ||
clearLatestStxBlocks, | ||
}; | ||
} |
39 changes: 0 additions & 39 deletions
39
src/app/_components/BlockList/Sockets/useBlockListWebSocket2.ts
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
src/app/_components/BlockList/Sockets/useBlockListWebSocketUIBlock.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,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, | ||
}; | ||
} |
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
Oops, something went wrong.