Skip to content

Commit

Permalink
fix(core): fix the instance cache logic (#2667)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels authored Nov 18, 2020
1 parent 2ce41aa commit 7f89643
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion sample/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {

import { FirestoreComponent } from './firestore/firestore.component';
import { AngularFireDatabaseModule, USE_EMULATOR as USE_DATABASE_EMULATOR } from '@angular/fire/database';
import { AngularFirestoreModule, USE_EMULATOR as USE_FIRESTORE_EMULATOR } from '@angular/fire/firestore';
import { AngularFirestoreModule, USE_EMULATOR as USE_FIRESTORE_EMULATOR, SETTINGS as FIRESTORE_SETTINGS } from '@angular/fire/firestore';
import { AngularFireStorageModule } from '@angular/fire/storage';
import { AngularFireAuthModule, USE_DEVICE_LANGUAGE, USE_EMULATOR as USE_AUTH_EMULATOR } from '@angular/fire/auth';
import { AngularFireMessagingModule, SERVICE_WORKER, VAPID_KEY } from '@angular/fire/messaging';
Expand Down Expand Up @@ -73,6 +73,7 @@ import { FirestoreOfflineModule } from './firestore-offline/firestore-offline.mo
UserTrackingService,
ScreenTrackingService,
PerformanceMonitoringService,
{ provide: FIRESTORE_SETTINGS, useValue: { ignoreUndefinedProperties: true } },
{ provide: ANALYTICS_DEBUG_MODE, useValue: true },
{ provide: COLLECTION_ENABLED, useValue: true },
{ provide: USE_AUTH_EMULATOR, useValue: environment.useEmulators ? ['localhost', 9099] : undefined },
Expand Down
22 changes: 12 additions & 10 deletions src/core/firebase.app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function ɵfirebaseAppFactory(options: FirebaseOptions, zone: NgZone, nam
const app = (existingApp || zone.runOutsideAngular(() => firebase.initializeApp(options, config as any))) as FirebaseApp;
if (JSON.stringify(options) !== JSON.stringify(app.options)) {
const hmr = !!(module as any).hot;
log('error', `${app.toString()} already initialized with different options${hmr ? ', you may need to reload as Firebase is not HMR aware.' : '.'}`);
log('error', `${app.name} Firebase App already initialized with different options${hmr ? ', you may need to reload as Firebase is not HMR aware.' : '.'}`);
}
return app;
}
Expand All @@ -65,16 +65,18 @@ globalThis.ɵAngularfireInstanceCache ||= new Map();

export function ɵfetchInstance<T>(cacheKey: any, moduleName: string, app: FirebaseApp, fn: () => T, args: any[]): T {
const [instance, ...cachedArgs] = globalThis.ɵAngularfireInstanceCache.get(cacheKey) || [];
if (instance && args.some((arg, i) => {
const cachedArg = cachedArgs[i];
if (arg && typeof arg === 'object') {
return JSON.stringify(arg) !== JSON.stringify(cachedArg);
} else {
return arg !== cachedArg;
if (instance) {
if (args.some((arg, i) => {
const cachedArg = cachedArgs[i];
if (arg && typeof arg === 'object') {
return JSON.stringify(arg) !== JSON.stringify(cachedArg);
} else {
return arg !== cachedArg;
}
})) {
const hmr = !!(module as any).hot;
log('error', `${moduleName} was already initialized on the ${app.name} Firebase App instance with different settings.${hmr ? ' You may need to reload as Firebase is not HMR aware.' : ''}`);
}
})) {
const hmr = !!(module as any).hot;
log('error', `${moduleName} was already initialized on the ${app.name} Firebase App instance with different settings.${hmr ? ' You may need to reload as Firebase is not HMR aware.' : ''}`);
return instance;
} else {
const newInstance = fn();
Expand Down

0 comments on commit 7f89643

Please sign in to comment.