Skip to content

Commit 916435a

Browse files
fix(core): wrap observer-object handlers like functions, and let a wrapped promise settle after teardown (#3829)
* fix(core): wrap a handler passed as an observer object like a function Firebase accepts a subscription handler either as a function or as an object with next, error and complete members (data-connect spells them onNext, onErr, onComplete). The zone wrapper only recognized the function, so an observer object reached the SDK untouched: no pending task held server rendering, inject() threw inside the handler, and in a zone.js app the handler ran outside the Angular zone, where the wrapper itself had moved the call. The object is now replaced by a copy whose function members are wrapped the same way, with this bound to the caller's object. onConfigUpdate takes only the object form and its handler fires only on a remote template change, so it joins the blockUntilFirst: false list along with onTokenChanged, whose first callback a first visit may never request. Both would otherwise hold the application unstable forever once their observers are wrapped. * fix(core): let a wrapped promise settle after its injector is destroyed When a wrapped call returns a Promise, the wrapper resolved its own promise through runInInjectionContext and NgZone.run. Neither reaches the caller: a .then callback runs as a later microtask after the injection context is torn down, and zone.js runs it in the zone where .then was attached, not where resolve was called. The only effect the two layers had was on teardown. runInInjectionContext throws once its injector is destroyed, so a Firebase promise settling after ApplicationRef.destroy() or a TestBed reset threw before resolve ran and the caller's promise never settled. The pending task still opens on the call and closes on settlement. * fix(core): wrap observer objects only for functions that take one The previous commit treated any object argument with a next, error or complete method as an observer. A model class with a complete() method, saved through setDoc with a converter, was replaced by a plain copy that lost its prototype, and its pending task was never released. tools/build.ts now reads Firebase's type declarations with the TypeScript compiler API and passes a fourth ɵzoneWrap argument, true, only to the 11 exports with an observer-shaped parameter. Every other function receives its object arguments untouched. A handler Firebase calls after the app is destroyed now runs directly, because entering a destroyed injector throws NG0205. Handlers passed as functions already hit this on main.
1 parent c593ecd commit 916435a

10 files changed

Lines changed: 498 additions & 27 deletions

File tree

‎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/auth/firebase.ts‎

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

‎src/data-connect/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/firestore/firebase.ts‎

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

‎src/firestore/firestore.spec.ts‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
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 { Firestore, connectFirestoreEmulator, getFirestore, provideFirestore } from '@angular/fire/firestore';
4+
import {
5+
Firestore,
6+
FirestoreDataConverter,
7+
connectFirestoreEmulator,
8+
doc,
9+
getDoc,
10+
getFirestore,
11+
provideFirestore,
12+
setDoc,
13+
} from '@angular/fire/firestore';
414
import { COMMON_CONFIG, firestoreEmulatorPort } from '../test-config';
515
import { rando } from '../utils';
616

17+
/* A model class with a method that shares a name with an observer member, saved through a converter. */
18+
class Task {
19+
constructor(public title: string) {}
20+
complete() {
21+
return this.title;
22+
}
23+
}
24+
25+
const taskConverter: FirestoreDataConverter<Task> = {
26+
toFirestore: task => ({ title: Reflect.get(task, 'title'), wasTaskInstance: task instanceof Task }),
27+
fromFirestore: snapshot => new Task(String(snapshot.get('title'))),
28+
};
29+
730
describe('Firestore', () => {
831
let app: FirebaseApp;
932
let firestore: Firestore;
@@ -34,6 +57,28 @@ describe('Firestore', () => {
3457
expect(firestore.app).toEqual(app);
3558
});
3659

60+
it('setDoc hands a converter the model instance itself and releases its pending task', async () => {
61+
const pendingTasks = TestBed.inject(PendingTasks);
62+
const originalAdd = pendingTasks.add.bind(pendingTasks);
63+
let added = 0;
64+
let released = 0;
65+
spyOn(pendingTasks, 'add').and.callFake(() => {
66+
added++;
67+
const remove = originalAdd();
68+
return () => {
69+
released++;
70+
remove();
71+
};
72+
});
73+
const ref = TestBed.runInInjectionContext(() => doc(firestore, `tasks/${rando()}`)).withConverter(taskConverter);
74+
await TestBed.runInInjectionContext(() => setDoc(ref, new Task('write the spec')));
75+
await new Promise(resolve => setTimeout(resolve, 10));
76+
expect(added).toBe(1);
77+
expect(released).toBe(1);
78+
const stored = await TestBed.runInInjectionContext(() => getDoc(doc(firestore, ref.path)));
79+
expect(stored.get('wasTaskInstance')).toBeTrue();
80+
});
81+
3782
});
3883

3984
});

‎src/messaging/firebase.ts‎

Lines changed: 3 additions & 3 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.

0 commit comments

Comments
 (0)