-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hook for pre-loading queries (#127)
- Loading branch information
Showing
8 changed files
with
546 additions
and
6 deletions.
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,5 @@ | ||
--- | ||
"@reactive-dot/react": minor | ||
--- | ||
|
||
Added hook for pre-loading queries. |
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
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,64 @@ | ||
import { queryPayloadAtomFamily } from "../stores/query.js"; | ||
import type { ChainHookOptions } from "./types.js"; | ||
import { useChainId_INTERNAL } from "./use-chain-id.js"; | ||
import { | ||
type ChainId, | ||
type Chains, | ||
type CommonDescriptor, | ||
Query, | ||
type QueryInstruction, | ||
} from "@reactive-dot/core"; | ||
import type { Getter } from "jotai"; | ||
import { useAtomCallback } from "jotai/utils"; | ||
import { useCallback } from "react"; | ||
|
||
/** | ||
* Hook for loading queries without suspending. | ||
* | ||
* @returns The function to load queries | ||
*/ | ||
export function useQueryLoader() { | ||
const chainId = useChainId_INTERNAL(); | ||
|
||
const _loadQuery = useCallback( | ||
(get: Getter) => | ||
< | ||
TQuery extends ( | ||
builder: Query<[], TDescriptor>, | ||
) => Query<QueryInstruction<TDescriptor>[], TDescriptor>, | ||
TDescriptor extends TChainId extends void | ||
? CommonDescriptor | ||
: Chains[TChainId], | ||
TChainId extends ChainId, | ||
>( | ||
builder: TQuery, | ||
options?: ChainHookOptions<TChainId>, | ||
) => { | ||
const query = builder(new Query([])); | ||
|
||
void get( | ||
queryPayloadAtomFamily({ | ||
chainId: options?.chainId ?? chainId, | ||
query, | ||
}), | ||
); | ||
}, | ||
[chainId], | ||
); | ||
|
||
const loadQuery = useAtomCallback( | ||
useCallback( | ||
( | ||
get, | ||
_, | ||
builder: <TChainId extends ChainId>( | ||
query: Query<[]>, | ||
options?: ChainHookOptions<TChainId>, | ||
) => Query<[]>, | ||
) => _loadQuery(get)(builder), | ||
[_loadQuery], | ||
), | ||
); | ||
|
||
return loadQuery as ReturnType<typeof _loadQuery>; | ||
} |
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
Oops, something went wrong.