Skip to content

Commit 3186afb

Browse files
committed
fix(firestore): pending value for collections
Fix #1314
1 parent e44a30d commit 3186afb

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/firestore/useFirestoreRef.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
OperationsType,
3232
walkSet,
3333
_RefWithState,
34+
isCollectionRef,
3435
} from '../shared'
3536
import { getInitialValue } from '../ssr/initialState'
3637
import { addPendingPromise } from '../ssr/plugin'
@@ -83,15 +84,19 @@ export function _useFirestoreRef(
8384
}
8485

8586
// set the initial value from SSR even if the ref comes from outside
86-
data.value = getInitialValue(
87+
const initialValue = getInitialValue(
8788
initialSourceValue,
8889
options.ssrKey,
8990
data.value,
9091
useFirebaseApp()
9192
)
93+
data.value = initialValue
94+
const hasInitialValue = isCollectionRef(initialSourceValue)
95+
? ((initialValue || []) as unknown[]).length > 0
96+
: initialValue !== undefined
9297

9398
// if no initial value is found (ssr), we should set pending to true
94-
let shouldStartAsPending = data.value === undefined // no initial value
99+
let shouldStartAsPending = !hasInitialValue
95100

96101
const pending = ref(false)
97102
const error = ref<FirestoreError>()
@@ -110,6 +115,7 @@ export function _useFirestoreRef(
110115
// we still want to create the new promise
111116
if (!docRefValue) {
112117
unbind = noop
118+
// TODO: should we set pending to false here? probably not since it starts as false
113119
// resolve to avoid an ever pending promise
114120
return resolve(null)
115121
}

tests/firestore/collection.spec.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,25 @@ describe(
155155
expect(wrapper.vm.list).toContainEqual({ name: 'cc' })
156156
})
157157

158+
it('sets pending while loading', async () => {
159+
const { wrapper, listRef, pending, promise } = factory<{ name: string }>()
160+
161+
expect(pending.value).toBe(true)
162+
await promise.value
163+
expect(pending.value).toBe(false)
164+
})
165+
158166
it('fetches once', async () => {
159167
const listRef = collection<{ name: string }>()
160168
await addDoc(listRef, { name: 'a' })
161-
const { wrapper, promise, data } = factory<{ name: string }>({
169+
const { wrapper, promise, data, pending } = factory<{ name: string }>({
162170
ref: listRef,
163171
options: { once: true },
164172
})
165173

174+
expect(pending.value).toBe(true)
166175
await promise.value
176+
expect(pending.value).toBe(false)
167177

168178
expect(wrapper.vm.list).toEqual([{ name: 'a' }])
169179
await addDoc(listRef, { name: 'd' })

0 commit comments

Comments
 (0)