Skip to content

Optional Datastore/FirestoreClient #3382

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 12 commits into from
Jul 24, 2020
Merged

Conversation

schmidt-sebastian
Copy link
Contributor

@schmidt-sebastian schmidt-sebastian commented Jul 9, 2020

This implements a proposal for optional dependencies discussed in the firebase-js-sdk chatroom. We currently have:

class LiteFirestore {
  datastore: Datastore;
}

class Firestore extends LiteFirestore {
  firestoreClient: FirestoreClient;
}

This means that any user of Firestore will pull in Datastore and FirestoreClient, even if they were to just use Timestamp or (more realistically) the new methods that only read from cache.

To solve this problem, I added a components.ts file for both the Lite SDK and the firestore-exp SDK that manages the relationship between Firestore and its components. Each Firestore instance has up to one component, and the Firestore class is responsible for its cleanup (via the removeMethods).

To make this worthwhile I also move the ComponentProvider initialization into components.ts, which has some impact on enableIndexedDbPersistence and clearIndexdDbPersistence.

The first commit contains the code changes, the second commit contains the updated dependency file (auto-generated).

@changeset-bot
Copy link

changeset-bot bot commented Jul 9, 2020

💥 No Changeset

Latest commit: 5f22a4e

Merging this PR will not cause any packages to be released. If these changes should not cause updates to packages in this repo, this is fine 🙂

If these changes should be published to npm, 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 Jul 10, 2020

Binary Size Report

Affected SDKs

  • @firebase/firestore

    Type Base (8a631e2) Head (5376744) Diff
    browser 245 kB 246 kB +144 B (+0.1%)
    esm2017 193 kB 193 kB +22 B (+0.0%)
    main 469 kB 469 kB +181 B (+0.0%)
    module 243 kB 243 kB +122 B (+0.1%)
    react-native 193 kB 193 kB +22 B (+0.0%)
  • @firebase/firestore/exp

    Type Base (8a631e2) Head (5376744) Diff
    browser 187 kB 188 kB +281 B (+0.1%)
    main 511 kB 512 kB +1.09 kB (+0.2%)
    react-native 187 kB 188 kB +282 B (+0.2%)
  • @firebase/firestore/lite

    Type Base (8a631e2) Head (5376744) Diff
    browser 68.0 kB 68.4 kB +395 B (+0.6%)
    main 497 kB 498 kB +991 B (+0.2%)
    react-native 68.1 kB 68.4 kB +390 B (+0.6%)
  • @firebase/firestore/memory

    Type Base (8a631e2) Head (5376744) Diff
    browser 184 kB 184 kB +144 B (+0.1%)
    esm2017 145 kB 145 kB +22 B (+0.0%)
    main 344 kB 344 kB +181 B (+0.1%)
    module 182 kB 182 kB +122 B (+0.1%)
    react-native 145 kB 145 kB +22 B (+0.0%)
  • firebase

    Type Base (8a631e2) Head (5376744) Diff
    firebase-firestore.js 283 kB 284 kB +119 B (+0.0%)
    firebase-firestore.memory.js 224 kB 224 kB +119 B (+0.1%)
    firebase.js 817 kB 817 kB +121 B (+0.0%)

Test Logs

@schmidt-sebastian schmidt-sebastian changed the title WIP Tree-Shakeable Datastore Optional Datastore/FirestoreClient Jul 15, 2020
schmidt-sebastian added a commit that referenced this pull request Jul 20, 2020
Pretty sure this is causes the CI failure in #3382
@schmidt-sebastian
Copy link
Contributor Author

@rafikhan Do you have time to review this?

@schmidt-sebastian
Copy link
Contributor Author

I updated the PR to use a more generic mechanism for component removal - see 2bac522. Note that for now only one component exists so the difference is minimal.

Copy link
Contributor

@rafikhan rafikhan left a comment

Choose a reason for hiding this comment

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

Minor comments around logging and factoring out functions. Otherwise LGTM.

Comment on lines +45 to +59
const settings = firestore._getSettings();
const databaseInfo = new DatabaseInfo(
firestore._databaseId,
firestore._persistenceKey,
settings.host ?? DEFAULT_HOST,
settings.ssl ?? DEFAULT_SSL,
/* forceLongPolling= */ false
);
const datastorePromise = newConnection(databaseInfo).then(connection => {
const serializer = newSerializer(databaseInfo.databaseId);
const datastore = newDatastore(firestore._credentials, serializer);
datastore.start(connection);
return datastore;
});
datastoreInstances.set(firestore, datastorePromise);
Copy link
Contributor

Choose a reason for hiding this comment

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

Add appropriate logging during instantiation and removal.

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

Comment on lines +146 to +152
if (firestoreImpl._initialized && !firestoreImpl._terminated) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'Persistence can only be cleared before a Firestore instance is ' +
'initialized or after it is terminated.'
);
}
Copy link
Contributor

@rafikhan rafikhan Jul 23, 2020

Choose a reason for hiding this comment

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

Suggestion: Consider putting this into an assert method like assertIsNotInitialized().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Kept as is for now since this is the only callsite and the only method that I am aware of that has this contract (plus I would have to come up with a good name - assertIsNotInitialized is only 90% correct...)

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay.

Copy link
Contributor

@rafikhan rafikhan left a comment

Choose a reason for hiding this comment

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

LGTM

@schmidt-sebastian schmidt-sebastian merged commit 1a630ba into master Jul 24, 2020
@firebase firebase locked and limited conversation to collaborators Aug 24, 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