diff --git a/entry/src/main/ets/ai-pick/AiPickNodeComponent.ets b/entry/src/main/ets/ai-pick/AiPickNodeComponent.ets index 852748a..cf0c1fe 100644 --- a/entry/src/main/ets/ai-pick/AiPickNodeComponent.ets +++ b/entry/src/main/ets/ai-pick/AiPickNodeComponent.ets @@ -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 { 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 { diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index a5184de..6afe070 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -21,7 +21,9 @@ struct Index { }) } Card() { - AiPickNodeComponent() + AiPickNodeComponent({ + refreshTrigger: $refreshTrigger, + }) } } .width('100%')