Skip to content

Using Compat-Layer for IndexedDB persistence (v2) #3974

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
merged 18 commits into from
Oct 27, 2020

Conversation

schmidt-sebastian
Copy link
Contributor

@schmidt-sebastian schmidt-sebastian commented Oct 21, 2020

This is a new version of #3974. The main goal is to keep the code similar to the other SDKs.

Instead of using https://github.com/firebase/firebase-js-sdk/blob/master/packages/firestore/exp/src/api/components.ts to manage optional dependencies, it transistions firestore-exp to use FirestoreClient. As a a result, the code from components.ts is moved to FirestoreClient and firestore-exp calls into the tree-shakeable FirestoreClient APIs for the first batch of methods.

One change from #3974 remains: The credential registration happens in two layers. FirestoreClient registers a listener with FirebaseAuth. This allows us to swap the logic for credential changes when RemoteStore is instantiated (via the OnlineComponentProvider).

Let me know if you want to chat before reviewing.

@changeset-bot
Copy link

changeset-bot bot commented Oct 21, 2020

⚠️ No Changeset found

Latest commit: dfa7afb

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

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Oct 21, 2020

Binary Size Report

Affected SDKs

  • @firebase/firestore

    Type Base (ab0f643) Head (4a3a43d) Diff
    browser 238 kB 241 kB +3.18 kB (+1.3%)
    esm2017 189 kB 190 kB +1.06 kB (+0.6%)
    main 471 kB 475 kB +4.48 kB (+1.0%)
    module 238 kB 241 kB +3.18 kB (+1.3%)
    react-native 189 kB 190 kB +1.06 kB (+0.6%)
  • @firebase/firestore/exp

    Type Base (ab0f643) Head (4a3a43d) Diff
    browser 188 kB 189 kB +1.23 kB (+0.7%)
    main 475 kB 476 kB +1.10 kB (+0.2%)
    module 188 kB 189 kB +1.23 kB (+0.7%)
    react-native 188 kB 189 kB +1.23 kB (+0.7%)
  • @firebase/firestore/lite

    Type Base (ab0f643) Head (4a3a43d) Diff
    browser 61.6 kB 62.9 kB +1.26 kB (+2.0%)
    main 137 kB 139 kB +2.05 kB (+1.5%)
    module 61.6 kB 62.9 kB +1.26 kB (+2.0%)
    react-native 61.8 kB 63.1 kB +1.26 kB (+2.0%)
  • @firebase/firestore/memory

    Type Base (ab0f643) Head (4a3a43d) Diff
    browser 176 kB 177 kB +1.77 kB (+1.0%)
    esm2017 139 kB 139 kB -163 B (-0.1%)
    main 343 kB 344 kB +1.32 kB (+0.4%)
    module 176 kB 177 kB +1.77 kB (+1.0%)
    react-native 139 kB 139 kB -163 B (-0.1%)
  • @firebase/util

    Type Base (ab0f643) Head (4a3a43d) Diff
    browser 20.1 kB 20.2 kB +69 B (+0.3%)
    esm2017 18.9 kB 19.0 kB +69 B (+0.4%)
    main 21.3 kB 21.3 kB +69 B (+0.3%)
    module 20.1 kB 20.2 kB +69 B (+0.3%)
  • firebase

    Type Base (ab0f643) Head (4a3a43d) Diff
    firebase-app.js 20.0 kB 20.0 kB +17 B (+0.1%)
    firebase-database.js 190 kB 190 kB +17 B (+0.0%)
    firebase-firestore.js 277 kB 280 kB +3.10 kB (+1.1%)
    firebase-firestore.memory.js 216 kB 218 kB +1.73 kB (+0.8%)
    firebase-performance-standalone.es2017.js 71.7 kB 71.7 kB +18 B (+0.0%)
    firebase-performance-standalone.js 48.1 kB 48.1 kB +17 B (+0.0%)
    firebase.js 819 kB 822 kB +3.14 kB (+0.4%)

Test Logs

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Oct 21, 2020

Size Analysis Report

Affected Products

No changes between base commit (593e065) and head commit (81b7629).

Test Logs

