Skip to content

Commit

Permalink
fix: avoid computed warns
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Feb 19, 2024
1 parent 294797f commit c11ee2f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
23 changes: 11 additions & 12 deletions src/query-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ export const queryEntry_toString = <TResult, TError>(
export const QUERY_STORE_ID = '_pc_query'

export const useQueryCache = defineStore(QUERY_STORE_ID, () => {
const entryRegistry = shallowReactive(
new TreeMapNode<UseQueryEntry<unknown, unknown>>()
)
// We have two versions of the cache, one that track changes and another that doesn't so the actions can be used
// inside computed properties
const cachesRaw = new TreeMapNode<UseQueryEntry<unknown, unknown>>()
const caches = shallowReactive(cachesRaw)

// this allows use to attach reactive effects to the scope later on
const scope = getCurrentScope()!
Expand All @@ -127,11 +128,9 @@ export const useQueryCache = defineStore(QUERY_STORE_ID, () => {
const key = keyRaw.map(stringifyFlatObject)
// ensure the state
// console.log('⚙️ Ensuring entry', key)
let entry = entryRegistry.get(key) as
| UseQueryEntry<TResult, TError>
| undefined
let entry = cachesRaw.get(key) as UseQueryEntry<TResult, TError> | undefined
if (!entry) {
entryRegistry.set(
cachesRaw.set(
key,
(entry = scope.run(() => createQueryEntry(options.initialData?.()))!)
)
Expand Down Expand Up @@ -160,7 +159,7 @@ export const useQueryCache = defineStore(QUERY_STORE_ID, () => {
exact?: boolean
} = {}
) {
const entryNode = entryRegistry.find(key.map(stringifyFlatObject))
const entryNode = cachesRaw.find(key.map(stringifyFlatObject))

// nothing to invalidate
if (!entryNode) {
Expand Down Expand Up @@ -273,7 +272,7 @@ export const useQueryCache = defineStore(QUERY_STORE_ID, () => {
key: UseQueryKey,
data: TResult | ((data: Ref<TResult | undefined>) => void)
) {
const entry = entryRegistry.get(key.map(stringifyFlatObject)) as
const entry = cachesRaw.get(key.map(stringifyFlatObject)) as
| UseQueryEntry<TResult>
| undefined
// TODO: Should it create the entry?
Expand All @@ -295,15 +294,15 @@ export const useQueryCache = defineStore(QUERY_STORE_ID, () => {
function getQueryData<TResult = unknown>(
key: UseQueryKey
): TResult | undefined {
const entry = entryRegistry.get(key.map(stringifyFlatObject)) as
const entry = caches.get(key.map(stringifyFlatObject)) as
| UseQueryEntry<TResult>
| undefined
return entry?.data.value
}

// TODO: find a way to make it possible to prefetch. Right now we need the actual options of the query
function prefetch(key: UseQueryKey) {
const entry = entryRegistry.get(key.map(stringifyFlatObject))
const entry = cachesRaw.get(key.map(stringifyFlatObject))
if (!entry) {
if (process.env.NODE_ENV !== 'production') {
console.warn(
Expand All @@ -316,7 +315,7 @@ export const useQueryCache = defineStore(QUERY_STORE_ID, () => {
}

return {
entryRegistry,
caches,

ensureEntry,
invalidateEntry,
Expand Down
8 changes: 4 additions & 4 deletions src/use-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,12 @@ describe('useQuery', () => {
await runTimers()

const cacheClient = useQueryCache()
expect(entryNodeSize(cacheClient.entryRegistry)).toBe(1)
expect(entryNodeSize(cacheClient.caches)).toBe(1)

mountSimple({ key: ['todos', 2] }, { plugins: [pinia] })
await runTimers()

expect(entryNodeSize(cacheClient.entryRegistry)).toBe(2)
expect(entryNodeSize(cacheClient.caches)).toBe(2)
})

it('populates the entry registry', async () => {
Expand All @@ -450,7 +450,7 @@ describe('useQuery', () => {
await runTimers()

const cacheClient = useQueryCache()
expect(entryNodeSize(cacheClient.entryRegistry)).toBe(3)
expect(entryNodeSize(cacheClient.caches)).toBe(3)
})

it('order in object keys does not matter', async () => {
Expand All @@ -470,7 +470,7 @@ describe('useQuery', () => {
await runTimers()

const cacheClient = useQueryCache()
expect(entryNodeSize(cacheClient.entryRegistry)).toBe(2)
expect(entryNodeSize(cacheClient.caches)).toBe(2)
})
})

Expand Down

0 comments on commit c11ee2f

Please sign in to comment.