Skip to content

Commit c593ecd

Browse files
fix: stop twelve wrapped calls from holding the app unstable (#3827)
ɵzoneWrap holds a pending task from the moment a wrapped call gets a callback until that callback first runs. For onLog, onTokenChanged, the Database child listeners (onChildAdded on an empty node, the other three until a later change), off, enableLogging, and the rxfire streams stateChanges and auditTrail, nothing guarantees that first run, so ApplicationRef.isStable can stay false for good and an SSR build's route extraction times out, as #3748 reported for beforeAuthStateChanged. They keep logLevel WARN, so only the pending task changes. isSupported in remote-config is the twelfth: #3613 put its blockUntilFirst: false entry in the rxfire block, which exports no isSupported, so it never applied, and #3759 removed the dead entry. Server renders no longer wait for onChildAdded's first batch (onValue, object and list still wait), and off(ref, type, cb) still detaches nothing, because the wrapper replaces the callback it is given. fromRef takes the event to listen for, and one blockUntilFirst value cannot fit both uses: value always fires on attach, while a child event may never fire. A hand-written fromRef in overrides.ts wraps rxfire's fromRef both ways and picks by event, so fromRef(ref, ListenEvent.value) holds a server render like object(ref) and child events do not. Fixes #3826
1 parent 7206aef commit c593ecd

11 files changed

Lines changed: 229 additions & 24 deletions

File tree

‎src/app-check/app-check.spec.ts‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { PLATFORM_ID } from '@angular/core';
1+
import { PLATFORM_ID, PendingTasks } from '@angular/core';
22
import { TestBed } from '@angular/core/testing';
3-
import { FirebaseApp, getApp, initializeApp, provideFirebaseApp } from '@angular/fire/app';
4-
import { APP_CHECK_ON_SERVER, AppCheck, CustomProvider, initializeAppCheck, provideAppCheck } from '@angular/fire/app-check';
3+
import { FirebaseApp, deleteApp, getApp, initializeApp, provideFirebaseApp } from '@angular/fire/app';
4+
import { APP_CHECK_ON_SERVER, AppCheck, CustomProvider, initializeAppCheck, onTokenChanged, provideAppCheck } from '@angular/fire/app-check';
55
import { Auth, connectAuthEmulator, getAuth, provideAuth } from '@angular/fire/auth';
66
import { COMMON_CONFIG } from '../test-config';
77
import { rando } from '../utils';
@@ -109,4 +109,39 @@ describe('AppCheck', () => {
109109
expect(appCheck.app).toEqual(TestBed.inject(FirebaseApp));
110110
});
111111

112+
describe('zone wrapping', () => {
113+
let appCheck: AppCheck;
114+
let addTask: jasmine.Spy;
115+
let cleanups: (() => void)[];
116+
117+
beforeEach(() => {
118+
TestBed.configureTestingModule({
119+
providers: [
120+
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
121+
provideTestAppCheck(),
122+
],
123+
});
124+
appCheck = TestBed.inject(AppCheck);
125+
addTask = spyOn(TestBed.inject(PendingTasks), 'add').and.callThrough();
126+
cleanups = [];
127+
});
128+
129+
afterEach(() => {
130+
// Listeners re-enter this test's injector, so none may outlive the TestBed.
131+
cleanups.forEach(cleanup => cleanup());
132+
});
133+
134+
it('onTokenChanged does not hold the application unstable', () => {
135+
const unsubscribe = TestBed.runInInjectionContext(() => onTokenChanged(appCheck, () => undefined));
136+
cleanups.push(unsubscribe);
137+
expect(addTask).not.toHaveBeenCalled();
138+
});
139+
140+
it('a blocking call in the same context does register a task', async () => {
141+
await TestBed.runInInjectionContext(() => deleteApp(initializeApp(COMMON_CONFIG, rando())));
142+
expect(addTask).toHaveBeenCalled();
143+
});
144+
145+
});
146+
112147
});

‎src/app-check/firebase.ts‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/app/app.spec.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { PendingTasks } from '@angular/core';
12
import { TestBed } from '@angular/core/testing';
2-
import { FirebaseApp, initializeApp, provideFirebaseApp } from '@angular/fire/app';
3+
import { FirebaseApp, deleteApp, initializeApp, onLog, provideFirebaseApp } from '@angular/fire/app';
34
import { COMMON_CONFIG } from '../test-config';
45
import { rando } from '../utils';
56

