Commit 33c0a4e8 authored by chenqikuai's avatar chenqikuai

perf:优化网络请求,请求成功后再进行请求

parent 0edca08d
...@@ -16,6 +16,13 @@ import { ...@@ -16,6 +16,13 @@ import {
import { icustomizedTx } from "@/types/trade"; import { icustomizedTx } from "@/types/trade";
import { getNodeNum } from "@/service/blockChainBrowser"; import { getNodeNum } from "@/service/blockChainBrowser";
import {
clearTimeoutFromTimerObj,
registerTimeoutToTimerObj,
} from "@/utils/common";
const TimerObj = {} as { [key: string]: number };
const __timeout = 10 * 1e3;
export default Vue.extend({ export default Vue.extend({
data() { data() {
...@@ -42,26 +49,22 @@ export default Vue.extend({ ...@@ -42,26 +49,22 @@ export default Vue.extend({
this.fetchTpsData(); this.fetchTpsData();
this.init(); this.init();
this.fetchLatestTxListData(); this.fetchLatestTxListData();
getNodeNum().then((res) => { this.fetchNodeNum();
if (res.error === null) {
this.nodeNum = (res.result.countries || []).reduce((prev, cur) => {
return prev + cur.value;
}, 0);
}
});
this.timer = window.setInterval(() => {
this.fetchAllPos33TicketCountData();
this.fetchAccountCountData();
this.fetchTpsData();
this.init();
this.fetchLatestTxListData();
}, 15 * 1000);
}, },
beforeDestroy() { beforeDestroy() {
window.clearInterval(this.timer); clearTimeoutFromTimerObj(TimerObj);
}, },
methods: { methods: {
fetchNodeNum() {
getNodeNum().then((res) => {
if (res.error === null) {
this.nodeNum = (res.result.countries || []).reduce((prev, cur) => {
return prev + cur.value;
}, 0);
registerTimeoutToTimerObj(TimerObj, this.fetchNodeNum, __timeout);
}
});
},
fetchTpsData() { fetchTpsData() {
getTxCountBewteenTimes( getTxCountBewteenTimes(
moment().unix(), moment().unix(),
...@@ -71,6 +74,7 @@ export default Vue.extend({ ...@@ -71,6 +74,7 @@ export default Vue.extend({
).then((res) => { ).then((res) => {
if (res.error === null) { if (res.error === null) {
this.tps = Number((res.result / (24 * 60 * 60)).toFixed(2)); this.tps = Number((res.result / (24 * 60 * 60)).toFixed(2));
registerTimeoutToTimerObj(TimerObj, this.fetchTpsData, __timeout);
} }
}); });
}, },
...@@ -87,6 +91,11 @@ export default Vue.extend({ ...@@ -87,6 +91,11 @@ export default Vue.extend({
amount: i.amount, amount: i.amount,
success: i.success, success: i.success,
})); }));
registerTimeoutToTimerObj(
TimerObj,
this.fetchLatestTxListData,
__timeout
);
} }
}); });
}, },
...@@ -94,6 +103,11 @@ export default Vue.extend({ ...@@ -94,6 +103,11 @@ export default Vue.extend({
getAccountCount().then((ret) => { getAccountCount().then((ret) => {
if (ret.error === null) { if (ret.error === null) {
this.addressNumber = ret.result; this.addressNumber = ret.result;
registerTimeoutToTimerObj(
TimerObj,
this.fetchAccountCountData,
__timeout
);
} }
}); });
}, },
...@@ -101,6 +115,11 @@ export default Vue.extend({ ...@@ -101,6 +115,11 @@ export default Vue.extend({
getAllPos33TicketCount().then((ret) => { getAllPos33TicketCount().then((ret) => {
if (ret.error === null) { if (ret.error === null) {
this.ticketCount = ret.result; this.ticketCount = ret.result;
registerTimeoutToTimerObj(
TimerObj,
this.fetchAllPos33TicketCountData,
__timeout
);
} }
}); });
}, },
...@@ -111,9 +130,6 @@ export default Vue.extend({ ...@@ -111,9 +130,6 @@ export default Vue.extend({
const lastBlock = res.result[1]; const lastBlock = res.result[1];
const perTime = const perTime =
(lastBlock.time - fatherBlock.time) / (this.latestHeight - 725000); (lastBlock.time - fatherBlock.time) / (this.latestHeight - 725000);
// const perTxCount =
// (lastBlock.tx_count - fatherBlock.tx_count) /
// (lastBlock.time - fatherBlock.time);
this.speed = Number(perTime.toFixed(2)); this.speed = Number(perTime.toFixed(2));
} }
}); });
...@@ -186,6 +202,7 @@ export default Vue.extend({ ...@@ -186,6 +202,7 @@ export default Vue.extend({
const totalFeeRet = await getRpc(yccApi).queryTotalFee([str]); const totalFeeRet = await getRpc(yccApi).queryTotalFee([str]);
if (!totalFeeRet.error) { if (!totalFeeRet.error) {
this.totalTx = totalFeeRet.result.txCount; this.totalTx = totalFeeRet.result.txCount;
registerTimeoutToTimerObj(TimerObj, this.init, __timeout);
} }
} }
}, },
......
...@@ -60,3 +60,19 @@ const nFormatter = (num: number, digits = 2) => { ...@@ -60,3 +60,19 @@ const nFormatter = (num: number, digits = 2) => {
}; };
export { tradeAccuracy, nFormatter }; export { tradeAccuracy, nFormatter };
export const registerTimeoutToTimerObj = (
TimerObj: { [key: string]: number },
func: () => any,
delay: number
) => {
TimerObj[func.name] = window.setTimeout(() => {
func();
}, delay);
};
export const clearTimeoutFromTimerObj = (TimerObj: {
[key: string]: number;
}) => {
Object.values(TimerObj).forEach((timer) => window.clearTimeout(timer));
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment