1- import Matter from "matter-js" ;
21import { FunctionalComponent } from "preact" ;
3- import { useEffect , useRef , useState } from "preact/hooks" ;
4- import { ChainService , Transaction } from "../../service/api" ;
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 { Transaction } from "../../service/type " ;
76
87type TxStatus = "pending" | "proposing" | "proposed" | "committed" | "none" ;
98
@@ -14,8 +13,6 @@ interface TxChange {
1413}
1514
1615const Pool : FunctionalComponent = ( ) => {
17- const [ blockHeader , setBlockHeader ] = useState ( undefined ) ;
18-
1916 const [ initProposedTxs , setInitProposedTxs ] = useState <
2017 Transaction [ ] | null
2118 > ( null ) ;
@@ -43,64 +40,62 @@ const Pool: FunctionalComponent = () => {
4340
4441 const [ stateChanges , setStateChanges ] = useState < TxChange [ ] > ( [ ] ) ; // 存储状态变化信息
4542
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- } ;
43+ const subNewSnapshot = async ( ) => {
44+ ChainService . subscribeNewSnapshot ( ( newSnapshot ) => {
45+ const {
46+ tipCommittedTransactions,
47+ pendingTransactions,
48+ proposingTransactions,
49+ proposedTransactions,
50+ } = newSnapshot ;
51+
52+ if ( initCommittedTxs == null ) {
53+ setInitCommittedTxs ( tipCommittedTransactions ) ;
54+ }
55+ if ( initProposedTxs == null ) {
56+ setInitProposedTxs ( proposedTransactions ) ;
57+ }
58+ if ( initPendingTxs == null ) {
59+ setInitPendingTxs ( pendingTransactions ) ;
60+ }
61+ if ( initProposingTxs == null ) {
62+ setInitProposingTxs ( proposingTransactions ) ;
63+ }
7564
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 ) ;
65+ // 拿到所有tx
66+ const newTxs = {
67+ pending : pendingTransactions ,
68+ proposing : proposingTransactions ,
69+ proposed : proposedTransactions ,
70+ committed : tipCommittedTransactions ,
71+ } ;
72+
73+ // diff 数据
74+ if ( previousTxs ) {
75+ const changes = detectStateChanges ( previousTxs , newTxs ) ;
8776 setStateChanges ( changes ) ;
8877 }
89- return newTxs ;
90- } ) ;
91-
92- setPendingTxs ( pendingTxs ) ;
9378
94- setProposingTxs ( proposingTxs ) ;
95- setProposedTxs ( proposedTransactions ) ;
96-
97- setCommittedTxs ( tipBlockTxs . committedTransactions ) ;
98- setBlockHeader ( tipBlockTxs . blockHeader ) ;
79+ // 更新历史记录
80+ // 使用函数式更新
81+ setPreviousTxs ( ( prev ) => {
82+ if ( prev ) {
83+ const changes = detectStateChanges ( prev , newTxs ) ;
84+ setStateChanges ( changes ) ;
85+ }
86+ return newTxs ;
87+ } ) ;
88+
89+ setPendingTxs ( pendingTransactions ) ;
90+ setProposingTxs ( proposingTransactions ) ;
91+ setProposedTxs ( proposedTransactions ) ;
92+ setCommittedTxs ( tipCommittedTransactions ) ;
93+ } ) ;
9994 } ;
10095
10196 useEffect ( ( ) => {
102- const task = setInterval ( fetchData , 1000 ) ;
103- return ( ) => clearInterval ( task ) ;
97+ ChainService . wsClient . connect ( ) ;
98+ subNewSnapshot ( ) ;
10499 } , [ ] ) ;
105100
106101 // 状态变化检测
0 commit comments