@@ -6,40 +6,41 @@ import { useFirebaseApp } from '../app'
66import { getGlobalScope } from '../globals'
77import { ResetOption , UnbindWithReset } from '../shared'
88import { 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'
1610
1711/**
1812 * Options for the Firebase Database Plugin that enables the Options API such as `$rtdbBind` and `$rtdbUnbind`.
1913 */
2014export interface DatabasePluginOptions
2115 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+ */
2219 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+ */
2324 unbindName ?: string
2425}
2526
2627const databasePluginDefaults : Readonly <
2728 Required < Omit < DatabasePluginOptions , keyof _GlobalDatabaseRefOptions > >
2829> = {
29- bindName : '$rtdbBind ' ,
30- unbindName : '$rtdbUnbind ' ,
30+ bindName : '$databaseBind ' ,
31+ unbindName : '$databaseUnbind ' ,
3132}
3233
3334export type VueFirebaseObject = Record < string , Query | DatabaseReference >
3435export type FirebaseOption = VueFirebaseObject | ( ( ) => VueFirebaseObject )
3536
36- export const rtdbUnbinds = new WeakMap <
37+ export const databaseUnbinds = new WeakMap <
3738 object ,
3839 Record < string , UnbindWithReset >
3940> ( )
4041
4142/**
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
4344 * you use the Options API. If you **exclusively use the Composition API** (e.g. `useObject()` and `useList()`), you
4445 * should not add it.
4546 *
@@ -64,32 +65,44 @@ export function databasePlugin(
6465 ? app . config . globalProperties
6566 : ( app as any ) . prototype
6667
67- GlobalTarget [ unbindName ] = function rtdbUnbind (
68+ GlobalTarget [ unbindName ] = function databaseUnbind (
69+ this : ComponentPublicInstance ,
6870 key : string ,
6971 reset ?: ResetOption
7072 ) {
71- internalUnbind ( key , rtdbUnbinds . get ( this ) , reset )
73+ internalUnbind ( key , databaseUnbinds . get ( this ) , reset )
74+ // @ts -expect-error: readonly for the users
7275 delete this . $firebaseRefs [ key ]
7376 }
7477
75- // add $rtdbBind and $rtdbUnbind methods
76- GlobalTarget [ bindName ] = function rtdbBind (
78+ GlobalTarget [ bindName ] = function databaseBind (
7779 this : ComponentPublicInstance ,
7880 key : string ,
7981 source : DatabaseReference | Query ,
8082 userOptions ?: _DatabaseRefOptions
8183 ) {
8284 const options = Object . assign ( { } , globalOptions , userOptions )
8385 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 , { } )
8688 }
87- const unbinds = rtdbUnbinds . get ( this ) !
89+ const unbinds = databaseUnbinds . get ( this ) !
8890
8991 if ( unbinds [ key ] ) {
9092 unbinds [ key ] ( options . reset )
9193 }
9294
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
93106 const scope = getGlobalScope ( firebaseApp || useFirebaseApp ( ) , app ) . run ( ( ) =>
94107 effectScope ( )
95108 ) !
@@ -117,6 +130,7 @@ export function databasePlugin(
117130 beforeCreate ( this : ComponentPublicInstance ) {
118131 this . $firebaseRefs = Object . create ( null )
119132 } ,
133+
120134 created ( this : ComponentPublicInstance ) {
121135 let bindings = this . $options . firebase
122136 if ( typeof bindings === 'function' ) {
@@ -136,7 +150,7 @@ export function databasePlugin(
136150 } ,
137151
138152 beforeUnmount ( this : ComponentPublicInstance ) {
139- const unbinds = rtdbUnbinds . get ( this )
153+ const unbinds = databaseUnbinds . get ( this )
140154 if ( unbinds ) {
141155 for ( const key in unbinds ) {
142156 unbinds [ key ] ( )
@@ -149,7 +163,8 @@ export function databasePlugin(
149163}
150164
151165/**
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.
153168 *
154169 * @example
155170 *
@@ -182,6 +197,16 @@ declare module '@vue/runtime-core' {
182197 * @param reference
183198 * @param options
184199 */
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+ */
185210 $rtdbBind (
186211 name : string ,
187212 reference : DatabaseReference | Query ,
@@ -191,6 +216,12 @@ declare module '@vue/runtime-core' {
191216 /**
192217 * Unbinds a bound reference
193218 */
219+ $databaseUnbind : ( name : string , reset ?: ResetOption ) => void
220+
221+ /**
222+ * {@inheritDoc ComponentCustomProperties.$databaseUnbind }
223+ * @deprecated Use `$databaseUnbind` instead.
224+ */
194225 $rtdbUnbind : ( name : string , reset ?: ResetOption ) => void
195226
196227 /**
@@ -210,7 +241,7 @@ declare module '@vue/runtime-core' {
210241 }
211242 export interface ComponentCustomOptions {
212243 /**
213- * Calls `$rtdbBind ` at created
244+ * Calls `$databaseBind ` at created
214245 */
215246 firebase ?: FirebaseOption
216247 }
0 commit comments