refactor: 抽离未读告警状态,不再持久化

This commit is contained in:
yangsy
2026-01-15 13:58:13 +08:00
parent de35075be8
commit b59b34e68b
9 changed files with 67 additions and 49 deletions

View File

@@ -1,8 +1,7 @@
import { initStationAlarms, type LineAlarms, type NdmDeviceAlarmLogResultVO, type Station, type StationAlarms } from '@/apis';
import { type LineAlarms, type Station, type StationAlarms } from '@/apis';
import { NDM_ALARM_STORE_ID } from '@/constants';
import { tryGetDeviceType } from '@/enums';
import { defineStore } from 'pinia';
import { computed, shallowRef, triggerRef } from 'vue';
import { shallowRef, triggerRef } from 'vue';
export const useAlarmStore = defineStore(
NDM_ALARM_STORE_ID,
@@ -18,42 +17,10 @@ export const useAlarmStore = defineStore(
triggerRef(lineAlarms);
};
// 全线所有车站的未读告警 (来自stomp订阅)
const unreadLineAlarms = shallowRef<LineAlarms>({});
const unreadAlarmCount = computed(() => {
let count = 0;
Object.values(unreadLineAlarms.value).forEach((stationAlarms) => {
count += stationAlarms['unclassified'].length;
});
return count;
});
const pushUnreadAlarm = (alarm: NdmDeviceAlarmLogResultVO) => {
const stationCode = alarm.stationCode;
if (!stationCode) return;
if (!unreadLineAlarms.value[stationCode]) {
unreadLineAlarms.value[stationCode] = initStationAlarms();
}
const deviceType = tryGetDeviceType(alarm.deviceType);
if (!deviceType) return;
const stationAlarms = unreadLineAlarms.value[stationCode];
stationAlarms[deviceType].push(alarm);
stationAlarms['unclassified'].push(alarm);
triggerRef(unreadLineAlarms);
};
const clearUnreadAlarms = () => {
unreadLineAlarms.value = {};
triggerRef(unreadLineAlarms);
};
return {
lineAlarms,
setLineAlarms,
setStationAlarms,
unreadLineAlarms,
unreadAlarmCount,
pushUnreadAlarm,
clearUnreadAlarms,
};
},
{

View File

@@ -3,4 +3,5 @@ export * from './device';
export * from './polling';
export * from './setting';
export * from './station';
export * from './unread';
export * from './user';

44
src/stores/unread.ts Normal file
View File

@@ -0,0 +1,44 @@
import { initStationAlarms, type LineAlarms, type NdmDeviceAlarmLogResultVO } from '@/apis';
import { NDM_UNREAD_STORE_ID } from '@/constants';
import { tryGetDeviceType } from '@/enums';
import { defineStore } from 'pinia';
import { computed, shallowRef, triggerRef } from 'vue';
export const useUnreadStore = defineStore(NDM_UNREAD_STORE_ID, () => {
// 全线所有车站的未读告警 (来自stomp订阅)
const unreadLineAlarms = shallowRef<LineAlarms>({});
const unreadAlarmCount = computed(() => {
let count = 0;
Object.values(unreadLineAlarms.value).forEach((stationAlarms) => {
count += stationAlarms['unclassified'].length;
});
return count;
});
const pushUnreadAlarm = (alarm: NdmDeviceAlarmLogResultVO) => {
const stationCode = alarm.stationCode;
if (!stationCode) return;
if (!unreadLineAlarms.value[stationCode]) {
unreadLineAlarms.value[stationCode] = initStationAlarms();
}
const deviceType = tryGetDeviceType(alarm.deviceType);
if (!deviceType) return;
const stationAlarms = unreadLineAlarms.value[stationCode];
stationAlarms[deviceType].push(alarm);
stationAlarms['unclassified'].push(alarm);
triggerRef(unreadLineAlarms);
};
const clearUnreadAlarms = () => {
unreadLineAlarms.value = {};
triggerRef(unreadLineAlarms);
};
return {
unreadLineAlarms,
unreadAlarmCount,
pushUnreadAlarm,
clearUnreadAlarms,
};
});