@@ -30,4 +31,28 @@ describe('FirebaseApp', () => {
3031

3132
});
3233

34+
describe('zone wrapping', () => {
35+
let addTask: jasmine.Spy;
36+
37+
beforeEach(() => {
38+
addTask = spyOn(TestBed.inject(PendingTasks), 'add').and.callThrough();
39+
});
40+
41+
afterEach(() => {
42+
// The handler onLog installs re-enters this test's injector, so it must not outlive the TestBed.
43+
TestBed.runInInjectionContext(() => onLog(null));
44+
});
45+
46+
it('onLog does not hold the application unstable', () => {
47+
TestBed.runInInjectionContext(() => onLog(() => undefined));
48+
expect(addTask).not.toHaveBeenCalled();
49+
});
50+
51+
it('a blocking call in the same context does register a task', async () => {
52+
await TestBed.runInInjectionContext(() => deleteApp(initializeApp(COMMON_CONFIG, rando())));
53+
expect(addTask).toHaveBeenCalled();
54+
});
55+
56+
});
57+
3358
});

‎src/app/firebase.ts‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/database/database.spec.ts‎

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1+
import { PendingTasks } from '@angular/core';
12
import { TestBed } from '@angular/core/testing';
23
import { FirebaseApp, getApp, initializeApp, provideFirebaseApp } from '@angular/fire/app';
3-
import { Database, connectDatabaseEmulator, getDatabase, provideDatabase } from '@angular/fire/database';
4-
import { COMMON_CONFIG } from '../test-config';
4+
import {
5+
DataSnapshot,
6+
Database,
7+
ListenEvent,
8+
Query,
9+
Unsubscribe,
10+
auditTrail,
11+
connectDatabaseEmulator,
12+
enableLogging,
13+
fromRef,
14+
getDatabase,
15+
list,
16+
off,
17+
onChildAdded,
18+
onChildChanged,
19+
onChildMoved,
20+
onChildRemoved,
21+
onValue,
22+
provideDatabase,
23+
ref,
24+
stateChanges,
25+
} from '@angular/fire/database';
26+
import { COMMON_CONFIG, databaseEmulatorPort } from '../test-config';
527
import { rando } from '../utils';
628

729
describe('Database', () => {
@@ -19,7 +41,7 @@ describe('Database', () => {
1941
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
2042
provideDatabase(() => {
2143
providedDatabase = getDatabase(getApp(appName));
22-
connectDatabaseEmulator(providedDatabase, 'localhost', 9002);
44+
connectDatabaseEmulator(providedDatabase, 'localhost', databaseEmulatorPort);
2345
return providedDatabase;
2446
}),
2547
],
@@ -34,6 +56,86 @@ describe('Database', () => {
3456
expect(database.app).toEqual(app);
3557
});
3658

59+
describe('zone wrapping', () => {
60+
let addTask: jasmine.Spy;
61+
let cleanups: (() => void)[];
62+
63+
beforeEach(() => {
64+
addTask = spyOn(TestBed.inject(PendingTasks), 'add').and.callThrough();
65+
cleanups = [];
66+
});
67+
68+
afterEach(() => {
69+
// Listeners re-enter this test's injector, so none may outlive the TestBed.
70+
cleanups.forEach(cleanup => cleanup());
71+
});
72+
73+
type ChildListener = (query: Query, callback: (snapshot: DataSnapshot) => void) => Unsubscribe;
74+
const childListeners: [string, ChildListener][] = [
75+
['onChildAdded', onChildAdded],
76+
['onChildChanged', onChildChanged],
77+
['onChildMoved', onChildMoved],
78+
['onChildRemoved', onChildRemoved],
79+
];
80+
childListeners.forEach(([name, listen]) => {
81+
it(`${name} does not hold the application unstable`, () => {
82+
const unsubscribe = TestBed.runInInjectionContext(() => listen(ref(database, rando()), () => undefined));
83+
cleanups.push(unsubscribe);
84+
expect(addTask).not.toHaveBeenCalled();
85+
});
86+
});
87+
88+
it('off does not hold the application unstable', () => {
89+
TestBed.runInInjectionContext(() => off(ref(database, rando()), 'value', () => undefined));
90+
expect(addTask).not.toHaveBeenCalled();
91+
});
92+
93+
it('enableLogging does not hold the application unstable', () => {
94+
TestBed.runInInjectionContext(() => enableLogging(() => undefined));
95+
cleanups.push(() => TestBed.runInInjectionContext(() => enableLogging(false)));
96+
expect(addTask).not.toHaveBeenCalled();
97+
});
98+
99+
[ListenEvent.added, ListenEvent.changed, ListenEvent.moved, ListenEvent.removed].forEach(event => {
100+
it(`fromRef on ${event} does not hold the application unstable`, () => {
101+
const subscription = TestBed.runInInjectionContext(() => fromRef(ref(database, rando()), event)).subscribe();
102+
cleanups.push(() => subscription.unsubscribe());
103+
expect(addTask).not.toHaveBeenCalled();
104+
});
105+
});
106+
107+
it('fromRef on the value event registers a pending task', () => {
108+
const subscription = TestBed.runInInjectionContext(() => fromRef(ref(database, rando()), ListenEvent.value)).subscribe();
109+
cleanups.push(() => subscription.unsubscribe());
110+
expect(addTask).toHaveBeenCalledTimes(1);
111+
});
112+
113+
it('stateChanges does not hold the application unstable', () => {
114+
const subscription = TestBed.runInInjectionContext(() => stateChanges(ref(database, rando()))).subscribe();
115+
cleanups.push(() => subscription.unsubscribe());
116+
expect(addTask).not.toHaveBeenCalled();
117+
});
118+
119+
it('auditTrail does not hold the application unstable', () => {
120+
const subscription = TestBed.runInInjectionContext(() => auditTrail(ref(database, rando()))).subscribe();
121+
cleanups.push(() => subscription.unsubscribe());
122+
expect(addTask).not.toHaveBeenCalled();
123+
});
124+
125+
it('onValue in the same context does register a task', () => {
126+
const unsubscribe = TestBed.runInInjectionContext(() => onValue(ref(database, rando()), () => undefined));
127+
cleanups.push(unsubscribe);
128+
expect(addTask).toHaveBeenCalledTimes(1);
129+
});
130+
131+
it('list in the same context does register a task', () => {
132+
const subscription = TestBed.runInInjectionContext(() => list(ref(database, rando()))).subscribe();
133+
cleanups.push(() => subscription.unsubscribe());
134+
expect(addTask).toHaveBeenCalledTimes(1);
135+
});
136+
137+
});
138+
37139
});
38140

39141
});

‎src/database/firebase.ts‎

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/database/overrides.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { LogLevel, ɵzoneWrap } from '@angular/fire';
2+
import { ListenEvent, fromRef as _fromRef } from 'rxfire/database';
3+
4+
const fromRefUntilFirst = ɵzoneWrap(_fromRef, true, LogLevel.WARN);
5+
const fromRefWithoutWaiting = ɵzoneWrap(_fromRef, false, LogLevel.WARN);
6+
7+
// Only the value event fires on attach whatever the node holds. A child event may never fire.
8+
export const fromRef: typeof _fromRef = (ref, event) =>
9+
event === ListenEvent.value ? fromRefUntilFirst(ref, event) : fromRefWithoutWaiting(ref, event);

‎src/database/rxfire.ts‎

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/remote-config/firebase.ts‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/remote-config/remote-config.spec.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { PendingTasks } from '@angular/core';
12
import { TestBed } from '@angular/core/testing';
23
import { FirebaseApp, getApp, initializeApp, provideFirebaseApp } from '@angular/fire/app';
3-
import { RemoteConfig, getRemoteConfig, provideRemoteConfig } from '@angular/fire/remote-config';
4+
import { RemoteConfig, getRemoteConfig, isSupported, provideRemoteConfig } from '@angular/fire/remote-config';
45
import { COMMON_CONFIG } from '../test-config';
56
import { rando } from '../utils';
67

@@ -33,6 +34,13 @@ describe('RemoteConfig', () => {
3334
expect(remoteConfig.app).toEqual(app);
3435
});
3536

37+
it('isSupported does not hold the application unstable', async () => {
38+
const addTask = spyOn(TestBed.inject(PendingTasks), 'add').and.callThrough();
39+
const supported = TestBed.runInInjectionContext(isSupported);
40+
expect(addTask).not.toHaveBeenCalled();
41+
expect(typeof await supported).toBe('boolean');
42+
});
43+
3644
});
3745

3846
});

0 commit comments

Comments
 (0)