refactor&perf
- proxy config - query export - optimize station card request
This commit is contained in:
54
src/composables/query/station/use-station-list-query.ts
Normal file
54
src/composables/query/station/use-station-list-query.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import type { Station } from '@/apis/domains';
|
||||
import { ndmVerify } from '@/apis/requests';
|
||||
import { useQueryControlStore } from '@/stores/query-control';
|
||||
import { useStationStore } from '@/stores/station';
|
||||
import { getAppEnvConfig } from '@/utils/env';
|
||||
import { useQuery } from '@tanstack/vue-query';
|
||||
import axios from 'axios';
|
||||
import dayjs from 'dayjs';
|
||||
import { storeToRefs } from 'pinia';
|
||||
import { computed } from 'vue';
|
||||
|
||||
export function useStationListQuery() {
|
||||
const stationStore = useStationStore();
|
||||
const { updatedTime, stationList } = storeToRefs(stationStore);
|
||||
const queryControlStore = useQueryControlStore();
|
||||
const { pollingEnabled } = storeToRefs(queryControlStore);
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['station-list'],
|
||||
enabled: computed(() => pollingEnabled.value),
|
||||
queryFn: async () => {
|
||||
const { data: ndmStationList } = await axios.get<{ code: string; name: string }[]>(`/minio/ndm/ndm-stations.json?_t=${dayjs().unix()}`);
|
||||
|
||||
let stations = ndmStationList.map<Station>((station) => ({
|
||||
code: station.code ?? '',
|
||||
name: station.name ?? '',
|
||||
online: false,
|
||||
}));
|
||||
|
||||
const pingResultList = await Promise.allSettled(stations.map((station) => ndmVerify(station.code)));
|
||||
|
||||
stations = stations.map((station, index) => ({
|
||||
...station,
|
||||
online: pingResultList[index].status === 'fulfilled',
|
||||
}));
|
||||
|
||||
const isSame =
|
||||
stationList.value.length === stations.length &&
|
||||
stationList.value.every((oldStation, index) => {
|
||||
const newStation = stations[index];
|
||||
return oldStation.code === newStation.code && oldStation.name === newStation.name && oldStation.online === newStation.online;
|
||||
});
|
||||
|
||||
if (!isSame) {
|
||||
stationList.value.splice(0, stationList.value.length, ...stations);
|
||||
}
|
||||
|
||||
updatedTime.value = dayjs().toJSON();
|
||||
|
||||
return stations;
|
||||
},
|
||||
refetchInterval: getAppEnvConfig().requestInterval * 1000,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user