-
-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathglobals.ts
30 lines (27 loc) · 900 Bytes
/
globals.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import type { FirebaseApp } from 'firebase/app'
import { App, EffectScope, effectScope } from 'vue-demi'
// @internal
const scopeMap = new WeakMap<FirebaseApp, EffectScope>()
/**
* Gets the VueFire global scope for the current app. Creates one if it doesn't exist.
* @internal
*
* @param app - Vue App
* @returns
*/
export function getGlobalScope(firebaseApp: FirebaseApp, app: App) {
// 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
// per app since it contains user data.
if (!scopeMap.has(firebaseApp)) {
const scope = effectScope(true)
scopeMap.set(firebaseApp, scope)
const { unmount } = app
// dispose up the scope when the app is unmounted
app.unmount = () => {
unmount.call(app)
scope.stop()
scopeMap.delete(firebaseApp)
}
}
return scopeMap.get(firebaseApp)!
}