Skip to content

Commit ee25d06

Browse files
committedFeb 26, 2023
fix(database): pending for lists
1 parent 3186afb commit ee25d06

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed
 

Diff for: ‎src/database/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ export function useDatabaseList<T = unknown>(
2828
options?: UseListOptions
2929
): _RefDatabase<VueDatabaseQueryData<T>> {
3030
const data = ref<T[]>([]) as Ref<T[]>
31-
return _useDatabaseRef(reference, {
32-
target: data,
33-
...options,
34-
}) as _RefDatabase<VueDatabaseQueryData<T>>
31+
return _useDatabaseRef(
32+
reference,
33+
{
34+
target: data,
35+
...options,
36+
},
37+
true
38+
) as _RefDatabase<VueDatabaseQueryData<T>>
3539
}
3640

3741
/**

Diff for: ‎src/database/useDatabaseRef.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export interface UseDatabaseRefOptions extends _DatabaseRefOptions {}
3939

4040
export function _useDatabaseRef(
4141
reference: _MaybeRef<_Nullable<DatabaseReference | Query>>,
42-
localOptions: UseDatabaseRefOptions = {}
42+
localOptions: UseDatabaseRefOptions = {},
43+
isList = false
4344
): _RefDatabase<unknown> {
4445
let unbind: UnbindWithReset = noop
4546
const options = Object.assign({}, globalDatabaseOptions, localOptions)
@@ -63,15 +64,20 @@ export function _useDatabaseRef(
6364
}
6465

6566
// set the initial value from SSR even if the ref comes from outside
66-
data.value = getInitialValue(
67+
const initialValue = getInitialValue(
6768
initialSourceValue,
6869
options.ssrKey,
6970
data.value,
7071
useFirebaseApp()
7172
)
73+
data.value = initialValue
74+
75+
const hasInitialValue = isList
76+
? ((initialValue || []) as unknown[]).length > 0
77+
: initialValue !== undefined
7278

7379
// if no initial value is found (ssr), we should set pending to true
74-
let shouldStartAsPending = data.value === undefined // no initial value
80+
let shouldStartAsPending = !hasInitialValue
7581

7682
const error = ref<Error>()
7783
const pending = ref(false)

Diff for: ‎tests/database/list.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,11 @@ describe('Database lists', () => {
336336
const ref = databaseRef()
337337
await push(ref, { name: 'a' })
338338
await push(ref, { name: 'b' })
339-
const { error, promise, data } = factory({ ref })
339+
const { error, promise, data, pending } = factory({ ref })
340340

341+
expect(pending.value).toBe(true)
341342
await expect(unref(promise)).resolves.toEqual(expect.anything())
343+
expect(pending.value).toBe(false)
342344
expect(data.value).toContainEqual({ name: 'a' })
343345
expect(data.value).toContainEqual({ name: 'b' })
344346
expect(error.value).toBeUndefined()

0 commit comments

Comments
 (0)
Please sign in to comment.