@@ -13,17 +13,24 @@ import type { DatabaseReference, Query } from 'firebase/database'
1313import {
1414 noop ,
1515 OperationsType ,
16+ ResetOption ,
1617 walkSet ,
1718 _MaybeRef ,
1819 _RefWithState ,
1920} from '../shared'
2021import { rtdbUnbinds } from './optionsApi'
21- import { bindAsArray , bindAsObject , _DatabaseRefOptions } from './subscribe'
22+ import {
23+ bindAsArray ,
24+ bindAsObject ,
25+ rtdbOptions ,
26+ _DatabaseRefOptions ,
27+ } from './subscribe'
2228import {
2329 VueDatabaseDocumentData ,
2430 VueDatabaseQueryData ,
2531 _RefDatabase ,
2632} from './utils'
33+ import { addPendingPromise } from '../ssr/plugin'
2734
2835export { databasePlugin } from './optionsApi'
2936
@@ -43,17 +50,19 @@ type UnbindType = ReturnType<typeof bindAsArray | typeof bindAsObject>
4350
4451export function _useDatabaseRef (
4552 reference : _MaybeRef < DatabaseReference | Query > ,
46- options : UseDatabaseRefOptions = { }
53+ localOptions : UseDatabaseRefOptions = { }
4754) {
55+ const options = Object . assign ( { } , rtdbOptions , localOptions )
4856 let _unbind ! : UnbindType
4957
5058 const data = options . target || ref < unknown | null > ( options . initialValue )
5159 const error = ref < Error > ( )
5260 const pending = ref ( true )
5361 // force the type since its value is set right after and undefined isn't possible
5462 const promise = shallowRef ( ) as ShallowRef < Promise < unknown | null > >
55- const createdPromises = new Set < Promise < unknown | null > > ( )
63+ let isPromiseAdded = false
5664 const hasCurrentScope = getCurrentScope ( )
65+ let removePendingPromise = noop
5766
5867 function bindDatabaseRef ( ) {
5968 const p = new Promise < unknown | null > ( ( resolve , reject ) => {
@@ -84,11 +93,10 @@ export function _useDatabaseRef(
8493 } )
8594
8695 // only add the first promise to the pending ones
87- if ( ! createdPromises . size ) {
88- // TODO: add the pending promise like in firestore
89- // pendingPromises.add(p)
96+ if ( ! isPromiseAdded ) {
97+ removePendingPromise = addPendingPromise ( p , unref ( reference ) )
98+ isPromiseAdded = true
9099 }
91- createdPromises . add ( p )
92100 promise . value = p
93101
94102 p . catch ( ( reason ) => {
@@ -112,19 +120,14 @@ export function _useDatabaseRef(
112120 }
113121
114122 if ( hasCurrentScope ) {
115- onScopeDispose ( ( ) => {
116- // TODO: clear pending promises
117- // for (const p of createdPromises) {
118- // pendingPromises.delete(p)
119- // }
120- _unbind ( options . reset )
121- } )
123+ onScopeDispose ( unbind )
122124 }
123125
124126 // TODO: rename to stop
125- function unbind ( ) {
127+ function unbind ( reset : ResetOption = options . reset ) {
126128 stopWatcher ( )
127- _unbind ( options . reset )
129+ removePendingPromise ( )
130+ _unbind ( reset )
128131 }
129132
130133 return Object . defineProperties ( data , {
@@ -140,7 +143,7 @@ export function _useDatabaseRef(
140143export function internalUnbind (
141144 key : string ,
142145 unbinds : Record < string , UnbindType > | undefined ,
143- reset ?: _DatabaseRefOptions [ 'reset' ]
146+ reset ?: ResetOption
144147) {
145148 if ( unbinds && unbinds [ key ] ) {
146149 unbinds [ key ] ( reset )
0 commit comments