-
Notifications
You must be signed in to change notification settings - Fork 14
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
feat: atomsWithQueryAsync, plus example #30
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6e70cb3
feat: atomsWithQueryAsync, plus example
leweyse ed76d68
fix: import declaration, plus test
leweyse 68a29b6
fix: added async example to package.json
leweyse 086c810
wip: add extra tests
leweyse d90a6c0
fix: use jotai types in atomsWithQueryAsync
leweyse 296ac12
fix: complete tests for atomsWithQueryAsync, update types and reject …
leweyse 4b08e75
feat: more tests for atomsWithQueryAsync
leweyse acffaf7
fix: valid force arg and types
leweyse 9bb7892
fix: linter error
leweyse 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,51 @@ | ||
import React, { StrictMode, Suspense } from 'react' | ||
import { render } from '@testing-library/react' | ||
import { atom, useAtom } from 'jotai' | ||
import { atomsWithQueryAsync } from '../src/index' | ||
|
||
it('async query basic test', async () => { | ||
let resolve = () => {} | ||
let resolveQuery = () => {} | ||
|
||
const userIdAtom = atom(async () => { | ||
await new Promise<void>((r) => (resolve = r)) | ||
return 2 | ||
}) | ||
const [userAtom] = atomsWithQueryAsync(async (get) => { | ||
const userId = await get(userIdAtom) | ||
|
||
return { | ||
queryKey: ['idTest', userId], | ||
queryFn: async ({ queryKey: [, id] }) => { | ||
await new Promise<void>((r) => (resolveQuery = r)) | ||
return { response: { id: id as number } } | ||
}, | ||
} | ||
}) | ||
const User = () => { | ||
const [ | ||
{ | ||
response: { id }, | ||
}, | ||
] = useAtom(userAtom) | ||
|
||
return ( | ||
<> | ||
<div>id: {id}</div> | ||
</> | ||
) | ||
} | ||
const { findByText } = render( | ||
<StrictMode> | ||
<Suspense fallback="loading"> | ||
<User /> | ||
</Suspense> | ||
</StrictMode> | ||
) | ||
|
||
await findByText('loading') | ||
resolve() | ||
await new Promise((r) => setTimeout(r)) // wait a tick | ||
resolveQuery() | ||
await findByText('id: 2') | ||
}) |
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,28 @@ | ||
{ | ||
"name": "jotai-tanstack-query-example", | ||
"version": "0.1.0", | ||
"private": true, | ||
"dependencies": { | ||
"@tanstack/query-core": "latest", | ||
"@types/react": "latest", | ||
"@types/react-dom": "latest", | ||
"jotai": "latest", | ||
"jotai-tanstack-query": "latest", | ||
"react": "latest", | ||
"react-dom": "latest", | ||
"react-scripts": "latest", | ||
"typescript": "latest" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
"build": "react-scripts build", | ||
"test": "react-scripts test", | ||
"eject": "react-scripts eject" | ||
}, | ||
"browserslist": [ | ||
">0.2%", | ||
"not dead", | ||
"not ie <= 11", | ||
"not op_mini all" | ||
] | ||
} |
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,8 @@ | ||
<html> | ||
<head> | ||
<title>jotai-tanstack-query example</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
</body> | ||
</html> |
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,32 @@ | ||
import React from 'react' | ||
import { atom, useAtomValue } from 'jotai' | ||
import { atomsWithQueryAsync } from 'jotai-tanstack-query' | ||
|
||
const idAtom = atom(async () => { | ||
await new Promise((resolve) => setTimeout(resolve, 2000)) | ||
return 2 | ||
}) | ||
|
||
const [userAtom] = atomsWithQueryAsync(async (get) => { | ||
const id = await get(idAtom) | ||
return { | ||
queryKey: ['getUser', id], | ||
queryFn: async () => { | ||
const res = await fetch('https://reqres.in/api/users/' + id) | ||
return res.json() as Promise<{ data: unknown }> | ||
}, | ||
} | ||
}) | ||
|
||
const UserData = () => { | ||
const data = useAtomValue(userAtom) | ||
return <div>{JSON.stringify(data)}</div> | ||
} | ||
|
||
const App = () => ( | ||
<React.Suspense fallback="Loading..."> | ||
<UserData /> | ||
</React.Suspense> | ||
) | ||
|
||
export default App |
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,8 @@ | ||
import React from 'react' | ||
import { createRoot } from 'react-dom/client' | ||
import App from './App' | ||
|
||
const ele = document.getElementById('app') | ||
if (ele) { | ||
createRoot(ele).render(React.createElement(App)) | ||
} |
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
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,56 @@ | ||
import type { QueryKey, QueryObserverOptions } from '@tanstack/query-core' | ||
import type { ExtractAtomValue, Getter, WritableAtom } from 'jotai' | ||
import { atom } from 'jotai' | ||
import { atomsWithQuery } from './atomsWithQuery' | ||
|
||
type ExtractAtomArgs<AtomType> = AtomType extends WritableAtom< | ||
any, | ||
infer Args, | ||
any | ||
> | ||
? Args | ||
: never | ||
|
||
export function atomsWithQueryAsync< | ||
TQueryFnData = unknown, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey | ||
>( | ||
getOptions: ( | ||
get: Getter | ||
) => Promise< | ||
QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey> | ||
> | ||
) { | ||
const atomsAtom = atom(async (get) => { | ||
const options = await getOptions(get) | ||
return atomsWithQuery(() => options) | ||
}) | ||
|
||
const dataAtom = atom( | ||
async (get) => get((await get(atomsAtom))[0]), | ||
async ( | ||
get, | ||
set, | ||
...args: ExtractAtomArgs<Awaited<ExtractAtomValue<typeof atomsAtom>>[0]> | ||
) => { | ||
const a = (await get(atomsAtom))[0] | ||
return set(a, ...args) | ||
} | ||
) | ||
|
||
const statusAtom = atom( | ||
async (get) => get((await get(atomsAtom))[1]), | ||
async ( | ||
get, | ||
set, | ||
...args: ExtractAtomArgs<Awaited<ExtractAtomValue<typeof atomsAtom>>[1]> | ||
) => { | ||
const a = (await get(atomsAtom))[1] | ||
return set(a, ...args) | ||
} | ||
) | ||
return [dataAtom, statusAtom] as const | ||
} |
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
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.
With pmndrs/jotai#1741, we should be able to use it.