-
Notifications
You must be signed in to change notification settings - Fork 33
feat: add Svelte support #91
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
Draft
wobsoriano
wants to merge
16
commits into
TanStack:main
Choose a base branch
from
wobsoriano:rob/svelte-support
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.
Draft
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a73674e
chore: scaffold svelte app
wobsoriano a730379
feat: add Svelte support
wobsoriano 3ca1f6d
chore: fix tests
wobsoriano 7900bf1
chore: adjust effect roots
wobsoriano 558af77
chore: remove unused packages
wobsoriano f555db5
chore: run dedupe
wobsoriano 1d7c2dd
chore: update scripts
wobsoriano 4be68d6
chore: remove unused packages
wobsoriano 013ac28
chore: add changeset
wobsoriano 546ef4e
chore: update readme
wobsoriano 8e8804f
chore: prettier fix
wobsoriano 0aad000
chore: update changeset
wobsoriano 248180a
chore: update vite config
wobsoriano e83cd59
chore: remove use of store
wobsoriano 284a807
chore: remove unused package
wobsoriano b8444cd
chore: add todo
wobsoriano 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
"@tanstack/svelte-db": patch | ||
--- | ||
|
||
Add Svelte support | ||
|
||
Usage example: | ||
|
||
```svelte | ||
<script lang="ts"> | ||
import { useLiveQuery } from "@tanstack/svelte-db" | ||
import { todoCollection } from "$lib/collections" | ||
|
||
const query = useLiveQuery((query) => | ||
query.from({ todoCollection }).where("@completed", "=", false) | ||
) | ||
</script> | ||
|
||
|
||
<List items={query.data} /> | ||
``` |
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,24 @@ | ||
node_modules | ||
|
||
# Output | ||
.output | ||
.vercel | ||
.netlify | ||
.wrangler | ||
/.svelte-kit | ||
/build | ||
/dist | ||
|
||
# OS | ||
.DS_Store | ||
Thumbs.db | ||
|
||
# Env | ||
.env | ||
.env.* | ||
!.env.example | ||
!.env.test | ||
|
||
# Vite | ||
vite.config.js.timestamp-* | ||
vite.config.ts.timestamp-* |
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,3 @@ | ||
# @tanstack/svelte-db | ||
|
||
Svelte hooks for TanStack DB. See [TanStack/db](https://github.com/TanStack/db) for more details. |
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,47 @@ | ||
{ | ||
"name": "@tanstack/svelte-db", | ||
"description": "Svelte integration for @tanstack/db", | ||
"version": "0.0.0", | ||
"scripts": { | ||
"build": "svelte-package --input ./src --output ./dist", | ||
"test": "npx vitest --run", | ||
"lint": "eslint . --fix" | ||
}, | ||
"files": [ | ||
"dist", | ||
"!dist/**/*.test.*", | ||
"!dist/**/*.spec.*" | ||
], | ||
"sideEffects": [ | ||
"**/*.css" | ||
], | ||
"svelte": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"svelte": "./dist/index.js" | ||
} | ||
}, | ||
"dependencies": { | ||
"@tanstack/db": "workspace:*", | ||
"@tanstack/svelte-store": "^0.7.0" | ||
}, | ||
"peerDependencies": { | ||
"svelte": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"@sveltejs/package": "^2.3.11", | ||
"@vitest/coverage-istanbul": "^3.0.9", | ||
"@sveltejs/vite-plugin-svelte": "^5.0.3", | ||
"publint": "^0.3.2", | ||
"svelte": "^5.28.6", | ||
"svelte-check": "^4.2.0" | ||
}, | ||
"keywords": [ | ||
"optimistic", | ||
"svelte", | ||
"typescript" | ||
] | ||
} |
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,10 @@ | ||
// Re-export all public APIs | ||
export * from "./useOptimisticMutation.js" | ||
export * from "./useLiveQuery.svelte.js" | ||
|
||
// Re-export everything from @tanstack/db | ||
export * from "@tanstack/db" | ||
|
||
// Re-export some stuff explicitly to ensure the type & value is exported | ||
export { Collection } from "@tanstack/db" | ||
export { createTransaction } from "@tanstack/db" |
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,58 @@ | ||
import { useStore } from "@tanstack/svelte-store" | ||
import { compileQuery, queryBuilder } from "@tanstack/db" | ||
import type { | ||
Collection, | ||
Context, | ||
InitialQueryBuilder, | ||
QueryBuilder, | ||
ResultsFromContext, | ||
Schema, | ||
} from "@tanstack/db" | ||
|
||
export interface UseLiveQueryReturn<T extends object> { | ||
state: Map<string, T> | ||
data: Array<T> | ||
collection: Collection<T> | ||
} | ||
|
||
export function useLiveQuery< | ||
TResultContext extends Context<Schema> = Context<Schema>, | ||
>( | ||
queryFn: ( | ||
q: InitialQueryBuilder<Context<Schema>> | ||
) => QueryBuilder<TResultContext>, | ||
deps: Array<() => unknown> = [] | ||
): UseLiveQueryReturn<ResultsFromContext<TResultContext>> { | ||
const compiledQuery = $derived.by(() => { | ||
// Just reference deps to make derived reactive to them | ||
deps.forEach((dep) => dep()) | ||
|
||
const query = queryFn(queryBuilder()) | ||
const compiled = compileQuery(query) | ||
compiled.start() | ||
return compiled | ||
}) | ||
|
||
const state = () => useStore(compiledQuery.results.derivedState).current | ||
const data = () => useStore(compiledQuery.results.derivedArray).current | ||
|
||
$effect(() => { | ||
return () => { | ||
compiledQuery.stop() | ||
} | ||
}) | ||
|
||
return { | ||
get state() { | ||
return state() | ||
}, | ||
get data() { | ||
return data() | ||
}, | ||
get collection() { | ||
return compiledQuery.results as unknown as Collection< | ||
ResultsFromContext<TResultContext> | ||
> | ||
}, | ||
} | ||
} |
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,15 @@ | ||
import { createTransaction } from "@tanstack/db" | ||
import type { Transaction, TransactionConfig } from "@tanstack/db" | ||
|
||
export function useOptimisticMutation(config: TransactionConfig) { | ||
return { | ||
mutate: (callback: () => void): Transaction => { | ||
const transaction = createTransaction(config) | ||
transaction.mutate(callback) | ||
return transaction | ||
}, | ||
createTransaction: (): Transaction => { | ||
return createTransaction({ ...config, autoCommit: false }) | ||
}, | ||
} | ||
} |
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,7 @@ | ||
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte" | ||
|
||
const config = { | ||
preprocess: vitePreprocess(), | ||
} | ||
|
||
export default config |
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.
Originally tried to use a
$derived
function here, butuseStore
for some reason is not reactive. This is still valid usage.Uh oh!
There was an error while loading. Please reload this page.
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.
Same problem in Solid, I think if we want it to work
useStore
needs to receive a function otherwise the signal/rune forcompiledQuery
is not registered inuseStore
.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.
Yeah I updated it to not use
useStore
for now. I think as long as the tests pass then we're good 👍🏼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.
The tests may pass but the value will not be reactive, which may cause the component not to update.
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.
Have you seen the updated code? What do you think about it?
So this means all tanstack store packages needs to be updated then?