Skip to content

Commit 14ebc8c

Browse files
committed
refactor: one scope per app
1 parent e5d09b3 commit 14ebc8c

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

src/app-check/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
onTokenChanged,
66
} from 'firebase/app-check'
77
import { App, inject, InjectionKey, Ref, ref } from 'vue'
8-
import { scope } from '../globals'
8+
import { getGlobalScope } from '../globals'
99

1010
export const AppCheckTokenInjectSymbol: InjectionKey<Ref<string | undefined>> =
1111
Symbol('app-check-token')
@@ -15,9 +15,9 @@ export function useAppCheckToken() {
1515
}
1616

1717
export function VueFireAppCheck(options: AppCheckOptions) {
18-
return (firebaseApp: FirebaseApp | undefined, app: App) => {
18+
return (firebaseApp: FirebaseApp, app: App) => {
1919
const appCheck = initializeAppCheck(firebaseApp, options)
20-
const token = scope.run(() => ref<string>())!
20+
const token = getGlobalScope(app, firebaseApp).run(() => ref<string>())!
2121
onTokenChanged(appCheck, (newToken) => {
2222
token.value = newToken.token
2323
})

src/auth/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { FirebaseApp } from 'firebase/app'
22
import { getAuth, User } from 'firebase/auth'
33
import { App, shallowRef } from 'vue'
44
import { useFirebaseApp } from '../app'
5-
import { scope } from '../globals'
5+
import { getGlobalScope } from '../globals'
66
import { AuthUserInjectSymbol, setupOnAuthStateChanged } from './user'
77

88
export { setupOnAuthStateChanged, useCurrentUser } from './user'
@@ -16,8 +16,10 @@ modules: [VueFireAuth()]`)
1616
}
1717
}
1818

19-
return (firebaseApp: FirebaseApp | undefined, app: App) => {
20-
const user = scope.run(() => shallowRef<User | null | undefined>())!
19+
return (firebaseApp: FirebaseApp, app: App) => {
20+
const user = getGlobalScope(app, firebaseApp).run(() =>
21+
shallowRef<User | null | undefined>()
22+
)!
2123
// userMap.set(app, user)
2224
app.provide(AuthUserInjectSymbol, user)
2325
setupOnAuthStateChanged(user, firebaseApp)

src/globals.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1-
import { effectScope } from 'vue'
1+
import { FirebaseApp } from 'firebase/app'
2+
import { App, EffectScope, effectScope } from 'vue'
23

34
/**
45
* Internal scope for globals
56
* @internal
67
*/
7-
export const scope = effectScope(true)
8+
const scope = effectScope(true)
9+
10+
// @internal
11+
const scopeMap = new WeakMap<FirebaseApp, EffectScope>()
12+
13+
/**
14+
* Gets the VueFire global scope for the current app. Creates one if it doesn't exist.
15+
* @internal
16+
*
17+
* @param app - Vue App
18+
* @returns
19+
*/
20+
export function getGlobalScope(app: App, firebaseApp: FirebaseApp) {
21+
let scope: EffectScope | undefined
22+
// we use the firebaseApp as a key because we are more likely to have access to it and it's supposed to be also unique
23+
// per app since it contains user data.
24+
if (!scopeMap.has(firebaseApp)) {
25+
scope = effectScope(true)
26+
scopeMap.set(firebaseApp, scope)
27+
const { unmount } = app
28+
// dispose up the scope when the app is unmounted
29+
app.unmount = () => {
30+
unmount.call(app)
31+
scope!.stop()
32+
scopeMap.delete(firebaseApp)
33+
}
34+
}
35+
36+
return scopeMap.get(firebaseApp)!
37+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface VueFireOptions {
7373
* Array of VueFire modules that should be added to the application. e.g. `[VueFireAuth, VueFireDatabase]`. Remember
7474
* to import them from `vuefire`.
7575
*/
76-
modules?: Array<(firebaseApp: FirebaseApp | undefined, app: App) => void>
76+
modules?: Array<(firebaseApp: FirebaseApp, app: App) => void>
7777
}
7878

7979
/**

0 commit comments

Comments
 (0)