refactor: add store to save data from queries progressively
This commit is contained in:
@@ -8,6 +8,7 @@ import dayjs from 'dayjs';
|
||||
import { storeToRefs } from 'pinia';
|
||||
import { computed } from 'vue';
|
||||
import type { StationAlarms } from './domains';
|
||||
import { useLineAlarmsStore } from '@/stores/line-alarms';
|
||||
|
||||
export interface LineAlarms {
|
||||
[stationCode: Station['code']]: StationAlarms;
|
||||
@@ -15,24 +16,29 @@ export interface LineAlarms {
|
||||
|
||||
export function useLineAlarmsQuery() {
|
||||
const stationStore = useStationStore();
|
||||
const { updatedTime, stationList, onlineStationList } = storeToRefs(stationStore);
|
||||
const { stationList, onlineStationList } = storeToRefs(stationStore);
|
||||
const queryControlStore = useQueryControlStore();
|
||||
const { pollingEnabled } = storeToRefs(queryControlStore);
|
||||
const lineAlarmsStore = useLineAlarmsStore();
|
||||
const { lineAlarms } = storeToRefs(lineAlarmsStore);
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['line-alarms', updatedTime],
|
||||
queryKey: ['line-alarms'],
|
||||
enabled: computed(() => onlineStationList.value.length > 0 && pollingEnabled.value),
|
||||
staleTime: Infinity,
|
||||
refetchOnMount: false,
|
||||
refetchOnReconnect: false,
|
||||
refetchOnWindowFocus: false,
|
||||
queryFn: async ({ signal }): Promise<LineAlarms> => {
|
||||
// console.time('useLineAlarmsQuery');
|
||||
|
||||
const lineAlarms: LineAlarms = {};
|
||||
|
||||
if (!stationList?.value) {
|
||||
return lineAlarms;
|
||||
lineAlarms.value = {};
|
||||
return lineAlarms.value;
|
||||
}
|
||||
|
||||
for (const station of stationList.value) {
|
||||
lineAlarms[station.code] = {
|
||||
lineAlarms.value[station.code] = {
|
||||
[DeviceType.Camera]: [],
|
||||
[DeviceType.Decoder]: [],
|
||||
[DeviceType.Keyboard]: [],
|
||||
@@ -71,7 +77,7 @@ export function useLineAlarmsQuery() {
|
||||
const securityBoxAlarms = alarmList.filter((device) => device.deviceType === DeviceType.SecurityBox);
|
||||
const switchAlarms = alarmList.filter((device) => device.deviceType === DeviceType.Switch);
|
||||
const videoServerAlarms = alarmList.filter((device) => device.deviceType === DeviceType.VideoServer);
|
||||
lineAlarms[station.code] = {
|
||||
lineAlarms.value[station.code] = {
|
||||
[DeviceType.Camera]: cameraAlarms,
|
||||
[DeviceType.Decoder]: decoderAlarms,
|
||||
[DeviceType.Keyboard]: keyboardAlarms,
|
||||
@@ -89,7 +95,7 @@ export function useLineAlarmsQuery() {
|
||||
|
||||
// console.timeEnd('useLineAlarmsQuery');
|
||||
|
||||
return lineAlarms;
|
||||
return lineAlarms.value;
|
||||
},
|
||||
placeholderData: (prev) => prev,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user