@@ -4,7 +4,7 @@ import type {
4
4
DocumentData ,
5
5
DocumentReference ,
6
6
} from 'firebase/firestore'
7
- import { App , ComponentPublicInstance , toRef } from 'vue'
7
+ import { App , ComponentPublicInstance , effectScope , toRef } from 'vue'
8
8
import { isVue3 } from 'vue-demi'
9
9
import {
10
10
bindCollection ,
@@ -17,6 +17,8 @@ import { internalUnbind, _useFirestoreRef } from '.'
17
17
import { ResetOption , UnbindWithReset } from '../shared'
18
18
import { firebaseApp } from '../../tests/utils'
19
19
import { FirebaseApp } from 'firebase/app'
20
+ import { getGlobalScope } from '../globals'
21
+ import { useFirebaseApp } from '../app'
20
22
21
23
export type VueFirestoreObject = Record <
22
24
string ,
@@ -60,7 +62,7 @@ const firestorePluginDefaults: Readonly<
60
62
export const firestorePlugin = function firestorePlugin (
61
63
app : App ,
62
64
pluginOptions ?: FirestorePluginOptions ,
63
- firebaseApp ?: FirebaseApp ,
65
+ firebaseApp ?: FirebaseApp
64
66
) {
65
67
// const strategies = app.config.optionMergeStrategies
66
68
// TODO: implement
@@ -69,7 +71,7 @@ export const firestorePlugin = function firestorePlugin(
69
71
const globalOptions = Object . assign (
70
72
{ } ,
71
73
firestorePluginDefaults ,
72
- pluginOptions ,
74
+ pluginOptions
73
75
)
74
76
const { bindName, unbindName } = globalOptions
75
77
@@ -79,7 +81,7 @@ export const firestorePlugin = function firestorePlugin(
79
81
80
82
GlobalTarget [ unbindName ] = function firestoreUnbind (
81
83
key : string ,
82
- reset ?: FirestoreRefOptions [ 'reset' ] ,
84
+ reset ?: FirestoreRefOptions [ 'reset' ]
83
85
) {
84
86
internalUnbind ( key , firestoreUnbinds . get ( this ) , reset )
85
87
delete this . $firestoreRefs [ key ]
@@ -92,7 +94,7 @@ export const firestorePlugin = function firestorePlugin(
92
94
| Query < unknown >
93
95
| CollectionReference < unknown >
94
96
| DocumentReference < unknown > ,
95
- userOptions ?: FirestoreRefOptions ,
97
+ userOptions ?: FirestoreRefOptions
96
98
) {
97
99
const options = Object . assign ( { } , globalOptions , userOptions )
98
100
const target = toRef ( this . $data as any , key )
@@ -105,10 +107,22 @@ export const firestorePlugin = function firestorePlugin(
105
107
unbinds [ key ] ( options . reset )
106
108
}
107
109
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
+ }
112
126
unbinds [ key ] = unbind
113
127
// @ts -expect-error: we are allowed to write it
114
128
this . $firestoreRefs [ key ] =
@@ -131,7 +145,7 @@ export const firestorePlugin = function firestorePlugin(
131
145
key ,
132
146
// @ts -expect-error: FIXME: there is probably a wrong type in global properties
133
147
refs [ key ] ,
134
- globalOptions ,
148
+ globalOptions
135
149
)
136
150
}
137
151
} ,
@@ -165,7 +179,7 @@ export const firestorePlugin = function firestorePlugin(
165
179
* ```
166
180
*/
167
181
export function VueFireFirestoreOptionsAPI (
168
- pluginOptions ?: FirestorePluginOptions ,
182
+ pluginOptions ?: FirestorePluginOptions
169
183
) {
170
184
return ( firebaseApp : FirebaseApp , app : App ) => {
171
185
return firestorePlugin ( app , pluginOptions , firebaseApp )
@@ -185,15 +199,15 @@ declare module '@vue/runtime-core' {
185
199
name : string ,
186
200
// TODO: create proper overloads with generics like in the composition API
187
201
reference : Query < unknown > | CollectionReference < unknown > ,
188
- options ?: FirestoreRefOptions ,
202
+ options ?: FirestoreRefOptions
189
203
// TODO: match the promise with the type of internalBind
190
204
) : Promise < DocumentData [ ] >
191
205
192
206
$firestoreBind (
193
207
name : string ,
194
208
// TODO: create proper overloads with generics like in the composition API
195
209
reference : DocumentReference < unknown > ,
196
- options ?: FirestoreRefOptions ,
210
+ options ?: FirestoreRefOptions
197
211
) : Promise < DocumentData >
198
212
199
213
/**
0 commit comments