UNPKG

7.66 kBJavaScriptView Raw
1import * as i0 from '@angular/core';
2import { InjectionToken, isDevMode, NgZone, Optional, VERSION as VERSION$1, PLATFORM_ID, NgModule, Inject } from '@angular/core';
3import firebase from 'firebase/compat/app';
4import { VERSION } from '@angular/fire';
5
6// DEBUG quick debugger function for inline logging that typescript doesn't complain about
7// wrote it for debugging the ɵlazySDKProxy, commenting out for now; should consider exposing a
8// verbose mode for AngularFire in a future release that uses something like this in multiple places
9// usage: () => log('something') || returnValue
10// const log = (...args: any[]): false => { console.log(...args); return false }
11// The problem here are things like ngOnDestroy are missing, then triggering the service
12// rather than dig too far; I'm capturing these as I go.
13const noopFunctions = ['ngOnDestroy'];
14// INVESTIGATE should we make the Proxy revokable and do some cleanup?
15// right now it's fairly simple but I'm sure this will grow in complexity
16const ɵlazySDKProxy = (klass, observable, zone, options = {}) => {
17 return new Proxy(klass, {
18 get: (_, name) => zone.runOutsideAngular(() => {
19 var _a;
20 if (klass[name]) {
21 if ((_a = options === null || options === void 0 ? void 0 : options.spy) === null || _a === void 0 ? void 0 : _a.get) {
22 options.spy.get(name, klass[name]);
23 }
24 return klass[name];
25 }
26 if (noopFunctions.indexOf(name) > -1) {
27 return () => {
28 };
29 }
30 const promise = observable.toPromise().then(mod => {
31 const ret = mod && mod[name];
32 // TODO move to proper type guards
33 if (typeof ret === 'function') {
34 return ret.bind(mod);
35 }
36 else if (ret && ret.then) {
37 return ret.then((res) => zone.run(() => res));
38 }
39 else {
40 return zone.run(() => ret);
41 }
42 });
43 // recurse the proxy
44 return new Proxy(() => { }, {
45 get: (_, name) => promise[name],
46 // TODO handle callbacks as transparently as I can
47 apply: (self, _, args) => promise.then(it => {
48 var _a;
49 const res = it && it(...args);
50 if ((_a = options === null || options === void 0 ? void 0 : options.spy) === null || _a === void 0 ? void 0 : _a.apply) {
51 options.spy.apply(name, args, res);
52 }
53 return res;
54 })
55 });
56 })
57 });
58};
59const ɵapplyMixins = (derivedCtor, constructors) => {
60 constructors.forEach((baseCtor) => {
61 Object.getOwnPropertyNames(baseCtor.prototype || baseCtor).forEach((name) => {
62 Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype || baseCtor, name));
63 });
64 });
65};
66
67class FirebaseApp {
68 constructor(app) {
69 return app;
70 }
71}
72
73const FIREBASE_OPTIONS = new InjectionToken('angularfire2.app.options');
74const FIREBASE_APP_NAME = new InjectionToken('angularfire2.app.name');
75function ɵfirebaseAppFactory(options, zone, nameOrConfig) {
76 const name = typeof nameOrConfig === 'string' && nameOrConfig || '[DEFAULT]';
77 const config = typeof nameOrConfig === 'object' && nameOrConfig || {};
78 config.name = config.name || name;
79 // Added any due to some inconsistency between @firebase/app and firebase types
80 const existingApp = firebase.apps.filter(app => app && app.name === config.name)[0];
81 // We support FirebaseConfig, initializeApp's public type only accepts string; need to cast as any
82 // Could be solved with https://github.com/firebase/firebase-js-sdk/pull/1206
83 const app = (existingApp || zone.runOutsideAngular(() => firebase.initializeApp(options, config)));
84 try {
85 if (JSON.stringify(options) !== JSON.stringify(app.options)) {
86 const hmr = !!module.hot;
87 log$1('error', `${app.name} Firebase App already initialized with different options${hmr ? ', you may need to reload as Firebase is not HMR aware.' : '.'}`);
88 }
89 }
90 catch (e) { }
91 return new FirebaseApp(app);
92}
93const log$1 = (level, ...args) => {
94 if (isDevMode() && typeof console !== 'undefined') {
95 console[level](...args);
96 }
97};
98const FIREBASE_APP_PROVIDER = {
99 provide: FirebaseApp,
100 useFactory: ɵfirebaseAppFactory,
101 deps: [
102 FIREBASE_OPTIONS,
103 NgZone,
104 [new Optional(), FIREBASE_APP_NAME]
105 ]
106};
107class AngularFireModule {
108 // tslint:disable-next-line:ban-types
109 constructor(platformId) {
110 firebase.registerVersion('angularfire', VERSION.full, 'core');
111 firebase.registerVersion('angularfire', VERSION.full, 'app-compat');
112 firebase.registerVersion('angular', VERSION$1.full, platformId.toString());
113 }
114 static initializeApp(options, nameOrConfig) {
115 return {
116 ngModule: AngularFireModule,
117 providers: [
118 { provide: FIREBASE_OPTIONS, useValue: options },
119 { provide: FIREBASE_APP_NAME, useValue: nameOrConfig }
120 ]
121 };
122 }
123}
124AngularFireModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireModule, deps: [{ token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.NgModule });
125AngularFireModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireModule });
126AngularFireModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireModule, providers: [FIREBASE_APP_PROVIDER] });
127i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.3", ngImport: i0, type: AngularFireModule, decorators: [{
128 type: NgModule,
129 args: [{
130 providers: [FIREBASE_APP_PROVIDER]
131 }]
132 }], ctorParameters: function () { return [{ type: Object, decorators: [{
133 type: Inject,
134 args: [PLATFORM_ID]
135 }] }]; } });
136
137function ɵcacheInstance(cacheKey, moduleName, appName, fn, deps) {
138 const [, instance, cachedDeps] = globalThis.ɵAngularfireInstanceCache.find((it) => it[0] === cacheKey) || [];
139 if (instance) {
140 if (!matchDep(deps, cachedDeps)) {
141 log('error', `${moduleName} was already initialized on the ${appName} Firebase App with different settings.${IS_HMR ? ' You may need to reload as Firebase is not HMR aware.' : ''}`);
142 log('warn', { is: deps, was: cachedDeps });
143 }
144 return instance;
145 }
146 else {
147 const newInstance = fn();
148 globalThis.ɵAngularfireInstanceCache.push([cacheKey, newInstance, deps]);
149 return newInstance;
150 }
151}
152function matchDep(a, b) {
153 try {
154 return a.toString() === b.toString();
155 }
156 catch (_) {
157 return a === b;
158 }
159}
160const IS_HMR = !!module.hot;
161const log = (level, ...args) => {
162 if (isDevMode() && typeof console !== 'undefined') {
163 console[level](...args);
164 }
165};
166globalThis.ɵAngularfireInstanceCache || (globalThis.ɵAngularfireInstanceCache = []);
167
168/**
169 * Generated bundle index. Do not edit.
170 */
171
172export { AngularFireModule, FIREBASE_APP_NAME, FIREBASE_OPTIONS, FirebaseApp, ɵapplyMixins, ɵcacheInstance, ɵfirebaseAppFactory, ɵlazySDKProxy };
173//# sourceMappingURL=angular-fire-compat.js.map