feat(ai-pick): 完善行情订阅生命周期

This commit is contained in:
clz
2026-07-24 16:42:17 +08:00
parent 0bff3eeb6b
commit 888f7e316c
2 changed files with 51 additions and 2 deletions
@@ -9,6 +9,11 @@ import { AllCardsCard } from '../common/AllCardsModels';
import { AiPickDataFetcher } from './AiPickDataFetcher';
import { AiPickHqRequestClient } from './AiPickHqRequestClient';
import { cellColor, cellValue, formatColName } from './AiPickUtils';
import {
emitter,
EVENT_TCP_RECONNECT,
EventListener,
} from '../common/EventEmitter';
import {
EMPTY_IMAGE_DARK,
EMPTY_IMAGE_LIGHT,
@@ -35,7 +40,13 @@ export struct AiPickNodeComponent {
@State tabContentWidth: number = 0;
@State tabScrollOffset: number = 0;
@State showExplainSheet: boolean = false;
@Link @Watch('onRefreshTriggerChange') refreshTrigger: number;
@Consume('firstPageVisible') @Watch('onFirstPageVisibleChange')
firstPageVisible: boolean = true;
private hqUnsubscribe: (() => void) | undefined;
private tcpReconnectListener: EventListener = (): void => {
this.onTcpReconnect();
};
private showTabGradient(): boolean {
const maxOffset = this.tabContentWidth - this.tabViewportWidth;
@@ -44,12 +55,17 @@ export struct AiPickNodeComponent {
aboutToAppear(): void {
this.updateCardConfig();
emitter.on(EVENT_TCP_RECONNECT, this.tcpReconnectListener);
if (!this.firstPageVisible) {
return;
}
setTimeout(() => {
this.updateCardData();
}, 600);
}
aboutToDisappear(): void {
emitter.off(EVENT_TCP_RECONNECT, this.tcpReconnectListener);
this.stopSubscribeHq();
}
@@ -58,6 +74,37 @@ export struct AiPickNodeComponent {
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> {
this.stopSubscribeHq();
@@ -66,7 +113,7 @@ export struct AiPickNodeComponent {
try {
const tabs = await fetcher.fetchCardData();
this.cardData = { tabs };
if (tabs.length > 0) {
if (tabs.length > 0 && this.firstPageVisible) {
this.subscribeHq();
}
} catch {
+3 -1
View File
@@ -21,7 +21,9 @@ struct Index {
})
}
Card() {
AiPickNodeComponent()
AiPickNodeComponent({
refreshTrigger: $refreshTrigger,
})
}
}
.width('100%')