@@ -4,7 +4,7 @@ import type {
44 DocumentData ,
55 DocumentReference ,
66} from 'firebase/firestore'
7- import { App , ComponentPublicInstance , toRef } from 'vue'
7+ import { App , ComponentPublicInstance , effectScope , toRef } from 'vue'
88import { isVue3 } from 'vue-demi'
99import {
1010 bindCollection ,
@@ -17,6 +17,8 @@ import { internalUnbind, _useFirestoreRef } from '.'
1717import { ResetOption , UnbindWithReset } from '../shared'
1818import { firebaseApp } from '../../tests/utils'
1919import { FirebaseApp } from 'firebase/app'
20+ import { getGlobalScope } from '../globals'
21+ import { useFirebaseApp } from '../app'
2022
2123export type VueFirestoreObject = Record <
2224 string ,
@@ -60,7 +62,7 @@ const firestorePluginDefaults: Readonly<
6062export const firestorePlugin = function firestorePlugin (
6163 app : App ,
6264 pluginOptions ?: FirestorePluginOptions ,
63- firebaseApp ?: FirebaseApp ,
65+ firebaseApp ?: FirebaseApp
6466) {
6567 // const strategies = app.config.optionMergeStrategies
6668 // TODO: implement
@@ -69,7 +71,7 @@ export const firestorePlugin = function firestorePlugin(
6971 const globalOptions = Object . assign (
7072 { } ,
7173 firestorePluginDefaults ,
72- pluginOptions ,
74+ pluginOptions
7375 )
7476 const { bindName, unbindName } = globalOptions
7577
@@ -79,7 +81,7 @@ export const firestorePlugin = function firestorePlugin(
7981
8082 GlobalTarget [ unbindName ] = function firestoreUnbind (
8183 key : string ,
82- reset ?: FirestoreRefOptions [ 'reset' ] ,
84+ reset ?: FirestoreRefOptions [ 'reset' ]
8385 ) {
8486 internalUnbind ( key , firestoreUnbinds . get ( this ) , reset )
8587 delete this . $firestoreRefs [ key ]
@@ -92,7 +94,7 @@ export const firestorePlugin = function firestorePlugin(
9294 | Query < unknown >
9395 | CollectionReference < unknown >
9496 | DocumentReference < unknown > ,
95- userOptions ?: FirestoreRefOptions ,
97+ userOptions ?: FirestoreRefOptions
9698 ) {
9799 const options = Object . assign ( { } , globalOptions , userOptions )
98100 const target = toRef ( this . $data as any , key )
@@ -105,10 +107,22 @@ export const firestorePlugin = function firestorePlugin(
105107 unbinds [ key ] ( options . reset )
106108 }
107109
108- const { promise, unbind } = _useFirestoreRef ( docOrCollectionRef , {
109- target,
110- ...options ,
111- } )
110+ const scope = getGlobalScope ( firebaseApp || useFirebaseApp ( ) , app ) . run ( ( ) =>
111+ effectScope ( )
112+ ) !
113+
114+ const { promise, unbind : _unbind } = scope . run ( ( ) =>
115+ _useFirestoreRef ( docOrCollectionRef , {
116+ target,
117+ ...options ,
118+ } )
119+ ) !
120+
121+ // override the unbind to also free th variables created
122+ const unbind : UnbindWithReset = ( reset ) => {
123+ _unbind ( reset )
124+ scope . stop ( )
125+ }
112126 unbinds [ key ] = unbind
113127 // @ts -expect-error: we are allowed to write it
114128 this . $firestoreRefs [ key ] =
@@ -131,7 +145,7 @@ export const firestorePlugin = function firestorePlugin(
131145 key ,
132146 // @ts -expect-error: FIXME: there is probably a wrong type in global properties
133147 refs [ key ] ,
134- globalOptions ,
148+ globalOptions
135149 )
136150 }
137151 } ,
@@ -165,7 +179,7 @@ export const firestorePlugin = function firestorePlugin(
165179 * ```
166180 */
167181export function VueFireFirestoreOptionsAPI (
168- pluginOptions ?: FirestorePluginOptions ,
182+ pluginOptions ?: FirestorePluginOptions
169183) {
170184 return ( firebaseApp : FirebaseApp , app : App ) => {
171185 return firestorePlugin ( app , pluginOptions , firebaseApp )
@@ -185,15 +199,15 @@ declare module '@vue/runtime-core' {
185199 name : string ,
186200 // TODO: create proper overloads with generics like in the composition API
187201 reference : Query < unknown > | CollectionReference < unknown > ,
188- options ?: FirestoreRefOptions ,
202+ options ?: FirestoreRefOptions
189203 // TODO: match the promise with the type of internalBind
190204 ) : Promise < DocumentData [ ] >
191205
192206 $firestoreBind (
193207 name : string ,
194208 // TODO: create proper overloads with generics like in the composition API
195209 reference : DocumentReference < unknown > ,
196- options ?: FirestoreRefOptions ,
210+ options ?: FirestoreRefOptions
197211 ) : Promise < DocumentData >
198212
199213 /**
0 commit comments