forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 2
Query context provider #2
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
Gabswim
merged 8 commits into
Gabswim:feat/lit-query-example
from
mindroute:feat/lit-query-example
Sep 25, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
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 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| // @ts-check | ||
|
|
||
| import { configs as litConfigs } from 'eslint-plugin-lit' | ||
| import rootConfig from '../../eslint.config.js' | ||
|
|
||
| export default [...rootConfig] | ||
| export default [ | ||
| ...rootConfig, | ||
| { | ||
| files: ['*.ts'], | ||
| ...litConfigs['flat/recommended'], | ||
| }, | ||
| ] |
This file contains hidden or 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
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.