— так компоненты не упадут, даже если кнопка или её node к моменту onDestroy уже не существует сделал ноды менеджеров списка оборудовани, деталей, и аварий Persistent Node, чтобы при смене сцены не сбрасывалось их состояние.
74 lines
2.9 KiB
TypeScript
74 lines
2.9 KiB
TypeScript
// 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') {
|
|
ErrorLazyLoadList.instance.showSensorDetails(data);
|
|
}
|
|
})
|
|
.catch(console.error);
|
|
}
|
|
}
|