Skip to content

Commit 61513f6

Browse files
committed
fix(firestore): set pending on queries
Fix #1317
1 parent 20f0c26 commit 61513f6

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

src/firestore/useFirestoreRef.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
OperationsType,
3232
walkSet,
3333
_RefWithState,
34-
isCollectionRef,
3534
} from '../shared'
3635
import { getInitialValue } from '../ssr/initialState'
3736
import { addPendingPromise } from '../ssr/plugin'
@@ -50,6 +49,8 @@ export interface _UseFirestoreRefOptions extends FirestoreRefOptions {
5049
converter?: FirestoreDataConverter<unknown>
5150
}
5251

52+
const NO_INITIAL_VALUE = Symbol()
53+
5354
/**
5455
* Internal version of `useDocument()` and `useCollection()`.
5556
*
@@ -87,18 +88,14 @@ export function _useFirestoreRef(
8788
const initialValue = getInitialValue(
8889
initialSourceValue,
8990
options.ssrKey,
90-
data.value,
91+
NO_INITIAL_VALUE,
9192
useFirebaseApp()
9293
)
93-
data.value = initialValue
94-
const hasInitialValue =
95-
// TODO: we need a stricter check for collections and queries and the initial target is passed as a ref([]) but
96-
// maybe that [] should be set here instead. It's also worth taking into account that a custom ref can be passed as
97-
// target as it should probably be initially empty but maybe this is too much to ask.
98-
// TODO: add and test || isFirestoreQuery()
99-
isCollectionRef(initialSourceValue)
100-
? ((initialValue || []) as unknown[]).length > 0
101-
: initialValue !== undefined
94+
95+
const hasInitialValue = initialValue !== NO_INITIAL_VALUE
96+
if (hasInitialValue) {
97+
data.value = initialValue
98+
}
10299

103100
// if no initial value is found (ssr), we should set pending to true
104101
let shouldStartAsPending = !hasInitialValue

src/shared.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,12 @@ export function isFirestoreDataReference<T = unknown>(
121121
return isDocumentRef(source) || isCollectionRef(source)
122122
}
123123

124-
export function isFirestoreQuery(
124+
export function isFirestoreQuery<
125+
AppModelType = DocumentData,
126+
DbModelType extends DocumentData = DocumentData
127+
>(
125128
source: unknown
126-
): source is FirestoreQuery<unknown> & { path: undefined } {
129+
): source is FirestoreQuery<AppModelType, DbModelType> & { path: undefined } {
127130
// makes some types so much easier
128131
return isObject(source) && source.type === 'query'
129132
}

tests/firestore/collection.spec.ts

+25-5
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,17 @@ describe(
7171
}
7272
}
7373

74-
function factoryQuery<T = DocumentData>({
74+
function factoryQuery<
75+
AppModelType = DocumentData,
76+
DbModelType extends DocumentData = DocumentData
77+
>({
7578
options,
7679
ref,
7780
}: {
7881
options?: UseCollectionOptions
79-
ref?: _MaybeRef<_Nullable<CollectionReference<T> | Query<T>>>
82+
ref?: _MaybeRef<_Nullable<Query<AppModelType, DbModelType>>>
8083
} = {}) {
81-
let data!: _RefFirestore<VueFirestoreQueryData<T>>
84+
let data!: _RefFirestore<VueFirestoreQueryData<AppModelType>>
8285

8386
const wrapper = mount(
8487
defineComponent({
@@ -88,7 +91,10 @@ describe(
8891
data = useCollection(
8992
// split for ts
9093
ref,
91-
options
94+
{
95+
ssrKey: Math.random().toString(36),
96+
...options,
97+
}
9298
)
9399
const { data: list, pending, error, promise, stop } = data
94100
return { list, pending, error, promise, stop }
@@ -100,6 +106,7 @@ describe(
100106
wrapper,
101107
// non enumerable properties cannot be spread
102108
data: data.data,
109+
listQuery: unref(ref)!,
103110
pending: data.pending,
104111
error: data.error,
105112
promise: data.promise,
@@ -175,6 +182,19 @@ describe(
175182
expect(pending.value).toBe(false)
176183
})
177184

185+
it('sets pending while loading with a query', async () => {
186+
const listRef = collection<{ name: string }>()
187+
const { wrapper, listQuery, pending, promise } = factoryQuery<{
188+
name: string
189+
}>({
190+
ref: query(listRef, where('name', '==', 'a')),
191+
})
192+
193+
expect(pending.value).toBe(true)
194+
await promise.value
195+
expect(pending.value).toBe(false)
196+
})
197+
178198
it('fetches once', async () => {
179199
const listRef = collection<{ name: string }>()
180200
await addDoc(listRef, { name: 'a' })
@@ -497,5 +517,5 @@ describe(
497517
expectType<Ref<string[]>>(useCollection(refWithConverter))
498518
})
499519
},
500-
{ retry: 3 }
520+
{ retry: process.env.CI ? 3 : 1 }
501521
)

0 commit comments

Comments
 (0)