yantar/assets/Scripts/stores/SensorDetailManager.ts
2025-05-27 13:58:07 +07:00

75 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// SensorDetailManager.ts
import { _decorator, Component, Node } from 'cc';
import { SensorDetailStore } from './SensorDetailStore';
import { ErrorLazyLoadList } from '../ui/ErrorLazyLoadList';
const { ccclass } = _decorator;
@ccclass('SensorDetailManager')
export class SensorDetailManager extends Component {
private currentSensorName: string = '';
onLoad() {
// Подпишемся только если менеджер существует в сцене
const equipmentManagerNode = this.node.scene?.getChildByName('EquipmentManager');
equipmentManagerNode?.on('showSensorDetails', this.onShowSensorDetails, this);
}
onDestroy() {
// Отпишемся и уберём все расписанные коллбэки
const equipmentManagerNode = this.node.scene?.getChildByName('EquipmentManager');
equipmentManagerNode?.off('showSensorDetails', this.onShowSensorDetails, this);
this.unscheduleAllCallbacks();
}
public onShowSensorDetails(sensorName: string) {
// Если уже показываем детали того же датчика — ничего не делаем
if (this.currentSensorName === sensorName && ErrorLazyLoadList.instance?.mode === 'details') {
return;
}
this.currentSensorName = sensorName;
// Сбрасываем предыдущий интервал обновлений
this.unschedule(this.updateSensorDetails);
// Запросим и отобразим детали
SensorDetailStore.getInstance().fetchSensorDetails(sensorName)
.then(data => {
if (data && ErrorLazyLoadList.instance) {
ErrorLazyLoadList.instance.showSensorDetails(data);
}
})
.catch(console.error);
// Запланируем периодические обновления, если компонент активен
if (this.node.active) {
this.schedule(this.updateSensorDetails, 5);
}
this.node.active = true;
}
onEnable() {
// При включении заново запускаем обновления, если есть текущий датчик
if (this.currentSensorName) {
this.unschedule(this.updateSensorDetails);
this.schedule(this.updateSensorDetails, 5);
}
}
onDisable() {
this.unschedule(this.updateSensorDetails);
}
private updateSensorDetails() {
if (!this.currentSensorName) return;
SensorDetailStore.getInstance()
.fetchSensorDetails(this.currentSensorName)
.then(data => {
if (data && ErrorLazyLoadList.instance?.mode === 'details') {
// Используем updateSensorDetails для обновления без сброса UI
ErrorLazyLoadList.instance.updateSensorDetails(data);
}
})
.catch(console.error);
}
}