@@ -6,40 +6,41 @@ import { useFirebaseApp } from '../app'
6
6
import { getGlobalScope } from '../globals'
7
7
import { ResetOption , UnbindWithReset } from '../shared'
8
8
import { internalUnbind , _useDatabaseRef } from './index'
9
- import {
10
- bindAsArray ,
11
- bindAsObject ,
12
- rtdbOptions ,
13
- _DatabaseRefOptions ,
14
- _GlobalDatabaseRefOptions ,
15
- } from './subscribe'
9
+ import { _DatabaseRefOptions , _GlobalDatabaseRefOptions } from './subscribe'
16
10
17
11
/**
18
12
* Options for the Firebase Database Plugin that enables the Options API such as `$rtdbBind` and `$rtdbUnbind`.
19
13
*/
20
14
export interface DatabasePluginOptions
21
15
extends Partial < _GlobalDatabaseRefOptions > {
16
+ /**
17
+ * @deprecated : was largely unused and not very useful. Please open an issue with use cases if you need this.
18
+ */
22
19
bindName ?: string
20
+
21
+ /**
22
+ * @deprecated : was largely unused and not very useful. Please open an issue with use cases if you need this.
23
+ */
23
24
unbindName ?: string
24
25
}
25
26
26
27
const databasePluginDefaults : Readonly <
27
28
Required < Omit < DatabasePluginOptions , keyof _GlobalDatabaseRefOptions > >
28
29
> = {
29
- bindName : '$rtdbBind ' ,
30
- unbindName : '$rtdbUnbind ' ,
30
+ bindName : '$databaseBind ' ,
31
+ unbindName : '$databaseUnbind ' ,
31
32
}
32
33
33
34
export type VueFirebaseObject = Record < string , Query | DatabaseReference >
34
35
export type FirebaseOption = VueFirebaseObject | ( ( ) => VueFirebaseObject )
35
36
36
- export const rtdbUnbinds = new WeakMap <
37
+ export const databaseUnbinds = new WeakMap <
37
38
object ,
38
39
Record < string , UnbindWithReset >
39
40
> ( )
40
41
41
42
/**
42
- * Install this plugin if you want to add `$rtdbBind ` and `$rtdbUnbind ` functions. Note this plugin is only necessary if
43
+ * Install this plugin if you want to add `$databaseBind ` and `$databaseUnbind ` functions. Note this plugin is only necessary if
43
44
* you use the Options API. If you **exclusively use the Composition API** (e.g. `useObject()` and `useList()`), you
44
45
* should not add it.
45
46
*
@@ -64,32 +65,44 @@ export function databasePlugin(
64
65
? app . config . globalProperties
65
66
: ( app as any ) . prototype
66
67
67
- GlobalTarget [ unbindName ] = function rtdbUnbind (
68
+ GlobalTarget [ unbindName ] = function databaseUnbind (
69
+ this : ComponentPublicInstance ,
68
70
key : string ,
69
71
reset ?: ResetOption
70
72
) {
71
- internalUnbind ( key , rtdbUnbinds . get ( this ) , reset )
73
+ internalUnbind ( key , databaseUnbinds . get ( this ) , reset )
74
+ // @ts -expect-error: readonly for the users
72
75
delete this . $firebaseRefs [ key ]
73
76
}
74
77
75
- // add $rtdbBind and $rtdbUnbind methods
76
- GlobalTarget [ bindName ] = function rtdbBind (
78
+ GlobalTarget [ bindName ] = function databaseBind (
77
79
this : ComponentPublicInstance ,
78
80
key : string ,
79
81
source : DatabaseReference | Query ,
80
82
userOptions ?: _DatabaseRefOptions
81
83
) {
82
84
const options = Object . assign ( { } , globalOptions , userOptions )
83
85
const target = toRef ( this . $data as any , key )
84
- if ( ! rtdbUnbinds . has ( this ) ) {
85
- rtdbUnbinds . set ( this , { } )
86
+ if ( ! databaseUnbinds . has ( this ) ) {
87
+ databaseUnbinds . set ( this , { } )
86
88
}
87
- const unbinds = rtdbUnbinds . get ( this ) !
89
+ const unbinds = databaseUnbinds . get ( this ) !
88
90
89
91
if ( unbinds [ key ] ) {
90
92
unbinds [ key ] ( options . reset )
91
93
}
92
94
95
+ // add the old rtdb* methods if the user was using the defaults
96
+ if ( pluginOptions ) {
97
+ if ( ! pluginOptions . bindName ) {
98
+ GlobalTarget [ '$rtdbBind' ] = GlobalTarget [ bindName ]
99
+ }
100
+ if ( ! pluginOptions . unbindName ) {
101
+ GlobalTarget [ '$rtdbUnbind' ] = GlobalTarget [ unbindName ]
102
+ }
103
+ }
104
+
105
+ // we create a local scope to avoid leaking the effect since it's created outside of the component
93
106
const scope = getGlobalScope ( firebaseApp || useFirebaseApp ( ) , app ) . run ( ( ) =>
94
107
effectScope ( )
95
108
) !
@@ -117,6 +130,7 @@ export function databasePlugin(
117
130
beforeCreate ( this : ComponentPublicInstance ) {
118
131
this . $firebaseRefs = Object . create ( null )
119
132
} ,
133
+
120
134
created ( this : ComponentPublicInstance ) {
121
135
let bindings = this . $options . firebase
122
136
if ( typeof bindings === 'function' ) {
@@ -136,7 +150,7 @@ export function databasePlugin(
136
150
} ,
137
151
138
152
beforeUnmount ( this : ComponentPublicInstance ) {
139
- const unbinds = rtdbUnbinds . get ( this )
153
+ const unbinds = databaseUnbinds . get ( this )
140
154
if ( unbinds ) {
141
155
for ( const key in unbinds ) {
142
156
unbinds [ key ] ( )
@@ -149,7 +163,8 @@ export function databasePlugin(
149
163
}
150
164
151
165
/**
152
- * VueFire Database Module to be added to the `VueFire` Vue plugin options.
166
+ * VueFire Database Module to be added to the `VueFire` Vue plugin options. If you **exclusively use the Composition
167
+ * API** (e.g. `useObject()` and `useList()`), you should not add it.
153
168
*
154
169
* @example
155
170
*
@@ -182,6 +197,16 @@ declare module '@vue/runtime-core' {
182
197
* @param reference
183
198
* @param options
184
199
*/
200
+ $databaseBind (
201
+ name : string ,
202
+ reference : DatabaseReference | Query ,
203
+ options ?: _DatabaseRefOptions
204
+ ) : Promise < DataSnapshot >
205
+
206
+ /**
207
+ * {@inheritDoc ComponentCustomProperties.$databaseBind }
208
+ * @deprecated Use `$databaseBind` instead.
209
+ */
185
210
$rtdbBind (
186
211
name : string ,
187
212
reference : DatabaseReference | Query ,
@@ -191,6 +216,12 @@ declare module '@vue/runtime-core' {
191
216
/**
192
217
* Unbinds a bound reference
193
218
*/
219
+ $databaseUnbind : ( name : string , reset ?: ResetOption ) => void
220
+
221
+ /**
222
+ * {@inheritDoc ComponentCustomProperties.$databaseUnbind }
223
+ * @deprecated Use `$databaseUnbind` instead.
224
+ */
194
225
$rtdbUnbind : ( name : string , reset ?: ResetOption ) => void
195
226
196
227
/**
@@ -210,7 +241,7 @@ declare module '@vue/runtime-core' {
210
241
}
211
242
export interface ComponentCustomOptions {
212
243
/**
213
- * Calls `$rtdbBind ` at created
244
+ * Calls `$databaseBind ` at created
214
245
*/
215
246
firebase ?: FirebaseOption
216
247
}
0 commit comments