Skip to content

Commit 8b8b26e

Browse files
committed
feat: allow getters to functions
This change allows replacing usage of `computed()` in `useDocument()` and similar with just a function: ```ts // from useDocument(computed(() => doc(db, 'users', userId))) // to useDocument(() => doc(db, 'users', userId)) ```
1 parent 744dd81 commit 8b8b26e

File tree

15 files changed

+84
-91
lines changed

15 files changed

+84
-91
lines changed

src/auth/user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from 'firebase/auth'
1111
import { computed, Ref } from 'vue-demi'
1212
import { useFirebaseApp } from '../app'
13-
import type { _MaybeRef, _Nullable } from '../shared'
13+
import type { _Nullable } from '../shared'
1414

1515
/**
1616
* Maps an application to a user

src/database/bind.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import {
88
noop,
99
ResetOption,
1010
_DataSourceOptions,
11-
_MaybeRef,
1211
_ResolveRejectFn,
1312
} from '../shared'
14-
import { Ref, unref } from 'vue-demi'
13+
import { Ref, toValue, MaybeRefOrGetter } from 'vue-demi'
1514
import {
1615
onValue,
1716
onChildAdded,
@@ -117,7 +116,9 @@ export function bindAsArray(
117116
) {
118117
const options = Object.assign({}, DEFAULT_OPTIONS, extraOptions)
119118

120-
let arrayRef: _MaybeRef<VueDatabaseQueryData> = options.wait ? [] : target
119+
let arrayRef: MaybeRefOrGetter<VueDatabaseQueryData> = options.wait
120+
? []
121+
: target
121122
// by default we wait, if not, set the value to an empty array so it can be populated correctly
122123
if (!options.wait) {
123124
target.value = []
@@ -146,7 +147,7 @@ export function bindAsArray(
146147
removeChildAddedListener = onChildAdded(
147148
collection,
148149
(snapshot, prevKey) => {
149-
const array = unref(arrayRef)
150+
const array = toValue(arrayRef)
150151
const index = prevKey ? indexForKey(array, prevKey) + 1 : 0
151152
// cannot be null because it exists
152153
array.splice(index, 0, options.serialize(snapshot)!)
@@ -158,7 +159,7 @@ export function bindAsArray(
158159
collection,
159160

160161
(snapshot) => {
161-
const array = unref(arrayRef)
162+
const array = toValue(arrayRef)
162163
array.splice(indexForKey(array, snapshot.key), 1)
163164
},
164165
reject
@@ -167,7 +168,7 @@ export function bindAsArray(
167168
removeChildChangedListener = onChildChanged(
168169
collection,
169170
(snapshot) => {
170-
const array = unref(arrayRef)
171+
const array = toValue(arrayRef)
171172
array.splice(
172173
indexForKey(array, snapshot.key),
173174
1,
@@ -181,7 +182,7 @@ export function bindAsArray(
181182
removeChildMovedListener = onChildMoved(
182183
collection,
183184
(snapshot, prevKey) => {
184-
const array = unref(arrayRef)
185+
const array = toValue(arrayRef)
185186
const index = indexForKey(array, snapshot.key)
186187
const oldRecord = array.splice(index, 1)[0]
187188
const newIndex = prevKey ? indexForKey(array, prevKey) + 1 : 0
@@ -194,7 +195,7 @@ export function bindAsArray(
194195
removeValueListener = onValue(
195196
collection,
196197
() => {
197-
const array = unref(arrayRef)
198+
const array = toValue(arrayRef)
198199
if (options.wait) {
199200
target.value = array
200201
// switch to the target so all changes happen into the target

src/database/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Ref, ref } from 'vue-demi'
1+
import { Ref, ref, MaybeRefOrGetter } from 'vue-demi'
22
import { DatabaseReference, getDatabase, Query } from 'firebase/database'
3-
import { _MaybeRef, _Nullable, _RefWithState } from '../shared'
3+
import { _Nullable, _RefWithState } from '../shared'
44
import { _DatabaseRefOptions } from './bind'
55
import {
66
VueDatabaseDocumentData,
@@ -24,7 +24,7 @@ export type UseListOptions = UseDatabaseRefOptions
2424
* @param options - optional options
2525
*/
2626
export function useDatabaseList<T = unknown>(
27-
reference: _MaybeRef<DatabaseReference | Query>,
27+
reference: MaybeRefOrGetter<DatabaseReference | Query>,
2828
options?: UseListOptions
2929
): _RefDatabase<VueDatabaseQueryData<T>> {
3030
const data = ref<T[]>([]) as Ref<T[]>
@@ -54,7 +54,7 @@ export type UseObjectOptions = UseDatabaseRefOptions
5454
* @param options - optional options
5555
*/
5656
export function useDatabaseObject<T = unknown>(
57-
reference: _MaybeRef<DatabaseReference>,
57+
reference: MaybeRefOrGetter<DatabaseReference>,
5858
options?: UseObjectOptions
5959
): _RefDatabase<VueDatabaseDocumentData<T> | undefined> {
6060
const data = ref<T | null>()

src/database/useDatabaseRef.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { DatabaseReference, Query } from 'firebase/database'
22
import {
3-
unref,
3+
MaybeRefOrGetter,
4+
toValue,
45
ref,
56
shallowRef,
67
ShallowRef,
@@ -14,7 +15,6 @@ import {
1415
} from 'vue-demi'
1516
import { useFirebaseApp } from '../app'
1617
import {
17-
_MaybeRef,
1818
_Nullable,
1919
UnbindWithReset,
2020
checkWrittenTarget,
@@ -38,13 +38,13 @@ import { _RefDatabase } from './utils'
3838
export interface UseDatabaseRefOptions extends _DatabaseRefOptions {}
3939

4040
export function _useDatabaseRef(
41-
reference: _MaybeRef<_Nullable<DatabaseReference | Query>>,
41+
reference: MaybeRefOrGetter<_Nullable<DatabaseReference | Query>>,
4242
localOptions: UseDatabaseRefOptions = {},
4343
isList = false
4444
): _RefDatabase<unknown> {
4545
let unbind: UnbindWithReset = noop
4646
const options = Object.assign({}, globalDatabaseOptions, localOptions)
47-
const initialSourceValue = unref(reference)
47+
const initialSourceValue = toValue(reference)
4848
const data = options.target || ref<unknown | null>()
4949

5050
// dev only warning
@@ -88,7 +88,7 @@ export function _useDatabaseRef(
8888
let removePendingPromise = noop
8989

9090
function bindDatabaseRef() {
91-
const referenceValue = unref(reference)
91+
const referenceValue = toValue(reference)
9292

9393
const newPromise = new Promise<unknown | null>((resolve, reject) => {
9494
unbind(options.reset)

src/firestore/bind.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import {
33
walkGet,
44
callOnceWithArg,
55
OperationsType,
6-
_MaybeRef,
76
ResetOption,
87
_DataSourceOptions,
98
noop,
109
_ResolveRejectFn,
1110
} from '../shared'
12-
import { ref, Ref, unref } from 'vue-demi'
11+
import { ref, Ref, toValue } from 'vue-demi'
1312
import type {
1413
CollectionReference,
1514
DocumentChange,
@@ -323,7 +322,7 @@ export function bindCollection<T = unknown>(
323322
subs,
324323
options
325324
)
326-
ops.add(unref(arrayRef), newIndex, data)
325+
ops.add(toValue(arrayRef), newIndex, data)
327326
subscribeToRefs(
328327
options,
329328
arrayRef,
@@ -337,7 +336,7 @@ export function bindCollection<T = unknown>(
337336
)
338337
},
339338
modified: ({ oldIndex, newIndex, doc }: DocumentChange<T>) => {
340-
const array = unref(arrayRef)
339+
const array = toValue(arrayRef)
341340
const subs = arraySubs[oldIndex]
342341
const oldData = array[oldIndex]
343342
const [data, refs] = extractRefs(
@@ -365,7 +364,7 @@ export function bindCollection<T = unknown>(
365364
)
366365
},
367366
removed: ({ oldIndex }: DocumentChange<T>) => {
368-
const array = unref(arrayRef)
367+
const array = toValue(arrayRef)
369368
ops.remove(array, oldIndex)
370369
unsubscribeAll(arraySubs.splice(oldIndex, 1)[0])
371370
},
@@ -396,10 +395,10 @@ export function bindCollection<T = unknown>(
396395
if (++count >= expectedItems) {
397396
// if wait is true, finally set the array
398397
if (wait) {
399-
ops.set(target, key, unref(arrayRef))
398+
ops.set(target, key, toValue(arrayRef))
400399
arrayRef = target
401400
}
402-
originalResolve(unref(arrayRef))
401+
originalResolve(toValue(arrayRef))
403402
// reset resolve to noop
404403
resolve = noop
405404
}
@@ -417,10 +416,10 @@ export function bindCollection<T = unknown>(
417416
// being called multiple times
418417
if (!docChanges.length) {
419418
if (wait) {
420-
ops.set(target, key, unref(arrayRef))
419+
ops.set(target, key, toValue(arrayRef))
421420
arrayRef = target
422421
}
423-
resolve(unref(arrayRef))
422+
resolve(toValue(arrayRef))
424423
}
425424
}
426425

src/firestore/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
Query,
55
getFirestore,
66
} from 'firebase/firestore'
7-
import { ref } from 'vue-demi'
7+
import { ref, MaybeRefOrGetter } from 'vue-demi'
88
import { useFirebaseApp } from '../app'
9-
import type { _MaybeRef, _Nullable, _RefWithState } from '../shared'
9+
import type { _Nullable, _RefWithState } from '../shared'
1010
import {
1111
VueFirestoreDocumentData,
1212
VueFirestoreQueryData,
@@ -30,7 +30,7 @@ export function useCollection<
3030
// explicit generic as unknown to allow arbitrary types like numbers or strings
3131
R extends CollectionReference<unknown> | Query<unknown>
3232
>(
33-
collectionRef: _MaybeRef<_Nullable<R>>,
33+
collectionRef: MaybeRefOrGetter<_Nullable<R>>,
3434
options?: UseCollectionOptions
3535
): _RefFirestore<_InferReferenceType<R>[]>
3636

@@ -43,12 +43,12 @@ export function useCollection<
4343
* @param options - optional options
4444
*/
4545
export function useCollection<T>(
46-
collectionRef: _MaybeRef<_Nullable<CollectionReference | Query>>,
46+
collectionRef: MaybeRefOrGetter<_Nullable<CollectionReference | Query>>,
4747
options?: UseCollectionOptions
4848
): _RefFirestore<VueFirestoreQueryData<T>>
4949

5050
export function useCollection<T>(
51-
collectionRef: _MaybeRef<
51+
collectionRef: MaybeRefOrGetter<
5252
_Nullable<CollectionReference<unknown> | Query<unknown>>
5353
>,
5454
options?: UseCollectionOptions
@@ -71,7 +71,7 @@ export function useDocument<
7171
// explicit generic as unknown to allow arbitrary types like numbers or strings
7272
R extends DocumentReference<unknown>
7373
>(
74-
documentRef: _MaybeRef<_Nullable<R>>,
74+
documentRef: MaybeRefOrGetter<_Nullable<R>>,
7575
options?: UseDocumentOptions
7676
): _RefFirestore<_InferReferenceType<R> | undefined> // this one can't be null or should be specified in the converter
7777

@@ -84,12 +84,12 @@ export function useDocument<
8484
* @param options - optional options
8585
*/
8686
export function useDocument<T>(
87-
documentRef: _MaybeRef<_Nullable<DocumentReference>>,
87+
documentRef: MaybeRefOrGetter<_Nullable<DocumentReference>>,
8888
options?: UseDocumentOptions
8989
): _RefFirestore<VueFirestoreDocumentData<T> | undefined>
9090

9191
export function useDocument<T>(
92-
documentRef: _MaybeRef<_Nullable<DocumentReference<unknown>>>,
92+
documentRef: MaybeRefOrGetter<_Nullable<DocumentReference<unknown>>>,
9393
options?: UseDocumentOptions
9494
): _RefFirestore<VueFirestoreDocumentData<T> | undefined> {
9595
// no unwrapRef to have a simpler type

src/firestore/useFirestoreRef.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import type {
77
FirestoreDataConverter,
88
} from 'firebase/firestore'
99
import {
10-
unref,
10+
MaybeRefOrGetter,
11+
toValue,
1112
ref,
1213
shallowRef,
1314
ShallowRef,
@@ -20,7 +21,6 @@ import {
2021
} from 'vue-demi'
2122
import { useFirebaseApp } from '../app'
2223
import {
23-
_MaybeRef,
2424
_Nullable,
2525
UnbindWithReset,
2626
noop,
@@ -57,7 +57,7 @@ const NO_INITIAL_VALUE = Symbol()
5757
* @internal
5858
*/
5959
export function _useFirestoreRef(
60-
docOrCollectionRef: _MaybeRef<
60+
docOrCollectionRef: MaybeRefOrGetter<
6161
_Nullable<
6262
DocumentReference<unknown> | Query<unknown> | CollectionReference<unknown>
6363
>
@@ -66,7 +66,7 @@ export function _useFirestoreRef(
6666
): _RefFirestore<unknown> {
6767
let unbind: UnbindWithReset = noop
6868
const options = Object.assign({}, firestoreOptionsDefaults, localOptions)
69-
const initialSourceValue = unref(docOrCollectionRef)
69+
const initialSourceValue = toValue(docOrCollectionRef)
7070
const data = options.target || ref<unknown | null>()
7171

7272
// dev only warning
@@ -108,7 +108,7 @@ export function _useFirestoreRef(
108108
let removePendingPromise = noop
109109

110110
function bindFirestoreRef() {
111-
let docRefValue = unref(docOrCollectionRef)
111+
let docRefValue = toValue(docOrCollectionRef)
112112

113113
const newPromise = new Promise<unknown | null>((resolve, reject) => {
114114
// stop the previous subscription

src/shared.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,6 @@ export interface _RefWithState<T, E = Error> extends Ref<T> {
223223
stop: (reset?: ResetOption) => void
224224
}
225225

226-
/**
227-
* @internal
228-
*/
229-
export type _MaybeRef<T> = T | Ref<T>
230-
231226
/**
232227
* Base options for the data source options in both Firestore and Realtime Database.
233228
*

0 commit comments

Comments
 (0)