feat(ai-pick): 完善行情订阅生命周期
This commit is contained in:
@@ -9,6 +9,11 @@ import { AllCardsCard } from '../common/AllCardsModels';
|
|||||||
import { AiPickDataFetcher } from './AiPickDataFetcher';
|
import { AiPickDataFetcher } from './AiPickDataFetcher';
|
||||||
import { AiPickHqRequestClient } from './AiPickHqRequestClient';
|
import { AiPickHqRequestClient } from './AiPickHqRequestClient';
|
||||||
import { cellColor, cellValue, formatColName } from './AiPickUtils';
|
import { cellColor, cellValue, formatColName } from './AiPickUtils';
|
||||||
|
import {
|
||||||
|
emitter,
|
||||||
|
EVENT_TCP_RECONNECT,
|
||||||
|
EventListener,
|
||||||
|
} from '../common/EventEmitter';
|
||||||
import {
|
import {
|
||||||
EMPTY_IMAGE_DARK,
|
EMPTY_IMAGE_DARK,
|
||||||
EMPTY_IMAGE_LIGHT,
|
EMPTY_IMAGE_LIGHT,
|
||||||
@@ -35,7 +40,13 @@ export struct AiPickNodeComponent {
|
|||||||
@State tabContentWidth: number = 0;
|
@State tabContentWidth: number = 0;
|
||||||
@State tabScrollOffset: number = 0;
|
@State tabScrollOffset: number = 0;
|
||||||
@State showExplainSheet: boolean = false;
|
@State showExplainSheet: boolean = false;
|
||||||
|
@Link @Watch('onRefreshTriggerChange') refreshTrigger: number;
|
||||||
|
@Consume('firstPageVisible') @Watch('onFirstPageVisibleChange')
|
||||||
|
firstPageVisible: boolean = true;
|
||||||
private hqUnsubscribe: (() => void) | undefined;
|
private hqUnsubscribe: (() => void) | undefined;
|
||||||
|
private tcpReconnectListener: EventListener = (): void => {
|
||||||
|
this.onTcpReconnect();
|
||||||
|
};
|
||||||
|
|
||||||
private showTabGradient(): boolean {
|
private showTabGradient(): boolean {
|
||||||
const maxOffset = this.tabContentWidth - this.tabViewportWidth;
|
const maxOffset = this.tabContentWidth - this.tabViewportWidth;
|
||||||
@@ -44,12 +55,17 @@ export struct AiPickNodeComponent {
|
|||||||
|
|
||||||
aboutToAppear(): void {
|
aboutToAppear(): void {
|
||||||
this.updateCardConfig();
|
this.updateCardConfig();
|
||||||
|
emitter.on(EVENT_TCP_RECONNECT, this.tcpReconnectListener);
|
||||||
|
if (!this.firstPageVisible) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.updateCardData();
|
this.updateCardData();
|
||||||
}, 600);
|
}, 600);
|
||||||
}
|
}
|
||||||
|
|
||||||
aboutToDisappear(): void {
|
aboutToDisappear(): void {
|
||||||
|
emitter.off(EVENT_TCP_RECONNECT, this.tcpReconnectListener);
|
||||||
this.stopSubscribeHq();
|
this.stopSubscribeHq();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,6 +74,37 @@ export struct AiPickNodeComponent {
|
|||||||
this.cardConfig = AiPickDataFetcher.getInstance().fetchCardConfig();
|
this.cardConfig = AiPickDataFetcher.getInstance().fetchCardConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// refreshTrigger 变化时重新获取策略数据,并在页面可见时重建行情订阅。
|
||||||
|
onRefreshTriggerChange(): void {
|
||||||
|
this.updateCardData();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 页面不可见时停止行情;恢复可见后使用已有策略数据重建订阅。
|
||||||
|
onFirstPageVisibleChange(): void {
|
||||||
|
if (!this.firstPageVisible) {
|
||||||
|
this.stopSubscribeHq();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.cardData.tabs.length > 0) {
|
||||||
|
this.subscribeHq();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.updateCardData();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 收到 TCP 重连事件后,仅在页面可见时重新订阅行情。
|
||||||
|
private onTcpReconnect(): void {
|
||||||
|
if (!this.firstPageVisible) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.stopSubscribeHq();
|
||||||
|
if (this.cardData.tabs.length > 0) {
|
||||||
|
this.subscribeHq();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.updateCardData();
|
||||||
|
}
|
||||||
|
|
||||||
// 只负责请求并更新卡片展示数据。
|
// 只负责请求并更新卡片展示数据。
|
||||||
private async updateCardData(): Promise<void> {
|
private async updateCardData(): Promise<void> {
|
||||||
this.stopSubscribeHq();
|
this.stopSubscribeHq();
|
||||||
@@ -66,7 +113,7 @@ export struct AiPickNodeComponent {
|
|||||||
try {
|
try {
|
||||||
const tabs = await fetcher.fetchCardData();
|
const tabs = await fetcher.fetchCardData();
|
||||||
this.cardData = { tabs };
|
this.cardData = { tabs };
|
||||||
if (tabs.length > 0) {
|
if (tabs.length > 0 && this.firstPageVisible) {
|
||||||
this.subscribeHq();
|
this.subscribeHq();
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
@@ -21,7 +21,9 @@ struct Index {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
Card() {
|
Card() {
|
||||||
AiPickNodeComponent()
|
AiPickNodeComponent({
|
||||||
|
refreshTrigger: $refreshTrigger,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.width('100%')
|
.width('100%')
|
||||||
|
|||||||
Reference in New Issue
Block a user