Repository navigation
Expand file tree
/
Copy pathfunctions.module.ts
More file actions
71 lines (64 loc) · 2.31 KB
/
Copy pathfunctions.module.ts
File metadata and controls
71 lines (64 loc) · 2.31 KB
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
import { NgModule, Optional, NgZone, InjectionToken, ModuleWithProviders, Injector } from '@angular/core';
import { Functions as FirebaseFunctions } from 'firebase/functions';
import { ɵgetDefaultInstanceOf, ɵAngularFireSchedulers, VERSION } from '@angular/fire';
import { Functions, FunctionsInstances, FUNCTIONS_PROVIDER_NAME } from './functions';
import { FirebaseApps, FirebaseApp } from '@angular/fire/app';
import { AuthInstances } from '@angular/fire/auth';
import { registerVersion } from 'firebase/app';
import { AppCheckInstances } from '@angular/fire/app-check';
export const PROVIDED_FUNCTIONS_INSTANCES = new InjectionToken<Functions[]>('angularfire2.functions-instances');
export function defaultFunctionsInstanceFactory(provided: FirebaseFunctions[]|undefined, defaultApp: FirebaseApp) {
const defaultAuth = ɵgetDefaultInstanceOf<FirebaseFunctions>(FUNCTIONS_PROVIDER_NAME, provided, defaultApp);
return defaultAuth && new Functions(defaultAuth);
}
export function functionsInstanceFactory(fn: (injector: Injector) => FirebaseFunctions) {
return (zone: NgZone, injector: Injector) => {
const functions = zone.runOutsideAngular(() => fn(injector));
return new Functions(functions);
};
}
const FUNCTIONS_INSTANCES_PROVIDER = {
provide: FunctionsInstances,
deps: [
[new Optional(), PROVIDED_FUNCTIONS_INSTANCES ],
]
};
const DEFAULT_FUNCTIONS_INSTANCE_PROVIDER = {
provide: Functions,
useFactory: defaultFunctionsInstanceFactory,
deps: [
[new Optional(), PROVIDED_FUNCTIONS_INSTANCES ],
FirebaseApp,
]
};
@NgModule({
providers: [
DEFAULT_FUNCTIONS_INSTANCE_PROVIDER,
FUNCTIONS_INSTANCES_PROVIDER,
]
})
export class FunctionsModule {
constructor() {
registerVersion('angularfire', VERSION.full, 'fn');
}
}
export function provideFunctions(fn: () => FirebaseFunctions, ...deps: any[]): ModuleWithProviders<FunctionsModule> {
return {
ngModule: FunctionsModule,
providers: [{
provide: PROVIDED_FUNCTIONS_INSTANCES,
useFactory: functionsInstanceFactory(fn),
multi: true,
deps: [
NgZone,
Injector,
ɵAngularFireSchedulers,
FirebaseApps,
// Defensively load Auth first, if provided
[new Optional(), AuthInstances ],
[new Optional(), AppCheckInstances ],
...deps,
]
}]
};
}