Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit e204994

Browse files
committed
refactor frontend to use websocket
1 parent defdca9 commit e204994

File tree

11 files changed

+279
-912
lines changed

11 files changed

+279
-912
lines changed

frontend/src/components/elevator/car.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Matter from "matter-js";
22
import { useEffect, useRef, useState } from "preact/hooks";
3-
import { BlockHeader, Transaction } from "../../service/api";
3+
import { BlockHeader, Transaction } from "../../service/type";
44
import {
55
boxSizeToMatterSize,
66
carBoxCenterPosX,

frontend/src/components/elevator/index.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import { useEffect, useState } from "preact/hooks";
2-
import { ChainService, TipBlockResponse } from "../../service/api";
2+
import { ChainService } from "../../service/api";
33
import ElevatorCar from "./car";
44
import ElevatorUpButton from "./up-btn";
55
import ElevatorPanel from "./panel";
66
import ElevatorHeader from "./header";
77
import { useAtomValue } from "jotai";
88
import { ChainTheme, chainThemeAtom } from "../../states/atoms";
9+
import { TipBlockResponse } from "../../service/type";
910

1011
export default function Elevator() {
1112
const chainTheme = useAtomValue(chainThemeAtom);
1213
const [tipBlock, setTipBlock] = useState<TipBlockResponse>(undefined);
1314
const [doorClosing, setDoorClosing] = useState(false);
1415

15-
// Update effect to fetch all data
16-
const fetch = async () => {
16+
// subscribe to new block
17+
const subNewBlock = async () => {
1718
ChainService.subscribeNewBlock((newBlock) => {
1819
if (newBlock.blockHeader) {
1920
setTipBlock((prev) => {
@@ -31,7 +32,9 @@ export default function Elevator() {
3132
});
3233
};
3334
useEffect(() => {
34-
fetch();
35+
ChainService.wsClient.connect(() => {
36+
subNewBlock();
37+
});
3538
}, []);
3639

3740
const bgElevatorFrame =

frontend/src/components/pool/index.tsx

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import Matter from "matter-js";
21
import { 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

87
type TxStatus = "pending" | "proposing" | "proposed" | "committed" | "none";
98

@@ -14,8 +13,6 @@ interface TxChange {
1413
}
1514

1615
const 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

Comments
 (0)