-
Notifications
You must be signed in to change notification settings - Fork 181
Add reactiveStore to PendingRpcRequest #1555
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| '@solana/rpc-spec': minor | ||
| '@solana/kit': minor | ||
| --- | ||
|
|
||
| Add a `reactiveStore()` method to `PendingRpcRequest`. It fires the request on construction and synchronously returns a `ReactiveActionStore` that holds the request's `idle`/`running`/`success`/`error` lifecycle state. Compatible with `useSyncExternalStore`, Svelte stores, and other reactive primitives. Call `dispatch()` to re-fire the request (e.g. after an error), or `reset()` to abort the in-flight call and return to idle. | ||
|
|
||
| ```ts | ||
| const store = rpc.getAccountInfo(address).reactiveStore(); | ||
| const state = useSyncExternalStore(store.subscribe, store.getState); | ||
| if (state.status === 'error') return <ErrorMessage error={state.error} onRetry={store.dispatch} />; | ||
| if (state.status === 'running' && !state.data) return <Spinner />; | ||
| return <View data={state.data!} />; | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { SOLANA_ERROR__RPC__API_PLAN_MISSING_FOR_RPC_METHOD, SolanaError } from '@solana/errors'; | ||
| import { Callable, Flatten, OverloadImplementations, UnionToIntersection } from '@solana/rpc-spec-types'; | ||
| import { createReactiveActionStore, ReactiveActionStore } from '@solana/subscribable'; | ||
|
|
||
| import { RpcApi, RpcPlan } from './rpc-api'; | ||
| import { RpcTransport } from './rpc-transport'; | ||
|
|
@@ -26,8 +27,29 @@ export type Rpc<TRpcMethods> = { | |
| * Calling the {@link PendingRpcRequest.send | `send(options)`} method on a | ||
| * {@link PendingRpcRequest | PendingRpcRequest<TResponse>} will trigger the request and return a | ||
| * promise for `TResponse`. | ||
| * | ||
| * Calling the {@link PendingRpcRequest.reactiveStore | `reactiveStore()`} method will fire the | ||
| * request and return a {@link ReactiveActionStore} compatible with `useSyncExternalStore`, Svelte | ||
| * stores, and other reactive primitives. | ||
| */ | ||
| export type PendingRpcRequest<TResponse> = { | ||
| /** | ||
| * Synchronously returns a {@link ReactiveActionStore} that fires the request on construction | ||
| * and holds its lifecycle state. Compatible with `useSyncExternalStore` and other reactive | ||
| * primitives that expect a `{ subscribe, getState }` contract. Call `dispatch()` to re-fire the | ||
| * request (for example after an error), or `reset()` to abort the in-flight call and return to | ||
| * `status: 'idle'`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const store = rpc.getAccountInfo(address).reactiveStore(); | ||
| * const state = useSyncExternalStore(store.subscribe, store.getState); | ||
| * if (state.status === 'error') return <ErrorMessage error={state.error} onRetry={store.dispatch} />; | ||
| * if (state.status === 'running' && !state.data) return <Spinner />; | ||
| * return <View data={state.data!} />; | ||
| * ``` | ||
| */ | ||
| reactiveStore(): ReactiveActionStore<[], TResponse>; | ||
| send(options?: RpcSendOptions): Promise<TResponse>; | ||
| }; | ||
|
|
||
|
|
@@ -96,6 +118,11 @@ function createPendingRpcRequest<TRpcMethods, TRpcTransport extends RpcTransport | |
| plan: RpcPlan<TResponse>, | ||
| ): PendingRpcRequest<TResponse> { | ||
| return { | ||
| reactiveStore(): ReactiveActionStore<[], TResponse> { | ||
| const store = createReactiveActionStore<[], TResponse>(signal => plan.execute({ signal, transport })); | ||
|
Comment on lines
118
to
+122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question (non-blocking): would it be worth accepting an optional
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Considered this but I don't think it's needed. There's nothing that can leak here - the http request is abortable and doesn't leave anything behind. In contrast to subscriptions where we have a subscription to clean up. Having both signals requires fiddly wiring in subscriptions, which is worth it there but would just be complexity for no value here IMO. |
||
| store.dispatch(); | ||
| return store; | ||
| }, | ||
| async send(options?: RpcSendOptions): Promise<TResponse> { | ||
| return await plan.execute({ signal: options?.abortSignal, transport }); | ||
| }, | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.