Skip to content

Commit

Permalink
fix(entities-shared): persistent cache key should share the same ref,…
Browse files Browse the repository at this point in the history
… expose useFetcherCacheKey (#1844)
  • Loading branch information
Justineo authored Dec 11, 2024
1 parent eba3e34 commit 117c96c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
3 changes: 2 additions & 1 deletion packages/entities/entities-shared/src/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import useDebouncedFilter from './useDebouncedFilter'
import useDeleteUrlBuilder from './useDeleteUrlBuilder'
import useErrors from './useErrors'
import useExternalLinkCreator from './useExternalLinkCreator'
import useFetcher from './useFetcher'
import useFetcher, { useFetcherCacheKey } from './useFetcher'
import useFetchUrlBuilder from './useFetchUrlBuilder'
import useHelpers from './useHelpers'
import useStringHelpers from './useStringHelpers'
Expand All @@ -21,6 +21,7 @@ export default {
useErrors,
useExternalLinkCreator,
useFetcher,
useFetcherCacheKey,
useFetchUrlBuilder,
useHelpers,
useStringHelpers,
Expand Down
30 changes: 15 additions & 15 deletions packages/entities/entities-shared/src/composables/useFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ref, toValue, unref, watch } from 'vue'
import type { MaybeRefOrGetter } from 'vue'
import { ref, toValue, unref } from 'vue'
import type { MaybeRefOrGetter, Ref } from 'vue'
import type {
FetcherResponse,
FetcherState,
Expand All @@ -13,7 +13,7 @@ import useFetchUrlBuilder from './useFetchUrlBuilder'
import type { TableDataFetcherParams } from '@kong/kongponents'

// Store cacheIdentifier => fetcherCacheKey globally so that the cache is persisted during the session
const cacheKeys = new Map<string, number>()
const cacheKeys = new Map<string, Ref<number>>()

export default function useFetcher(
config: KonnectBaseTableConfig | KongManagerBaseTableConfig,
Expand Down Expand Up @@ -125,20 +125,20 @@ export default function useFetcher(
}

const cacheId = config.cacheIdentifier
const fetcherCacheKey = ref<number>(1)
if (cacheId) {
if (cacheKeys.has(cacheId)) {
fetcherCacheKey.value = cacheKeys.get(cacheId)!
} else {
cacheKeys.set(cacheId, fetcherCacheKey.value)
}
}
const fetcherCacheKey = useFetcherCacheKey(cacheId)

return { fetcher, fetcherState: state, fetcherCacheKey }
}

watch(fetcherCacheKey, (key) => {
if (cacheId) {
export function useFetcherCacheKey(cacheId?: string) {
if (cacheId) {
let key = cacheKeys.get(cacheId)
if (!key) {
key = ref(1)
cacheKeys.set(cacheId, key)
}
})
return key
}

return { fetcher, fetcherState: state, fetcherCacheKey }
return ref(1)
}

0 comments on commit 117c96c

Please sign in to comment.