@schmidt-sebastian schmidt-sebastian force-pushed the mrschmidt/enablepersistence-compat-v2 branch from 19836c8 to c2e7065 Compare October 22, 2020 22:28
@schmidt-sebastian schmidt-sebastian changed the base branch from master to mrschmidt/treeshakeablefirestoreclient October 22, 2020 23:49
@schmidt-sebastian schmidt-sebastian force-pushed the mrschmidt/enablepersistence-compat-v2 branch 4 times, most recently from 7239f49 to 75b105a Compare October 23, 2020 18:20
@schmidt-sebastian schmidt-sebastian force-pushed the mrschmidt/treeshakeablefirestoreclient branch from d655125 to 6b8f384 Compare October 23, 2020 18:23
@schmidt-sebastian schmidt-sebastian force-pushed the mrschmidt/enablepersistence-compat-v2 branch from 75b105a to feb5809 Compare October 23, 2020 18:25
@schmidt-sebastian schmidt-sebastian changed the title Redo of enablePersistenceCompat Using Compat-Layer for IndexedDB persistence (v2) Oct 23, 2020
this._receivedInitialUser.promise.then(() =>
this._credentialListener(this._user)
);
_ensureClientConfigured(): FirestoreClient {
Copy link
Contributor

Choose a reason for hiding this comment

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

These all seem like they could be free functions too. Mixing methods and free functions makes development confusing because you have to remember which operations are methods and which are free functions.

If this is painful to implement, beyond the scope of what you want to tackle, or otherwise messy feel free to punt.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I implemented this. It allows me to merge some of the code from Firestore-Exp and Firestore. I went back and forth on the naming for these functions. The last commit renames them to "ensureFirestoreConfigured" and "configureFirestore".

if (!this._firestoreClient) {
this._configureClient();
}
this._firestoreClient!.verifyNotTerminated();
Copy link
Contributor

Choose a reason for hiding this comment

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

Completely optional: you could avoid the forced unwrap here by having _configureClient return the new instance. Then here you'd have:

let client = this._firestoreClient;
if (!client) {
  client = this._configureClient();
}
client.verfyNotTerminated();
return client;

This works because initially, client will have the type FirestoreClient | undefined, but because the undefined case results in an unconditional assignment of a FirestoreClient value, tsc will change the inferred type to just FirestoreClient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted not to do this since this is the only callsite.

@@ -326,12 +266,12 @@ export function enableMultiTabIndexedDbPersistence(
* but the client remains usable.
*/
function setPersistenceProviders(
firestore: FirebaseFirestore,
firestore: FirestoreClient,
Copy link
Contributor

Choose a reason for hiding this comment

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

This instance is no longer a Firestore or FirestoreCompat: rename to client or similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

// to the components (as `verifyNotInitialized()` would fail). Components can
// then be accessed via `getOfflineComponentProvider()` and
// `getOnlineComponentProvider()`
const firestoreClient = firestore._ensureClientConfigured();
Copy link
Contributor

Choose a reason for hiding this comment

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

There aren't that many different kinds of clients in this code base, so you can probably abbreviate this to just client if you wanted to. Here and throughout if you choose to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mass-renamed firestoreClient to client.

settings.host ?? DEFAULT_HOST,
settings.ssl ?? DEFAULT_SSL,
/* forceLongPolling= */ false,
/* forceAutoDetectLongPolling= */ true
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not consult settings for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I originally chose not to pull in FirestoreSettings into firestore-exp since it included a lot of validation code. This is no longer the case so I was able to use FirestoreStettings here.

}
enableMultiTabIndexedDbPersistence = enableMultiTabIndexedDbPersistence;
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like it's a no-op. Did you mean to change a member of this? Here and the next line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this instance, the this member doesn't matter since these new functions should not rely on an associated this context. To make this more readable, I assigned null using bind.

Copy link
Contributor

Choose a reason for hiding this comment

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

The thing I'm missing is that this looks like an assignment of a value to itself, which should do nothing and could therefore be removed. What am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The left part points to an instance member, while the right part is the free-standing function of the same name. JavaScript can differentiate because access to instance members requires this.

@wilhuff wilhuff assigned schmidt-sebastian and unassigned wilhuff Oct 26, 2020
Copy link
Contributor

@wilhuff wilhuff left a comment

Choose a reason for hiding this comment

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

LGTM

@google-cla
Copy link

google-cla bot commented Oct 27, 2020

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@schmidt-sebastian schmidt-sebastian changed the base branch from mrschmidt/treeshakeablefirestoreclient to master October 27, 2020 21:25
@schmidt-sebastian schmidt-sebastian merged commit 8922f54 into master Oct 27, 2020
@firebase firebase locked and limited conversation to collaborators Nov 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants