-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Gabswim
wants to merge
10
commits into
TanStack:main
Choose a base branch
from
Gabswim:feat/lit-query-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+8,694
−5,519
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
eee8fd4
Add example
Gabswim d11039a
chore(lit-query): change to tsup build
klasjersevi a3efa3b
feat(lit-query): lit context definition
klasjersevi 977f0b7
chore(lit-query): eslint lit
klasjersevi 4be615e
feat(lit-query): query client context provider
klasjersevi f8b3a01
docs(lit-query): add missing mixin description
klasjersevi 683d2ec
feature(lit-query): enabled query client context
klasjersevi 3c27bad
Updated tests
klasjersevi 9cca338
Apply suggestions from code review
klasjersevi 68e3c7e
Merge pull request #2 from mindroute/feat/lit-query-example
Gabswim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"ignoreRules": ["cjs-resolves-to-esm", "internal-resolution-error"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// @ts-check | ||
|
||
import { configs as litConfigs } from 'eslint-plugin-lit' | ||
import rootConfig from '../../eslint.config.js' | ||
|
||
export default [ | ||
...rootConfig, | ||
{ | ||
files: ['*.ts'], | ||
...litConfigs['flat/recommended'], | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{ | ||
"name": "@tanstack/lit-query", | ||
"version": "5.50.3", | ||
"description": "Primitives for managing, caching and syncing asynchronous and remote data in lit", | ||
"author": "Gabriel Legault", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/TanStack/query.git", | ||
"directory": "packages/lit-query" | ||
}, | ||
"homepage": "https://tanstack.com/query", | ||
"funding": { | ||
"type": "github", | ||
"url": "https://github.com/sponsors/tannerlinsley" | ||
}, | ||
"scripts": { | ||
"clean": "rimraf ./dist && rimraf ./coverage", | ||
"test:eslint": "eslint ./src", | ||
"test:lib": "vitest", | ||
"test:lib:dev": "pnpm run test:lib --watch", | ||
"test:build": "publint --strict && attw --pack", | ||
"build": "tsup" | ||
}, | ||
"type": "module", | ||
"types": "build/legacy/index.d.ts", | ||
"main": "build/legacy/index.cjs", | ||
"module": "build/legacy/index.js", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./build/modern/index.d.ts", | ||
"default": "./build/modern/index.js" | ||
}, | ||
"require": { | ||
"types": "./build/modern/index.d.cts", | ||
"default": "./build/modern/index.cjs" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"sideEffects": false, | ||
"files": [ | ||
"build", | ||
"src" | ||
], | ||
"dependencies": { | ||
"@tanstack/query-core": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@lit/context": "^1.1.2", | ||
"@open-wc/testing-helpers": "3.0.1", | ||
"@types/jest-when": "3.5.5", | ||
"eslint-plugin-lit": "^1.14.0", | ||
"jest-when": "3.6.0", | ||
"lit": "3.1.4" | ||
}, | ||
"peerDependencies": { | ||
"lit": "^2.7.0 || ^3.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { LitElement, html } from 'lit' | ||
import { customElement, state } from 'lit/decorators.js' | ||
import { provide } from '@lit/context' | ||
import { QueryClient } from '@tanstack/query-core' | ||
import { QueryContext } from './context.js' | ||
|
||
/** | ||
* Definition for the properties provided by the query client mixin class. | ||
*/ | ||
export interface QueryContextProps { | ||
/** | ||
* Tanstack Query Client | ||
*/ | ||
queryClient: QueryClient | ||
} | ||
|
||
/** | ||
* Generic constructor definition | ||
*/ | ||
export type Constructor<T = object> = new (...args: Array<any>) => T | ||
|
||
/** | ||
* Query Client Context as mixin class. | ||
* Extend this mixin class to make any LitElement class a context provider. | ||
* | ||
* @param Base - The base class to extend. Must be or inherit LitElement. | ||
* @returns Class extended with query client context provider property. | ||
*/ | ||
export const QueryClientMixin = <T extends Constructor<LitElement>>( | ||
Base: T, | ||
) => { | ||
class QueryClientContextProvider extends Base implements QueryContextProps { | ||
/** | ||
* The query client provided as a context. | ||
* May be overridden to set a custom configuration. | ||
*/ | ||
@provide({ context: QueryContext }) | ||
@state() | ||
queryClient = new QueryClient() | ||
|
||
connectedCallback(): void { | ||
super.connectedCallback() | ||
this.queryClient.mount() | ||
} | ||
|
||
disconnectedCallback(): void { | ||
super.disconnectedCallback() | ||
this.queryClient.unmount() | ||
} | ||
} | ||
|
||
// Cast return type to the mixin's interface intersected with the Base type | ||
return QueryClientContextProvider as Constructor<QueryContextProps> & T | ||
} | ||
|
||
/** | ||
* Query client context provided as a Custom Component. | ||
* Place any components that should use the query client context as children. | ||
*/ | ||
@customElement('query-client-provider') | ||
export class QueryClientProvider extends QueryClientMixin(LitElement) { | ||
render() { | ||
return html`<slot></slot>` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
import { ContextConsumer } from '@lit/context' | ||
import { QueryObserver } from '@tanstack/query-core' | ||
import { QueryContext } from './context' | ||
import type { | ||
QueryClient, | ||
QueryKey, | ||
QueryObserverOptions, | ||
QueryObserverResult, | ||
} from '@tanstack/query-core' | ||
import type { | ||
LitElement, | ||
ReactiveController, | ||
ReactiveControllerHost, | ||
} from 'lit' | ||
|
||
/** | ||
* Temporary Promise.withResolvers type polyfill until Typescript workspace dependency is updated from 5.3.3 to >=5.4 | ||
*/ | ||
type PromiseWithResolvers = Promise<unknown> & { | ||
withResolvers: <T>() => { | ||
resolve: (value: T | PromiseLike<T>) => void | ||
reject: (reason: any) => void | ||
promise: Promise<T> | ||
} | ||
} | ||
|
||
export type { QueryObserverOptions } | ||
|
||
/** | ||
* QueryController is a class that integrates a query-based data fetching system | ||
* into a Lit component as a ReactiveController. | ||
* | ||
* @template TQueryFnData - The data type returned by the query function. | ||
* @template TError - The error type for query errors. | ||
* @template TData - The data type to be used in the component. | ||
* @template TQueryData - The data type returned by the query (may differ from TData). | ||
* @template TQueryKey - The query key type. | ||
*/ | ||
export class QueryController< | ||
TQueryFnData = unknown, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
> implements ReactiveController | ||
{ | ||
/** | ||
* The result of the query observer, containing data and error information. | ||
*/ | ||
result?: QueryObserverResult<TData, TError> | ||
|
||
/** | ||
* Consumer of the lit query client context. | ||
*/ | ||
protected context: ContextConsumer<{ __context__: QueryClient }, LitElement> | ||
|
||
/** | ||
* Promise that is resolved when the query client is set. | ||
*/ | ||
whenQueryClient = ( | ||
Promise as unknown as PromiseWithResolvers | ||
).withResolvers<QueryClient>() | ||
|
||
/** | ||
* The query client. | ||
* This can be set manually or using a lit query client context provider. | ||
*/ | ||
set queryClient(queryClient: QueryClient | undefined) { | ||
this._queryClient = queryClient | ||
if (queryClient) { | ||
this.whenQueryClient.resolve(queryClient) | ||
} | ||
this.host.requestUpdate() | ||
} | ||
|
||
get queryClient() { | ||
return this._queryClient | ||
} | ||
|
||
/** | ||
* The internal query observer responsible for managing the query. | ||
*/ | ||
protected queryObserver?: QueryObserver< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryData, | ||
TQueryKey | ||
> | ||
|
||
/** | ||
* Promise that resolves when the query observer is created. | ||
*/ | ||
whenQueryObserver = ( | ||
Promise as unknown as PromiseWithResolvers | ||
).withResolvers< | ||
QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey> | ||
>() | ||
|
||
/** | ||
* Creates a new QueryController instance. | ||
* | ||
* @param host - The host component to which this controller is added. | ||
* @param optionsFn - A function that provides QueryObserverOptions for the query. | ||
* @param _queryClient - Optionally set the query client. | ||
* @link [QueryObserverOptions API Docs](). //TODO: Add the correct doc | ||
*/ | ||
constructor( | ||
protected host: ReactiveControllerHost, | ||
protected optionsFn?: () => QueryObserverOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryData, | ||
TQueryKey | ||
>, | ||
private _queryClient?: QueryClient, | ||
) { | ||
this.host.addController(this) | ||
|
||
// Initialize the context | ||
this.context = new ContextConsumer(this.host as LitElement, { | ||
context: QueryContext, | ||
subscribe: true, | ||
callback: (value) => { | ||
if (value) { | ||
this.queryClient = value | ||
} | ||
}, | ||
}) | ||
|
||
// Observe the query if a query function is provided | ||
if (this.optionsFn) { | ||
this.observeQuery(this.optionsFn) | ||
} | ||
} | ||
|
||
/** | ||
* Creates a query observer. The query is subscribed whenever the host is connected to the dom. | ||
* | ||
* @param options - Options for the query observer | ||
* @param optimistic - Get an initial optimistic result. Defaults to true. | ||
*/ | ||
async observeQuery( | ||
options: | ||
| QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey> | ||
| (() => QueryObserverOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryData, | ||
TQueryKey | ||
>), | ||
optimistic: boolean = true, | ||
) { | ||
const queryClient = await this.whenQueryClient.promise | ||
|
||
// Initialize the QueryObserver with defaulted options. | ||
const defaultedOptions = await this.getDefaultedOptions( | ||
typeof options === 'function' ? options() : options, | ||
) | ||
this.queryObserver = new QueryObserver(queryClient, defaultedOptions) | ||
|
||
// Get an optimistic result based on the defaulted options. | ||
if (optimistic) { | ||
this.result = this.queryObserver.getOptimisticResult(defaultedOptions) | ||
} else { | ||
this.result = undefined | ||
} | ||
|
||
this.host.requestUpdate() | ||
|
||
this.whenQueryObserver.resolve(this.queryObserver) | ||
} | ||
|
||
/** | ||
* Unsubscribe function to remove the observer when the component disconnects. | ||
*/ | ||
protected unsubscribe?: () => void | ||
|
||
/** | ||
* Invoked when the host component updates. | ||
* Updates the query observer options with default options if a query function is set. | ||
*/ | ||
async hostUpdate() { | ||
if (this.optionsFn) { | ||
const queryObserver = await this.whenQueryObserver.promise | ||
|
||
// Update options from the options function | ||
const defaultedOptions = await this.getDefaultedOptions(this.optionsFn()) | ||
queryObserver.setOptions(defaultedOptions) | ||
} | ||
} | ||
|
||
/** | ||
* Invoked when the host component is connected. | ||
*/ | ||
hostConnected() { | ||
this.subscribe() | ||
} | ||
|
||
/** | ||
* Subscribes to the query observer and updates the result. | ||
*/ | ||
async subscribe() { | ||
const queryObserver = await this.whenQueryObserver.promise | ||
|
||
// Unsubscribe any previous subscription before subscribing | ||
this.unsubscribe?.() | ||
|
||
this.unsubscribe = queryObserver.subscribe((result: typeof this.result) => { | ||
this.result = result | ||
this.host.requestUpdate() | ||
}) | ||
|
||
queryObserver.updateResult() | ||
this.host.requestUpdate() | ||
} | ||
|
||
/** | ||
* Invoked when the host component is disconnected. | ||
* Unsubscribes from the query observer to clean up. | ||
*/ | ||
hostDisconnected() { | ||
this.unsubscribe?.() | ||
this.unsubscribe = undefined | ||
} | ||
|
||
/** | ||
* Retrieves the default query options by combining the user-provided options | ||
* with the default options from the query client. | ||
* | ||
* @returns The default query options. | ||
*/ | ||
protected async getDefaultedOptions( | ||
options: QueryObserverOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryData, | ||
TQueryKey | ||
>, | ||
) { | ||
const queryClient = await this.whenQueryClient.promise | ||
|
||
return queryClient.defaultQueryOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryData, | ||
TQueryKey | ||
>(options) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.