1- import Matter from "matter-js" ;
21import { FunctionalComponent } from "preact" ;
3- import { useEffect , useRef , useState } from "preact/hooks" ;
4- import { ChainService , Transaction } from "../../service/chain" ;
5- import PoolScene from "./scene" ;
6- import QueueComponent from "./motion" ;
2+ import { useEffect , useState } from "preact/hooks" ;
3+ import { ChainService } from "../../service/api" ;
4+ import QueueComponent from "./queue" ;
5+ import { Network , Transaction } from "../../service/type" ;
6+ import { useAtomValue } from "jotai" ;
7+ import { ChainTheme , chainThemeAtom } from "../../states/atoms" ;
78
89type TxStatus = "pending" | "proposing" | "proposed" | "committed" | "none" ;
910
@@ -14,7 +15,7 @@ interface TxChange {
1415}
1516
1617const Pool : FunctionalComponent = ( ) => {
17- const [ blockHeader , setBlockHeader ] = useState ( undefined ) ;
18+ const chainTheme = useAtomValue ( chainThemeAtom ) ;
1819
1920 const [ initProposedTxs , setInitProposedTxs ] = useState <
2021 Transaction [ ] | null
@@ -43,65 +44,69 @@ const Pool: FunctionalComponent = () => {
4344
4445 const [ stateChanges , setStateChanges ] = useState < TxChange [ ] > ( [ ] ) ; // 存储状态变化信息
4546
46- const fetchData = async ( ) => {
47- const [ tipBlockTxs , pendingTxs , proposingTxs , proposedTransactions ] =
48- await Promise . all ( [
49- ChainService . getTipBlockTransactions ( ) ,
50- ChainService . getPendingTransactions ( ) ,
51- ChainService . getProposingTransactions ( ) ,
52- ChainService . getProposedTransactions ( ) ,
53- ] ) ;
54-
55- if ( initCommittedTxs == null ) {
56- setInitCommittedTxs ( tipBlockTxs . committedTransactions ) ;
57- }
58- if ( initProposedTxs == null ) {
59- setInitProposedTxs ( proposedTransactions ) ;
60- }
61- if ( initPendingTxs == null ) {
62- setInitPendingTxs ( pendingTxs ) ;
63- }
64- if ( initProposingTxs == null ) {
65- setInitProposingTxs ( proposingTxs ) ;
66- }
67-
68- // 拿到所有tx
69- const newTxs = {
70- pending : pendingTxs ,
71- proposing : proposingTxs ,
72- proposed : proposedTransactions ,
73- committed : tipBlockTxs . committedTransactions ,
74- } ;
47+ const subNewSnapshot = async ( ) => {
48+ const network =
49+ chainTheme === ChainTheme . mainnet
50+ ? Network . Mainnet
51+ : Network . Testnet ;
52+ const chainService = new ChainService ( network ) ;
53+ chainService . wsClient . connect ( ( ) => {
54+ chainService . subscribeNewSnapshot ( ( newSnapshot ) => {
55+ const {
56+ tipCommittedTransactions,
57+ pendingTransactions,
58+ proposingTransactions,
59+ proposedTransactions,
60+ } = newSnapshot ;
61+
62+ if ( initCommittedTxs == null ) {
63+ setInitCommittedTxs ( tipCommittedTransactions ) ;
64+ }
65+ if ( initProposedTxs == null ) {
66+ setInitProposedTxs ( proposedTransactions ) ;
67+ }
68+ if ( initPendingTxs == null ) {
69+ setInitPendingTxs ( pendingTransactions ) ;
70+ }
71+ if ( initProposingTxs == null ) {
72+ setInitProposingTxs ( proposingTransactions ) ;
73+ }
74+
75+ // 拿到所有tx
76+ const newTxs = {
77+ pending : pendingTransactions ,
78+ proposing : proposingTransactions ,
79+ proposed : proposedTransactions ,
80+ committed : tipCommittedTransactions ,
81+ } ;
82+
83+ // diff 数据
84+ if ( previousTxs ) {
85+ const changes = detectStateChanges ( previousTxs , newTxs ) ;
86+ setStateChanges ( changes ) ;
87+ }
88+
89+ // 更新历史记录
90+ // 使用函数式更新
91+ setPreviousTxs ( ( prev ) => {
92+ if ( prev ) {
93+ const changes = detectStateChanges ( prev , newTxs ) ;
94+ setStateChanges ( changes ) ;
95+ }
96+ return newTxs ;
97+ } ) ;
7598
76- // diff 数据
77- if ( previousTxs ) {
78- const changes = detectStateChanges ( previousTxs , newTxs ) ;
79- setStateChanges ( changes ) ;
80- }
81-
82- // 更新历史记录
83- // 使用函数式更新
84- setPreviousTxs ( ( prev ) => {
85- if ( prev ) {
86- const changes = detectStateChanges ( prev , newTxs ) ;
87- setStateChanges ( changes ) ;
88- }
89- return newTxs ;
99+ setPendingTxs ( pendingTransactions ) ;
100+ setProposingTxs ( proposingTransactions ) ;
101+ setProposedTxs ( proposedTransactions ) ;
102+ setCommittedTxs ( tipCommittedTransactions ) ;
103+ } ) ;
90104 } ) ;
91-
92- setPendingTxs ( pendingTxs ) ;
93-
94- setProposingTxs ( proposingTxs ) ;
95- setProposedTxs ( proposedTransactions ) ;
96-
97- setCommittedTxs ( tipBlockTxs . committedTransactions ) ;
98- setBlockHeader ( tipBlockTxs . blockHeader ) ;
99105 } ;
100106
101107 useEffect ( ( ) => {
102- const task = setInterval ( fetchData , 1000 ) ;
103- return ( ) => clearInterval ( task ) ;
104- } , [ ] ) ;
108+ subNewSnapshot ( ) ;
109+ } , [ chainTheme ] ) ;
105110
106111 // 状态变化检测
107112 const detectStateChanges = (
0 commit comments