Skip to content

Simplify Listen API #3491

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 3 commits into from
Jul 28, 2020
Merged

Conversation

schmidt-sebastian
Copy link
Contributor

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

This is a cleanup PR before making FirestoreClient tree-shakeable.

  • It combines listen/unlisten into the same interface in FirestoreClient and inlines some APIs that no longer do anything with that change
  • It moves the PartialObserver -> AsyncObserver conversion into one central place

Note that the "unsubscribe" callbacks now enqueue a callback on the async queue even after termination. This is a no-op and so we don't need to check for termination: https://github.com/firebase/firebase-js-sdk/blob/master/packages/firestore/src/util/async_queue.ts#L312

@changeset-bot
Copy link

changeset-bot bot commented Jul 28, 2020

💥 No Changeset

Latest commit: 5a0a67d

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

@schmidt-sebastian schmidt-sebastian force-pushed the mrschmidt/asyncobservercleanup branch 2 times, most recently from ec8c6d8 to ec5eddb Compare July 28, 2020 04:46
@google-oss-bot
Copy link
Contributor

google-oss-bot commented Jul 28, 2020

Binary Size Report

Affected SDKs

  • @firebase/firestore

    Type Base (1971dde) Head (e30dbed) Diff
    browser 246 kB 246 kB -360 B (-0.1%)
    esm2017 193 kB 193 kB -416 B (-0.2%)
    main 471 kB 470 kB -1.13 kB (-0.2%)
    module 244 kB 243 kB -404 B (-0.2%)
    react-native 193 kB 193 kB -416 B (-0.2%)
  • @firebase/firestore/exp

    Type Base (1971dde) Head (e30dbed) Diff
    browser 187 kB 187 kB -411 B (-0.2%)
    main 467 kB 466 kB -1.12 kB (-0.2%)
    module 187 kB 187 kB -411 B (-0.2%)
    react-native 187 kB 187 kB -411 B (-0.2%)
  • @firebase/firestore/memory

    Type Base (1971dde) Head (e30dbed) Diff
    browser 185 kB 184 kB -360 B (-0.2%)
    esm2017 145 kB 144 kB -416 B (-0.3%)
    main 346 kB 345 kB -1.13 kB (-0.3%)
    module 183 kB 182 kB -404 B (-0.2%)
    react-native 145 kB 145 kB -416 B (-0.3%)
  • firebase

    Type Base (1971dde) Head (e30dbed) Diff
    firebase-firestore.js 285 kB 284 kB -415 B (-0.1%)
    firebase-firestore.memory.js 225 kB 225 kB -415 B (-0.2%)
    firebase.js 818 kB 818 kB -411 B (-0.1%)

Test Logs

@schmidt-sebastian schmidt-sebastian force-pushed the mrschmidt/asyncobservercleanup branch from ec5eddb to 829de71 Compare July 28, 2020 05:00
});
return () => {
observer.mute();
this.asyncQueue.enqueueAndForget(() => this.eventMgr.unlisten(listener));
Copy link
Contributor

Choose a reason for hiding this comment

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

@schmidt-sebastian Can you just confirm that it is indeed unnecessary to check for if (this.clientTerminated) before calling this.asyncQueue.enqueueAndForget()? I'm not familiar enough with AsyncQueue to know, but the old code avoids calling this.asyncQueue.enqueueAndForget() in the case that this.clientTerminated is false. The new code will call it regardless of the value of this.clientTerminated.

This comment also applies to addSnapshotsInSyncListener() below.

this.eventMgr.removeSnapshotsInSyncListener(observer);
return Promise.resolve();
});
this.asyncQueue.enqueueAndForget(async () =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is it necessary to specify an async function here to this.asyncQueue.enqueueAndForget()? In the listen() method it just specifies a normal, non-async function. I see that this is consistent with the old code but the reason for the inconsistency within this file is non-obvious to me. Should all calls to enqueueAndForget() be async functions? It would appear so based on the signature of enqueueAndForget().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a test already:

it('can unlisten queries after termination', async () => {

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh perfect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry - I replied to the wrong comment (but looks like you managed to restore the context). async makes the function return a Promise, even if the return of the value being returned is not a Promise. In the case above, "eventManager.listen" already returns a Promise.

This change is not needed, it just felt right to me. Manually returning Promise.resolve works as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes sense. I forgot to check the return type of eventManager.listen(). With this explanation, I'd recommend to keep it as is, with the more modern async keyword.

@schmidt-sebastian schmidt-sebastian force-pushed the mrschmidt/asyncobservercleanup branch from 4a93ee3 to 3e4c610 Compare July 28, 2020 18:58
@schmidt-sebastian schmidt-sebastian merged commit 217dca9 into master Jul 28, 2020
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/asyncobservercleanup branch July 28, 2020 22:13
@firebase firebase locked and limited conversation to collaborators Aug 28, 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