Skip to content

Commit d1d2b5a

Browse files
committedNov 7, 2022
feat(database): support null as a value
1 parent cb72361 commit d1d2b5a

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed
 

Diff for: ‎src/database/index.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
UnbindWithReset,
1818
walkSet,
1919
_MaybeRef,
20+
_Nullable,
2021
_RefWithState,
2122
} from '../shared'
2223
import { rtdbUnbinds } from './optionsApi'
@@ -48,7 +49,7 @@ export interface UseDatabaseRefOptions extends _DatabaseRefOptions {
4849
}
4950

5051
export function _useDatabaseRef(
51-
reference: _MaybeRef<DatabaseReference | Query>,
52+
reference: _MaybeRef<_Nullable<DatabaseReference | Query>>,
5253
localOptions: UseDatabaseRefOptions = {}
5354
) {
5455
const options = Object.assign({}, rtdbOptions, localOptions)
@@ -64,8 +65,15 @@ export function _useDatabaseRef(
6465
let removePendingPromise = noop
6566

6667
function bindDatabaseRef() {
68+
let referenceValue = unref(reference)
69+
6770
const p = new Promise<unknown | null>((resolve, reject) => {
68-
const referenceValue = unref(reference)
71+
if (!referenceValue) {
72+
_unbind = noop
73+
// TODO: maybe we shouldn't resolve this at all?
74+
return resolve(null)
75+
}
76+
6977
if (Array.isArray(data.value)) {
7078
_unbind = bindAsArray(
7179
{
@@ -92,8 +100,8 @@ export function _useDatabaseRef(
92100
})
93101

94102
// only add the first promise to the pending ones
95-
if (!isPromiseAdded) {
96-
removePendingPromise = addPendingPromise(p, unref(reference))
103+
if (!isPromiseAdded && referenceValue) {
104+
removePendingPromise = addPendingPromise(p, referenceValue)
97105
isPromiseAdded = true
98106
}
99107
promise.value = p

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
_RefDatabase,
99
} from '../../src'
1010
import { expectType, tds, setupDatabaseRefs, database } from '../utils'
11-
import { computed, nextTick, ref, unref, type Ref } from 'vue'
11+
import { computed, nextTick, ref, shallowRef, unref, type Ref } from 'vue'
1212
import { DatabaseReference, ref as _databaseRef } from 'firebase/database'
13-
import { _MaybeRef } from '../../src/shared'
13+
import { _MaybeRef, _Nullable } from '../../src/shared'
1414

1515
describe('Database objects', () => {
1616
const { databaseRef, set, update, remove } = setupDatabaseRefs()
@@ -114,6 +114,24 @@ describe('Database objects', () => {
114114
expect(data.value).toEqual({ name: 'a' })
115115
})
116116

117+
it('can be bound to a null ref', async () => {
118+
const aRef = databaseRef()
119+
const bRef = databaseRef()
120+
await set(aRef, { name: 'a' })
121+
await set(bRef, { name: 'b' })
122+
const targetRef = shallowRef()
123+
124+
const { data, promise } = factory({ ref: targetRef })
125+
await promise.value
126+
127+
expect(data.value).toBeFalsy()
128+
129+
targetRef.value = aRef
130+
await nextTick()
131+
await promise.value
132+
expect(data.value).toEqual({ name: 'a' })
133+
})
134+
117135
describe('reset option', () => {
118136
it('resets the value when specified', async () => {
119137
const { wrapper, itemRef, data } = factory({

Diff for: ‎tests/firestore/document.spec.ts

-18
Original file line numberDiff line numberDiff line change
@@ -207,24 +207,6 @@ describe(
207207
expect(data.value).toEqual({ name: 'b' })
208208
})
209209

210-
it('can be set to a null ref', async () => {
211-
const aRef = doc()
212-
const bRef = doc()
213-
await setDoc(aRef, { name: 'a' })
214-
await setDoc(bRef, { name: 'b' })
215-
const targetRef = shallowRef()
216-
217-
const { data, promise } = factory({ ref: targetRef })
218-
await promise.value
219-
220-
expect(data.value).toBeFalsy()
221-
222-
targetRef.value = aRef
223-
await nextTick()
224-
await promise.value
225-
expect(data.value).toEqual({ name: 'a' })
226-
})
227-
228210
tds(() => {
229211
const db = firestore
230212
const doc = originalDoc

0 commit comments

Comments
 (0)