Skip to content

Commit f309472

Browse files
committed
Tested deleteOnDeref, added more comments
1 parent 11bc150 commit f309472

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

packages/app/src/api.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,14 @@ export function initializeServerApp(
248248

249249
// Build an app name based on a hash of the configuration options.
250250
const nameObj = {
251-
_serverAppConfig,
251+
..._serverAppConfig,
252252
...appOptions
253253
};
254254

255255
// However, Do not mangle the name based on releaseOnDeref, since it will vary between the
256256
// construction of FirebaseServerApp instances. For example, if the object is the request headers.
257-
if (nameObj._serverAppConfig.releaseOnDeref !== undefined) {
258-
delete nameObj._serverAppConfig.releaseOnDeref;
257+
if (nameObj.releaseOnDeref !== undefined) {
258+
delete nameObj.releaseOnDeref;
259259
}
260260

261261
const hashCode = (s: string): number => {

packages/app/src/firebaseServerApp.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,25 @@ export class FirebaseServerAppImpl
6666
...serverConfig
6767
};
6868

69-
this._finalizationRegistry = new FinalizationRegistry(
70-
this.automaticCleanup
71-
);
69+
this._finalizationRegistry = new FinalizationRegistry(() => {
70+
this.automaticCleanup();
71+
});
7272

7373
this._refCount = 0;
7474
this.incRefCount(this._serverConfig.releaseOnDeref);
7575

7676
// Do not retain a hard reference to the dref object, otherwise the FinalizationRegisry
7777
// will never trigger.
7878
this._serverConfig.releaseOnDeref = undefined;
79+
serverConfig.releaseOnDeref = undefined;
7980
}
8081

8182
get refCount(): number {
8283
return this._refCount;
8384
}
8485

86+
// Increment the reference count of this server app. If an object is provided, register it
87+
// with the finalization registry.
8588
incRefCount(obj: object | undefined): void {
8689
if (this.isDeleted) {
8790
return;
@@ -92,15 +95,19 @@ export class FirebaseServerAppImpl
9295
}
9396
}
9497

98+
// Decrement the reference count.
9599
decRefCount(): number {
96100
if (this.isDeleted) {
97101
return 0;
98102
}
99103
return --this._refCount;
100104
}
101105

102-
private automaticCleanup(serverApp: FirebaseServerAppImpl): void {
103-
void deleteApp(serverApp);
106+
// Invoked by the FinalizationRegistry callback to note that this app should go through its
107+
// reference counts and delete itself if no reference count remain. The coordinating logic that
108+
// handles this is in deleteApp(...).
109+
private automaticCleanup(): void {
110+
void deleteApp(this);
104111
}
105112

106113
get settings(): FirebaseServerAppSettings {

0 commit comments

Comments
 (0)