/
firebase.module.ts
103 lines (94 loc) · 2.98 KB
/
firebase.module.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { InjectionToken, Injector, ModuleWithProviders, NgModule } from '@angular/core'
import { FirebaseApp, FirebaseAppConfig } from './firebase-app.service'
import { FirebaseAuth } from './firebase-auth.service'
import { FirebaseDatabase } from './firebase-database.service'
/** @internal */
let lazyInvocation = true
/** @internal */
export function invokeLazy(): boolean {
return lazyInvocation
}
/**
* Enable or disable lazy invocation of firebase operations.
*
* Since the library focuses on observables, all operations are invoked lazily as is usually the
* case with observables. This means for example, calling `someRef.set({ foo: 'bar' })` will do
* nothing without either subscribing to the returned observable or calling `toPromise()` on it.
*
* This is in contrast to the Firebase Web-API which starts the operation when the function is
* called. It is possible to globally configure the library to behave like the native Firebase
* Web-API by calling `setLazyInvocation(false)`
*
* @param lazy
*/
export function setLazyInvocation(lazy: boolean) {
lazyInvocation = lazy
}
/** @internal */
export function appFactory(injector: Injector, config: FirebaseAppConfig) {
return new FirebaseApp(config, injector)
}
/** @internal */
export function authFactory(app: FirebaseApp) {
return app.auth()
}
/** @internal */
export function databaseFactory(app: FirebaseApp) {
return app.database()
}
@NgModule({})
export class FirebaseModule {
/**
* Provides a firebase app which will be be injected for {@link FirebaseApp}. Further the app's
* {@link FirebaseAuth} and {@link FirebaseDatabase} can be injected in the same way.
* @param config Firebase app config.
*/
static primaryApp(config: FirebaseAppConfig): ModuleWithProviders {
return {
ngModule: FirebaseModule,
providers: [
{
provide: 'firebase-config-' + config.options.apiKey,
useValue: config
},
{
provide: FirebaseApp,
useFactory: appFactory,
deps: [Injector, 'firebase-config-' + config.options.apiKey]
},
{
provide: FirebaseAuth,
useFactory: authFactory,
deps: [FirebaseApp]
},
{
provide: FirebaseDatabase,
useFactory: databaseFactory,
deps: [FirebaseApp]
}
]
}
}
/**
* Provides a {@link FirebaseApp} which will be injected for token.
*
* @param token Token for which {@link FirebaseApp} will be injected.
* @param config Firebase app config.
*/
static secondaryApp(token: InjectionToken<FirebaseApp>, config: FirebaseAppConfig) {
return {
ngModule: FirebaseModule,
providers: [
{
provide: 'firebase-config-' + config.options.apiKey,
useValue: config
},
{
provide: token,
useFactory: appFactory,
deps: [Injector, 'firebase-config-' + config.options.apiKey,]
}
]
}
}
}