Skip to content
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

Add example of lit query adapter #7715

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

Gabswim
Copy link

@Gabswim Gabswim commented Jul 11, 2024

This is just a draft PR to follow up on the discussion I started a while ago here: #6390

@@ -0,0 +1,49 @@
import type { QueryClient } from '@tanstack/query-core'
Copy link
Author

Choose a reason for hiding this comment

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

I am not super happy with this solution but I have tried to make it work with lit - Context but I was not able to make it work because of the way it is implemented with the lifecycle.

Choose a reason for hiding this comment

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

I can help with the lit context and some controllers. Let me know where/how.

Add example
*/
hostUpdate() {
const defaultOption = this.getDefaultOptions()
this.queryObserver.setOptions(defaultOption)

Choose a reason for hiding this comment

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

This would make the options being set in each lifecycle, which is undesirable. It's preferable to only update the options when they have changed.

hostConnected() {
this.unsubscribe = this.queryObserver.subscribe((result) => {
this.result = result
this.host.requestUpdate()

Choose a reason for hiding this comment

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

It's preferable to only requestUpdate when the result has changed since a lot of unnecessary updates is triggered otherwise.

Copy link
Author

Choose a reason for hiding this comment

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

I was under the impression that this event listener was only going to get called when the result has changed.

Choose a reason for hiding this comment

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

I think you're right. I think all this is handled properly in the queryObserver now.

@JulianCataldo
Copy link

JulianCataldo commented Sep 14, 2024

Hello! Thanks a lot for attempting to bridge Lit and Tanstack Query.

I tried to make this works locally, just to see.

Small, ongoing PR for fixing build, adding a mutation controller, example(s)…:
Gabswim#1

EDIT And some whitespace tests issues (never mind, it was just prettier)

@klasjersevi
Copy link

Hello! Thanks a lot for attempting to bridge Lit and Tanstack Query.

I tried to make this works locally, just to see.

Small, ongoing PR for fixing build, adding a mutation controller, example(s)…: Gabswim#1

@JulianCataldo Nice to have you onboard! I've made some additions with a context provider also. Gabswim#2 Feel free to try it out.

Remove unused line

Co-authored-by: Gabriel Legault <[email protected]>
@JulianCataldo
Copy link

JulianCataldo commented Sep 24, 2024

Context provider is a huge addition. I'll try this as soon as I can :)
IDK if @Gabswim viewed my PR, I wish to go on helping while avoiding duplicating efforts ^^.
I can add more examples, WDYT? And try to do achieve tests for MutationController, also.
Tell me, and I'll try to help with the best I can achieve.

Ty!

@Gabswim
Copy link
Author

Gabswim commented Sep 25, 2024

Thank you for your contributions @JulianCataldo ! I saw your PR, but I’ve been more focused on the one for the context provider by @klasjersevi .

Before going any further, I think it would be good to get some feedback from @TkDodo, justinfagnani, just to make sure we're all aligned.

One challenge, which I believe has no workaround with Lit’s context, is its asynchronous flow. This issue doesn't seem to exist in other packages, as seen in these implementations:

Svelte: getContext
SolidJS: useContext

@klasjersevi
Copy link

klasjersevi commented Sep 25, 2024

One challenge, which I believe has no workaround with Lit’s context, is its asynchronous flow. This issue doesn't seem to exist in other packages, as seen in these implementations:

Svelte: getContext
SolidJS: useContext

Just to explain for anyone who is not familiar with Lit Context:

Lit context is an event-based protocol between web components based on the Context Community Protocol by the W3C's Web Components Community Group. Normally this happens synchronously as long as the context provider (parent node) is defined before the context consumer is defined. In some cases this can happen in a different order or if the provider is not yet available for any reason. So the context might be available directly or it might be available at a later point. To overcome some of the challenges with this lit also provides a ContextRoot that can take care of requests until the context provider is available.

Many other context api:s in other frameworks are synchronous, which instead throws errors instantly if a context isn't available directly.

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