Skip to content

[Feat] Firestore onSnapshot support for bundles for feature branch. #8896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

DellaBitta
Copy link
Contributor

@DellaBitta DellaBitta commented Apr 4, 2025

Discussion

Pull request to add onSnapshot listeners support for bundles to the Firestore SSR serialization feature branch.

This change adds a series of onSnapshot overloads that match the existing onSnapshot variants (observer, individual callback functions, etc) but accepts a bundle instead of a QuerySnapshot or a DocumentSnapshot.

The toJSON methods of QuerySnapshot and DocumentSnapshot have also been updated to explicitly name the fields in the object return type.

Fixed an issue in the bundle builder where the bundleName for DocumentSnapshots didn't match the ResourcePath of the document, which is needed when reconstituting the DocumentReference in onSnapshot.

Finally, I cleaned up the text wrapping for the onSnapshot reference doc comments so they take up fewer lines of source code.

Testing

Added integration tests to ensure that onSnapshot functions invoked with a bundle properly call next with the snapshot data initially and when the document is updated in the service, and error if the bundle is invalid. Made tests that exercise the method signatures that take an Observer as well.

API Changes

Numerous onSnapshot overloads that now accept a bundle a parameter. These will be part of a formal API proposal once the full feature branch implementation is closer to completion.

Copy link
Contributor

github-actions bot commented Apr 4, 2025

Vertex AI Mock Responses Check ⚠️

A newer major version of the mock responses for Vertex AI unit tests is available. update_vertexai_responses.sh should be updated to clone the latest version of the responses: v8.0

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Apr 4, 2025

Size Report 1

Affected Products

  • @firebase/firestore

    TypeBase (b85747b)Merge (9ad6c4a)Diff
    browser387 kB389 kB+1.95 kB (+0.5%)
    main599 kB602 kB+3.35 kB (+0.6%)
    module387 kB389 kB+1.95 kB (+0.5%)
    react-native388 kB390 kB+1.95 kB (+0.5%)
  • bundle

    TypeBase (b85747b)Merge (9ad6c4a)Diff
    firestore (Query Cursors)255 kB255 kB+100 B (+0.0%)
    firestore (Query)253 kB262 kB+9.37 kB (+3.7%)
    firestore (Read data once)244 kB244 kB+44 B (+0.0%)
    firestore (Read Write w Persistence)332 kB332 kB+44 B (+0.0%)
    firestore (Realtime updates)247 kB256 kB+9.37 kB (+3.8%)
    firestore (Transaction)221 kB221 kB+44 B (+0.0%)
  • firebase

    TypeBase (b85747b)Merge (9ad6c4a)Diff
    firebase-compat.js799 kB801 kB+1.60 kB (+0.2%)
    firebase-firestore-compat.js345 kB346 kB+1.60 kB (+0.5%)
    firebase-firestore.js446 kB448 kB+1.93 kB (+0.4%)

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/3xTflPfA2w.html

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Apr 4, 2025

Size Analysis Report 1

This report is too large (92,564 characters) to be displayed here in a GitHub comment. Please use the below link to see the full report on Google Cloud Storage.

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/TIMU02ifNe.html

Copy link

changeset-bot bot commented Apr 7, 2025

⚠️ No Changeset found

Latest commit: fc8d568

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@DellaBitta DellaBitta marked this pull request as ready for review April 11, 2025 17:44
@DellaBitta DellaBitta requested review from a team as code owners April 11, 2025 17:44
@DellaBitta DellaBitta changed the title [Feat] WIP - Firestore onSnapshot support for bundles [Feat] Firestore onSnapshot support for bundles for feature branch. Apr 11, 2025
Copy link
Contributor

@MarkDuckworth MarkDuckworth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I think the only thing that needs to be fixed is checking for unsubscribed before creating a snapshot listener for a DocumentReference

) => void,
error: args[curArg++] as (error: FirestoreError) => void,
complete: args[curArg++] as () => void
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it feels like you could reduce some duplicate code by creating these observer objects higher in the function. It may only reduce two calls to onSnapshot[Query|Document]SnapshotBundle

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's what I had originally but it ended up being more lines of code due to the type definiton and then configuring the object with optional parameters. I can revist it though, if you'd like.

Edit: revisited, and done!

json => snapshotJSON.
Common observer definition
default SnapshotOption object
unsubscribe check on onSnapshotDocumentSnapshotBundle
Copy link
Contributor

@MarkDuckworth MarkDuckworth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

*
* @internal
*/
function normalizeSnapshotJsonFields(snapshotJson: object): {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have used a type predicate for this. However, since you are also checking values, this implementation is also okay, IMO.

@DellaBitta DellaBitta merged commit e34353d into ddb-firestore-result-serialization Apr 15, 2025
72 of 105 checks passed
@DellaBitta DellaBitta deleted the ddb-onsnapshot-docbundle branch April 15, 2025 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants