Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(entities-shared): persistent cache key should share the same refs #1844

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading