| | |
| | | import { ref, reactive, computed, onMounted, onUnmounted, nextTick, readonly } from 'vue' |
| | | import { useWindowSize } from '@vueuse/core' |
| | | import { hash } from 'ohash' |
| | | import { useTableColumns } from './useTableColumns' |
| | | import { TableCache, CacheInvalidationStrategy } from '../../utils/table/tableCache' |
| | | import { |
| | |
| | | const error = ref(null) |
| | | const data = ref([]) |
| | | let abortController = null |
| | | let inflightRequest = null |
| | | let inflightRequestKey = '' |
| | | let cacheCleanupTimer = null |
| | | const searchParams = reactive( |
| | | Object.assign( |
| | | { |
| | | [pageKey]: 1, |
| | | [sizeKey]: 10 |
| | | [sizeKey]: 20 |
| | | }, |
| | | apiParams || {} |
| | | ) |
| | |
| | | cacheUpdateTrigger.value++ |
| | | } |
| | | const fetchData = async (params, useCache = enableCache) => { |
| | | if (abortController) { |
| | | abortController.abort() |
| | | } |
| | | const currentController = new AbortController() |
| | | abortController = currentController |
| | | loadingState.value = 'loading' |
| | | error.value = null |
| | | try { |
| | | let requestParams = Object.assign( |
| | | {}, |
| | | searchParams, |
| | |
| | | }) |
| | | requestParams = filteredParams |
| | | } |
| | | const requestKey = hash(requestParams) |
| | | if (inflightRequest && inflightRequestKey === requestKey) { |
| | | logger.log('复用进行中的请求') |
| | | return inflightRequest |
| | | } |
| | | if (abortController) { |
| | | abortController.abort() |
| | | } |
| | | const currentController = new AbortController() |
| | | abortController = currentController |
| | | loadingState.value = 'loading' |
| | | error.value = null |
| | | const currentRequest = (async () => { |
| | | try { |
| | | if (useCache && cache) { |
| | | const cachedItem = cache.get(requestParams) |
| | | if (cachedItem) { |
| | |
| | | if (abortController === currentController) { |
| | | abortController = null |
| | | } |
| | | if (inflightRequest === currentRequest) { |
| | | inflightRequest = null |
| | | inflightRequestKey = '' |
| | | } |
| | | } |
| | | })() |
| | | inflightRequest = currentRequest |
| | | inflightRequestKey = requestKey |
| | | return currentRequest |
| | | } |
| | | const getData = async (params) => { |
| | | try { |
| | | return await fetchData(params) |