Skip to content

Commit 5d244b7

Browse files
committed
fix(options-api): cleanup variables
1 parent 908f6c3 commit 5d244b7

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

src/auth/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ modules: [VueFireAuth()]`)
3636

3737
return (firebaseApp: FirebaseApp, app: App) => {
3838
const user = getGlobalScope(firebaseApp, app).run(() =>
39-
shallowRef<User | null | undefined>(),
39+
shallowRef<User | null | undefined>()
4040
)!
4141
// userMap.set(app, user)
4242
app.provide(AuthUserInjectSymbol, user)

src/database/optionsApi.ts

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { FirebaseApp } from 'firebase/app'
22
import { DatabaseReference, DataSnapshot, Query } from 'firebase/database'
3-
import { App, ComponentPublicInstance, toRef } from 'vue'
3+
import { App, ComponentPublicInstance, effectScope, toRef } from 'vue'
44
import { isVue3 } from 'vue-demi'
5+
import { useFirebaseApp } from '../app'
6+
import { getGlobalScope } from '../globals'
57
import { ResetOption, UnbindWithReset } from '../shared'
68
import { internalUnbind, _useDatabaseRef } from './index'
79
import {
@@ -49,7 +51,7 @@ export const rtdbUnbinds = new WeakMap<
4951
export function databasePlugin(
5052
app: App,
5153
pluginOptions?: DatabasePluginOptions,
52-
firebaseApp?: FirebaseApp,
54+
firebaseApp?: FirebaseApp
5355
) {
5456
// TODO: implement
5557
// const strategies = Vue.config.optionMergeStrategies
@@ -64,7 +66,7 @@ export function databasePlugin(
6466

6567
GlobalTarget[unbindName] = function rtdbUnbind(
6668
key: string,
67-
reset?: ResetOption,
69+
reset?: ResetOption
6870
) {
6971
internalUnbind(key, rtdbUnbinds.get(this), reset)
7072
delete this.$firebaseRefs[key]
@@ -75,7 +77,7 @@ export function databasePlugin(
7577
this: ComponentPublicInstance,
7678
key: string,
7779
source: DatabaseReference | Query,
78-
userOptions?: _DatabaseRefOptions,
80+
userOptions?: _DatabaseRefOptions
7981
) {
8082
const options = Object.assign({}, globalOptions, userOptions)
8183
const target = toRef(this.$data as any, key)
@@ -88,8 +90,19 @@ export function databasePlugin(
8890
unbinds[key](options.reset)
8991
}
9092

91-
// FIXME: Create a single scopeEffect per instance that wraps thin call and stop the effect scope when `unbind()` is called
92-
const { promise, unbind } = _useDatabaseRef(source, { target, ...options })
93+
const scope = getGlobalScope(firebaseApp || useFirebaseApp(), app).run(() =>
94+
effectScope()
95+
)!
96+
97+
const { promise, unbind: _unbind } = scope.run(() =>
98+
_useDatabaseRef(source, { target, ...options })
99+
)!
100+
101+
// override the unbind to also free th variables created
102+
const unbind: UnbindWithReset = (reset) => {
103+
_unbind(reset)
104+
scope.stop()
105+
}
93106
unbinds[key] = unbind
94107

95108
// we make it readonly for the user but we must change it. Maybe there is a way to have an internal type here but expose a readonly type through a d.ts
@@ -117,7 +130,7 @@ export function databasePlugin(
117130
// ts
118131
key,
119132
bindings[key],
120-
globalOptions,
133+
globalOptions
121134
)
122135
}
123136
},
@@ -151,7 +164,7 @@ export function databasePlugin(
151164
* ```
152165
*/
153166
export function VueFireDatabaseOptionsAPI(
154-
pluginOptions?: DatabasePluginOptions,
167+
pluginOptions?: DatabasePluginOptions
155168
) {
156169
return (firebaseApp: FirebaseApp, app: App) => {
157170
return databasePlugin(app, pluginOptions, firebaseApp)
@@ -172,7 +185,7 @@ declare module '@vue/runtime-core' {
172185
$rtdbBind(
173186
name: string,
174187
reference: DatabaseReference | Query,
175-
options?: _DatabaseRefOptions,
188+
options?: _DatabaseRefOptions
176189
): Promise<DataSnapshot>
177190

178191
/**

src/firestore/optionsApi.ts

+27-13
Original file line numberDiff line numberDiff line change
@@ -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'
88
import { isVue3 } from 'vue-demi'
99
import {
1010
bindCollection,
@@ -17,6 +17,8 @@ import { internalUnbind, _useFirestoreRef } from '.'
1717
import { ResetOption, UnbindWithReset } from '../shared'
1818
import { firebaseApp } from '../../tests/utils'
1919
import { FirebaseApp } from 'firebase/app'
20+
import { getGlobalScope } from '../globals'
21+
import { useFirebaseApp } from '../app'
2022

2123
export type VueFirestoreObject = Record<
2224
string,
@@ -60,7 +62,7 @@ const firestorePluginDefaults: Readonly<
6062
export 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
*/
167181
export 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

Comments
 (0)