Skip to content

Commit

Permalink
WIP: Svelte 5 adapter (#6981)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhihengGet authored and lachlancollins committed Jul 31, 2024
1 parent 01e63a5 commit 1fe6b77
Show file tree
Hide file tree
Showing 129 changed files with 3,339 additions and 1,106 deletions.
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default [
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/no-empty-function': 'off',
'no-case-declarations': 'off',
'prefer-const': 'off',
},
},
]
4 changes: 2 additions & 2 deletions examples/svelte/auto-refetching/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"devDependencies": {
"@sveltejs/adapter-auto": "^3.2.2",
"@sveltejs/kit": "^2.5.18",
"@sveltejs/vite-plugin-svelte": "^3.1.1",
"svelte": "^4.2.18",
"@sveltejs/vite-plugin-svelte": "^4.0.0-next.4",
"svelte": "5.0.0-next.192",
"svelte-check": "^3.8.4",
"typescript": "5.3.3",
"vite": "^5.3.5"
Expand Down
4 changes: 3 additions & 1 deletion examples/svelte/auto-refetching/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { QueryClientProvider, QueryClient } from '@tanstack/svelte-query'
import { SvelteQueryDevtools } from '@tanstack/svelte-query-devtools'
const { children } = $props()
const queryClient = new QueryClient({
defaultOptions: {
queries: {
Expand All @@ -15,7 +17,7 @@

<QueryClientProvider client={queryClient}>
<main>
<slot />
{@render children()}
</main>
<SvelteQueryDevtools />
</QueryClientProvider>
26 changes: 12 additions & 14 deletions examples/svelte/auto-refetching/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
const endpoint = 'http://localhost:5173/api/data'
$: todos = createQuery<{ items: string[] }>({
const todos = createQuery<{ items: string[] }>({
queryKey: ['refetch'],
queryFn: async () => await fetch(endpoint).then((r) => r.json()),
// Refetch the data every second
Expand Down Expand Up @@ -49,46 +49,44 @@
margin-left:.5rem;
width:.75rem;
height:.75rem;
background: {$todos.isFetching ? 'green' : 'transparent'};
transition:: {!$todos.isFetching ? 'all .3s ease' : 'none'};
background: {todos.isFetching ? 'green' : 'transparent'};
transition:: {!todos.isFetching ? 'all .3s ease' : 'none'};
border-radius: 100%;
transform: 'scale(2)"
></span>
</div>
</label>
<h2>Todo List</h2>
<form
on:submit={(e) => {
onsubmit={(e) => {
e.preventDefault()
e.stopPropagation()
$addMutation.mutate(value, {
addMutation.mutate(value, {
onSuccess: () => (value = ''),
})
}}
>
<input placeholder="enter something" bind:value />
</form>

{#if $todos.isPending}
{#if todos.isPending}
Loading...
{/if}
{#if $todos.error}
{#if todos.error}
An error has occurred:
{$todos.error.message}
{todos.error.message}
{/if}
{#if $todos.isSuccess}
{#if todos.isSuccess}
<ul>
{#each $todos.data.items as item}
{#each todos.data.items as item}
<li>{item}</li>
{/each}
</ul>
<div>
<button on:click={() => $clearMutation.mutate(undefined)}>
Clear All
</button>
<button onclick={() => clearMutation.mutate(undefined)}> Clear All </button>
</div>
{/if}
{#if $todos.isFetching}
{#if todos.isFetching}
<div style="color:darkgreen; font-weight:700">
'Background Updating...' : ' '
</div>
Expand Down
4 changes: 2 additions & 2 deletions examples/svelte/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"devDependencies": {
"@sveltejs/adapter-auto": "^3.2.2",
"@sveltejs/kit": "^2.5.18",
"@sveltejs/vite-plugin-svelte": "^3.1.1",
"svelte": "^4.2.18",
"@sveltejs/vite-plugin-svelte": "^4.0.0-next.4",
"svelte": "5.0.0-next.192",
"svelte-check": "^3.8.4",
"typescript": "5.3.3",
"vite": "^5.3.5"
Expand Down
16 changes: 8 additions & 8 deletions examples/svelte/basic/src/lib/Post.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { getPostById } from './data'
import type { Post } from './types'
export let postId: number
const { postId }: { postId: number } = $props()
const post = createQuery<Post>({
queryKey: ['post', postId],
Expand All @@ -15,17 +15,17 @@
<div>
<a class="button" href="/"> Back </a>
</div>
{#if !postId || $post.isPending}
{#if !postId || post.isPending}
<span>Loading...</span>
{/if}
{#if $post.error}
<span>Error: {$post.error.message}</span>
{#if post.error}
<span>Error: {post.error.message}</span>
{/if}
{#if $post.isSuccess}
<h1>{$post.data.title}</h1>
{#if post.isSuccess}
<h1>{post.data.title}</h1>
<div>
<p>{$post.data.body}</p>
<p>{post.data.body}</p>
</div>
<div>{$post.isFetching ? 'Background Updating...' : ' '}</div>
<div>{post.isFetching ? 'Background Updating...' : ' '}</div>
{/if}
</div>
10 changes: 5 additions & 5 deletions examples/svelte/basic/src/lib/Posts.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

<div>
<div>
{#if $posts.status === 'pending'}
{#if posts.status === 'pending'}
<span>Loading...</span>
{:else if $posts.status === 'error'}
<span>Error: {$posts.error.message}</span>
{:else if posts.status === 'error'}
<span>Error: {posts.error.message}</span>
{:else}
<ul>
{#each $posts.data as post}
{#each posts.data as post}
<article>
<a
href={`/${post.id}`}
Expand All @@ -38,7 +38,7 @@
</article>
{/each}
</ul>
{#if $posts.isFetching}
{#if posts.isFetching}
<div style="color:darkgreen; font-weight:700">
Background Updating...
</div>
Expand Down
4 changes: 3 additions & 1 deletion examples/svelte/basic/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import { PersistQueryClientProvider } from '@tanstack/svelte-query-persist-client'
import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister'
const { children } = $props()
const queryClient = new QueryClient({
defaultOptions: {
queries: {
Expand All @@ -21,7 +23,7 @@

<PersistQueryClientProvider client={queryClient} persistOptions={{ persister }}>
<main>
<slot />
{@render children()}
</main>
<SvelteQueryDevtools />
</PersistQueryClientProvider>
3 changes: 1 addition & 2 deletions examples/svelte/basic/src/routes/[postId]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<script lang="ts">
import Post from '$lib/Post.svelte'
import type { PageData } from './$types'
export let data: PageData
const { data } = $props()
</script>

<Post postId={data.postId} />
4 changes: 2 additions & 2 deletions examples/svelte/load-more-infinite-scroll/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"devDependencies": {
"@sveltejs/adapter-auto": "^3.2.2",
"@sveltejs/kit": "^2.5.18",
"@sveltejs/vite-plugin-svelte": "^3.1.1",
"svelte": "^4.2.18",
"@sveltejs/vite-plugin-svelte": "^4.0.0-next.4",
"svelte": "5.0.0-next.192",
"svelte-check": "^3.8.4",
"typescript": "5.3.3",
"vite": "^5.3.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
})
</script>

{#if $query.isPending}
{#if query.isPending}
Loading...
{/if}
{#if $query.error}
<span>Error: {$query.error.message}</span>
{#if query.error}
<span>Error: {query.error.message}</span>
{/if}
{#if $query.isSuccess}
{#if query.isSuccess}
<div>
{#each $query.data.pages as { results }}
{#each query.data.pages as { results }}
{#each results as planet}
<div class="card">
<div class="card-body">
Expand All @@ -44,12 +44,12 @@
</div>
<div>
<button
on:click={() => $query.fetchNextPage()}
disabled={!$query.hasNextPage || $query.isFetchingNextPage}
onclick={() => query.fetchNextPage()}
disabled={!query.hasNextPage || query.isFetchingNextPage}
>
{#if $query.isFetching}
{#if query.isFetching}
Loading more...
{:else if $query.hasNextPage}
{:else if query.hasNextPage}
Load More
{:else}Nothing more to load{/if}
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { QueryClientProvider, QueryClient } from '@tanstack/svelte-query'
import { SvelteQueryDevtools } from '@tanstack/svelte-query-devtools'
const { children } = $props()
const queryClient = new QueryClient({
defaultOptions: {
queries: {
Expand All @@ -15,7 +17,7 @@

<QueryClientProvider client={queryClient}>
<main>
<slot />
{@render children()}
</main>
<SvelteQueryDevtools />
</QueryClientProvider>
4 changes: 2 additions & 2 deletions examples/svelte/optimistic-updates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"devDependencies": {
"@sveltejs/adapter-auto": "^3.2.2",
"@sveltejs/kit": "^2.5.18",
"@sveltejs/vite-plugin-svelte": "^3.1.1",
"svelte": "^4.2.18",
"@sveltejs/vite-plugin-svelte": "^4.0.0-next.4",
"svelte": "5.0.0-next.192",
"svelte-check": "^3.8.4",
"typescript": "5.3.3",
"vite": "^5.3.5"
Expand Down
4 changes: 3 additions & 1 deletion examples/svelte/optimistic-updates/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { QueryClientProvider, QueryClient } from '@tanstack/svelte-query'
import { SvelteQueryDevtools } from '@tanstack/svelte-query-devtools'
const { children } = $props()
const queryClient = new QueryClient({
defaultOptions: {
queries: {
Expand All @@ -15,7 +17,7 @@

<QueryClientProvider client={queryClient}>
<main>
<slot />
{@render children()}
</main>
<SvelteQueryDevtools />
</QueryClientProvider>
20 changes: 10 additions & 10 deletions examples/svelte/optimistic-updates/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -87,36 +87,36 @@
</p>

<form
on:submit={(e) => {
onsubmit={(e) => {
e.preventDefault()
e.stopPropagation()
$addTodoMutation.mutate(text)
addTodoMutation.mutate(text)
}}
>
<div>
<input type="text" bind:value={text} />
<button disabled={$addTodoMutation.isPending}>Create</button>
<button disabled={addTodoMutation.isPending}>Create</button>
</div>
</form>

{#if $todos.isPending}
{#if todos.isPending}
Loading...
{/if}
{#if $todos.error}
{#if todos.error}
An error has occurred:
{$todos.error.message}
{todos.error.message}
{/if}
{#if $todos.isSuccess}
{#if todos.isSuccess}
<div class="mb-4">
Updated At: {new Date($todos.data.ts).toLocaleTimeString()}
Updated At: {new Date(todos.data.ts).toLocaleTimeString()}
</div>
<ul>
{#each $todos.data.items as todo}
{#each todos.data.items as todo}
<li>{todo.text}</li>
{/each}
</ul>
{/if}
{#if $todos.isFetching}
{#if todos.isFetching}
<div style="color:darkgreen; font-weight:700">
'Background Updating...' : ' '
</div>
Expand Down
4 changes: 2 additions & 2 deletions examples/svelte/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"devDependencies": {
"@sveltejs/adapter-auto": "^3.2.2",
"@sveltejs/kit": "^2.5.18",
"@sveltejs/vite-plugin-svelte": "^3.1.1",
"svelte": "^4.2.18",
"@sveltejs/vite-plugin-svelte": "^4.0.0-next.4",
"svelte": "5.0.0-next.192",
"svelte-check": "^3.8.4",
"typescript": "5.3.3",
"vite": "^5.3.5"
Expand Down
4 changes: 3 additions & 1 deletion examples/svelte/playground/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { QueryClientProvider, QueryClient } from '@tanstack/svelte-query'
import { SvelteQueryDevtools } from '@tanstack/svelte-query-devtools'
const { children } = $props()
const queryClient = new QueryClient({
defaultOptions: {
queries: {
Expand All @@ -19,7 +21,7 @@

<QueryClientProvider client={queryClient}>
<div id="app">
<slot />
{@render children()}
</div>
<SvelteQueryDevtools />
</QueryClientProvider>
2 changes: 1 addition & 1 deletion examples/svelte/playground/src/routes/AddTodo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<input bind:value={name} disabled={$addMutation.status === 'pending'} />

<button
on:click={() => $addMutation.mutate({ name, notes: name })}
onclick={() => $addMutation.mutate({ name, notes: name })}
disabled={$addMutation.status === 'pending' || !name}
>
Add Todo
Expand Down
Loading

0 comments on commit 1fe6b77

Please sign in to comment.