Skip to content

Commit

Permalink
Drop isSupported injection, bump rxfire (#3588)
Browse files Browse the repository at this point in the history
* Bump rxfire dep
* Drop isSupported inject in favor of PLATFORM_ID test
* Analytics is technically isPlatformBrowser rather than !isPlatformServer, prolly no diff
  • Loading branch information
jamesdaniels authored Dec 12, 2024
1 parent e18d8da commit 6b1b0ca
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 186 deletions.
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"jsonc-parser": "^3.0.0",
"open": "^7.0.3 || ^8.0.0",
"ora": "^5.3.0",
"rxfire": "^6.0.5",
"rxfire": "^6.1.0",
"rxjs": "~7.8.0",
"semver": "^7.1.3",
"triple-beam": "^1.3.0",
Expand Down
26 changes: 9 additions & 17 deletions src/analytics/analytics.module.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { isPlatformBrowser } from '@angular/common';
import {
APP_INITIALIZER,
EnvironmentProviders,
InjectionToken,
Injector,
NgModule,
NgZone,
Optional,
PLATFORM_ID,
makeEnvironmentProviders,
} from '@angular/core';
import { VERSION, ɵAngularFireSchedulers, ɵgetDefaultInstanceOf } from '@angular/fire';
import { FirebaseApp, FirebaseApps } from '@angular/fire/app';
import { Analytics as FirebaseAnalytics } from 'firebase/analytics';
import { registerVersion } from 'firebase/app';
import { ANALYTICS_PROVIDER_NAME, Analytics, AnalyticsInstances } from './analytics';
import { isAnalyticsSupportedFactory } from './is-analytics-supported-factory';
import { ScreenTrackingService } from './screen-tracking.service';
import { UserTrackingService } from './user-tracking.service';

export const PROVIDED_ANALYTICS_INSTANCES = new InjectionToken<Analytics[]>('angularfire2.analytics-instances');

export function defaultAnalyticsInstanceFactory(provided: FirebaseAnalytics[]|undefined, defaultApp: FirebaseApp) {
if (!isAnalyticsSupportedFactory.sync()) { return null; }
export function defaultAnalyticsInstanceFactory(provided: FirebaseAnalytics[]|undefined, defaultApp: FirebaseApp, platformId: object) {
if (!isPlatformBrowser(platformId)) { return null; }
const defaultAnalytics = ɵgetDefaultInstanceOf<FirebaseAnalytics>(ANALYTICS_PROVIDER_NAME, provided, defaultApp);
return defaultAnalytics && new Analytics(defaultAnalytics);
}

export function analyticsInstanceFactory(fn: (injector: Injector) => FirebaseAnalytics) {
return (zone: NgZone, injector: Injector) => {
if (!isAnalyticsSupportedFactory.sync()) { return null; }
return (zone: NgZone, injector: Injector, platformId: object) => {
if (!isPlatformBrowser(platformId)) { return null; }
const analytics = zone.runOutsideAngular(() => fn(injector));
return new Analytics(analytics);
};
Expand All @@ -46,18 +46,14 @@ const DEFAULT_ANALYTICS_INSTANCE_PROVIDER = {
deps: [
[new Optional(), PROVIDED_ANALYTICS_INSTANCES ],
FirebaseApp,
PLATFORM_ID,
]
};

@NgModule({
providers: [
DEFAULT_ANALYTICS_INSTANCE_PROVIDER,
ANALYTICS_INSTANCES_PROVIDER,
{
provide: APP_INITIALIZER,
useValue: isAnalyticsSupportedFactory.async,
multi: true,
}
ANALYTICS_INSTANCES_PROVIDER
]
})
export class AnalyticsModule {
Expand All @@ -75,18 +71,14 @@ export function provideAnalytics(fn: (injector: Injector) => FirebaseAnalytics,
return makeEnvironmentProviders([
DEFAULT_ANALYTICS_INSTANCE_PROVIDER,
ANALYTICS_INSTANCES_PROVIDER,
{
provide: APP_INITIALIZER,
useValue: isAnalyticsSupportedFactory.async,
multi: true,
},
{
provide: PROVIDED_ANALYTICS_INSTANCES,
useFactory: analyticsInstanceFactory(fn),
multi: true,
deps: [
NgZone,
Injector,
PLATFORM_ID,
ɵAngularFireSchedulers,
FirebaseApps,
...deps,
Expand Down
10 changes: 1 addition & 9 deletions src/analytics/analytics.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed } from '@angular/core/testing';
import { Analytics, getAnalytics, isSupported, provideAnalytics } from '@angular/fire/analytics';
import { Analytics, getAnalytics, provideAnalytics } from '@angular/fire/analytics';
import { FirebaseApp, getApp, initializeApp, provideFirebaseApp } from '@angular/fire/app';
import { COMMON_CONFIG_TOO } from '../test-config';
import { rando } from '../utils';
Expand All @@ -10,14 +10,6 @@ describe('Analytics', () => {
let providedAnalytics: Analytics;
let appName: string;

beforeAll(done => {
// The APP_INITIALIZER that is making isSupported() sync for DI may not
// be done evaulating by the time we inject from the TestBed. We can
// ensure correct behavior by waiting for the (global) isSuppported() promise
// to resolve.
isSupported().then(() => done());
});

describe('single injection', () => {

beforeEach(() => {
Expand Down
6 changes: 2 additions & 4 deletions src/analytics/firebase.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions src/analytics/is-analytics-supported-factory.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/analytics/overrides.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/auth/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Auth', () => {
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
provideAuth(() => {
providedAuth = getAuth(getApp(appName));
connectAuthEmulator(providedAuth, `http://localhost:${authEmulatorPort}`);
connectAuthEmulator(providedAuth, `http://localhost:${authEmulatorPort}`, { disableWarnings: true });
return providedAuth;
}),
],
Expand Down
4 changes: 2 additions & 2 deletions src/compat/analytics/user-tracking.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPlatformServer } from '@angular/common';
import { isPlatformBrowser } from '@angular/common';
import { Inject, Injectable, NgZone, OnDestroy, PLATFORM_ID } from '@angular/core';
import { VERSION } from '@angular/fire';
import { AngularFireAuth } from '@angular/fire/compat/auth';
Expand All @@ -21,7 +21,7 @@ export class UserTrackingService implements OnDestroy {
zone: NgZone,
) {
firebase.registerVersion('angularfire', VERSION.full, 'compat-user-tracking');
if (!isPlatformServer(platformId)) {
if (isPlatformBrowser(platformId)) {
let resolveInitialized;
this.initialized = zone.runOutsideAngular(() => new Promise(resolve => resolveInitialized = resolve));
this.disposables = [
Expand Down
6 changes: 2 additions & 4 deletions src/messaging/firebase.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions src/messaging/is-messaging-supported-factory.ts

This file was deleted.

25 changes: 9 additions & 16 deletions src/messaging/messaging.module.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import { isPlatformServer } from '@angular/common';
import {
APP_INITIALIZER, EnvironmentProviders,
EnvironmentProviders,
InjectionToken,
Injector,
NgModule,
NgZone,
Optional,
PLATFORM_ID,
makeEnvironmentProviders,
} from '@angular/core';
import { VERSION, ɵAngularFireSchedulers, ɵgetDefaultInstanceOf } from '@angular/fire';
import { FirebaseApp, FirebaseApps } from '@angular/fire/app';
import { registerVersion } from 'firebase/app';
import { Messaging as FirebaseMessaging } from 'firebase/messaging';
import { isMessagingSupportedFactory } from './is-messaging-supported-factory';
import { MESSAGING_PROVIDER_NAME, Messaging, MessagingInstances } from './messaging';

const PROVIDED_MESSAGING_INSTANCES = new InjectionToken<Messaging[]>('angularfire2.messaging-instances');

export function defaultMessagingInstanceFactory(provided: FirebaseMessaging[]|undefined, defaultApp: FirebaseApp) {
if (!isMessagingSupportedFactory.sync()) { return null; }
export function defaultMessagingInstanceFactory(provided: FirebaseMessaging[]|undefined, defaultApp: FirebaseApp, platformId: object) {
if (isPlatformServer(platformId)) { return null; }
const defaultMessaging = ɵgetDefaultInstanceOf<FirebaseMessaging>(MESSAGING_PROVIDER_NAME, provided, defaultApp);
return defaultMessaging && new Messaging(defaultMessaging);
}

export function messagingInstanceFactory(fn: (injector: Injector) => FirebaseMessaging) {
return (zone: NgZone, injector: Injector) => {
if (!isMessagingSupportedFactory.sync()) { return null; }
return (zone: NgZone, injector: Injector, platformId: object) => {
if (isPlatformServer(platformId)) { return null; }
const messaging = zone.runOutsideAngular(() => fn(injector));
return new Messaging(messaging);
};
Expand All @@ -43,18 +44,14 @@ const DEFAULT_MESSAGING_INSTANCE_PROVIDER = {
deps: [
[new Optional(), PROVIDED_MESSAGING_INSTANCES ],
FirebaseApp,
PLATFORM_ID,
]
};

@NgModule({
providers: [
DEFAULT_MESSAGING_INSTANCE_PROVIDER,
MESSAGING_INSTANCES_PROVIDER,
{
provide: APP_INITIALIZER,
useValue: isMessagingSupportedFactory.async,
multi: true,
},
]
})
export class MessagingModule {
Expand All @@ -69,18 +66,14 @@ export function provideMessaging(fn: (injector: Injector) => FirebaseMessaging,
return makeEnvironmentProviders([
DEFAULT_MESSAGING_INSTANCE_PROVIDER,
MESSAGING_INSTANCES_PROVIDER,
{
provide: APP_INITIALIZER,
useValue: isMessagingSupportedFactory.async,
multi: true,
},
{
provide: PROVIDED_MESSAGING_INSTANCES,
useFactory: messagingInstanceFactory(fn),
multi: true,
deps: [
NgZone,
Injector,
PLATFORM_ID,
ɵAngularFireSchedulers,
FirebaseApps,
...deps,
Expand Down
29 changes: 9 additions & 20 deletions src/messaging/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ describe('Messaging', () => {
let providedMessaging: Messaging;
let appName: string;

beforeAll(done => {
// The APP_INITIALIZER that is making isSupported() sync for DI may not
// be done evaulating by the time we inject from the TestBed. We can
// ensure correct behavior by waiting for the (global) isSuppported() promise
// to resolve.
isSupported().then(() => done());
});

describe('single injection', () => {

beforeEach(() => {
Expand All @@ -33,18 +25,15 @@ describe('Messaging', () => {
messaging = TestBed.inject(Messaging);
});

it('should be injectable', done => {
(async () => {
const supported = await isSupported();
if (supported) {
expect(providedMessaging).toBeTruthy();
expect(messaging).toEqual(providedMessaging);
} else {
expect(providedMessaging).toBeUndefined();
expect(messaging).toBeNull();
}
done();
})();
it('should be injectable', async () => {
const supported = await TestBed.runInInjectionContext(isSupported);
if (supported) {
expect(providedMessaging).toBeTruthy();
expect(messaging).toEqual(providedMessaging);
} else {
expect(providedMessaging).toBeUndefined();
expect(messaging).toBeNull();
}
});

});
Expand Down
3 changes: 0 additions & 3 deletions src/messaging/overrides.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"dependencies": {
"firebase": "^11.0.2",
"rxfire": "^6.0.5",
"rxfire": "^6.1.0",
"@angular-devkit/schematics": "^19.0.0",
"@schematics/angular": "^19.0.0",
"tslib": "^2.3.0"
Expand Down
Loading

0 comments on commit 6b1b0ca

Please sign in to comment.