From 4e415b51b14be86924c9bcc5d7e6a4a0021a2cc3 Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Mon, 25 May 2026 10:32:05 +0200 Subject: [PATCH 1/2] fix(firestore): generate unique listener IDs Listener IDs were derived from `Date.now()`, so listeners created in the same millisecond shared an ID and overwrote each other in the unsubscribes map, leaving the earlier ones unable to be removed. Replace the timestamp with a per-instance incrementing counter. The same issue was present in the storage plugin (downloadFile/uploadFile callback IDs) and is fixed here as well. Closes #986 --- packages/firestore/src/web.ts | 11 ++++++++--- packages/storage/src/web.ts | 10 ++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/firestore/src/web.ts b/packages/firestore/src/web.ts index 3319d6857..a5279c495 100644 --- a/packages/firestore/src/web.ts +++ b/packages/firestore/src/web.ts @@ -96,6 +96,7 @@ export class FirebaseFirestoreWeb implements FirebaseFirestorePlugin { private readonly unsubscribesMap: Map = new Map(); + private lastListenerId = 0; public async addCollectionGroupSnapshotListener< T extends DocumentData = DocumentData, @@ -129,7 +130,7 @@ export class FirebaseFirestoreWeb }, error => callback(null, error), ); - const id = Date.now().toString(); + const id = this.generateListenerId(); this.unsubscribesMap.set(id, unsubscribe); return id; } @@ -166,7 +167,7 @@ export class FirebaseFirestoreWeb }, error => callback(null, error), ); - const id = Date.now().toString(); + const id = this.generateListenerId(); this.unsubscribesMap.set(id, unsubscribe); return id; } @@ -220,7 +221,7 @@ export class FirebaseFirestoreWeb }, error => callback(null, error), ); - const id = Date.now().toString(); + const id = this.generateListenerId(); this.unsubscribesMap.set(id, unsubscribe); return id; } @@ -670,4 +671,8 @@ export class FirebaseFirestoreWeb return marker; } } + + private generateListenerId(): string { + return (++this.lastListenerId).toString(); + } } diff --git a/packages/storage/src/web.ts b/packages/storage/src/web.ts index 2237dfc2b..d69a2e98c 100644 --- a/packages/storage/src/web.ts +++ b/packages/storage/src/web.ts @@ -43,6 +43,8 @@ export class FirebaseStorageWeb { public static readonly ERROR_BLOB_MISSING = 'blob must be provided.'; + private lastCallbackId = 0; + public async downloadFile( options: DownloadFileOptions, callback: DownloadFileCallback, @@ -63,7 +65,7 @@ export class FirebaseStorageWeb .catch(error => { callback(null, error); }); - return Date.now().toString(); + return this.generateCallbackId(); } public async deleteFile(options: DeleteFileOptions): Promise { @@ -179,7 +181,7 @@ export class FirebaseStorageWeb callback(result, undefined); }, }); - return Date.now().toString(); + return this.generateCallbackId(); } public async useEmulator(options: UseEmulatorOptions): Promise { @@ -199,4 +201,8 @@ export class FirebaseStorageWeb }; return result; } + + private generateCallbackId(): string { + return (++this.lastCallbackId).toString(); + } } From 385702178f93d542c7ded39dad993339009c373f Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Mon, 25 May 2026 10:35:11 +0200 Subject: [PATCH 2/2] chore: add changeset --- .changeset/firestore-storage-unique-listener-ids.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/firestore-storage-unique-listener-ids.md diff --git a/.changeset/firestore-storage-unique-listener-ids.md b/.changeset/firestore-storage-unique-listener-ids.md new file mode 100644 index 000000000..01d6e5387 --- /dev/null +++ b/.changeset/firestore-storage-unique-listener-ids.md @@ -0,0 +1,6 @@ +--- +'@capacitor-firebase/firestore': patch +'@capacitor-firebase/storage': patch +--- + +fix: generate unique listener/callback IDs on web so listeners created in the same millisecond do not overwrite each other