UNPKG

@solana/kit

Version:

Solana Javascript API

107 lines 5.9 kB
import type { SolanaRpcResponse } from '@solana/rpc-types'; import type { ReactiveActionSource, ReactiveStreamSource, ReactiveStreamStore } from '@solana/subscribable'; /** * Configuration for {@link createReactiveStoreWithInitialValueAndSlotTracking}. Pairs a one-shot * initial-value source with an ongoing stream source so the resulting store can hydrate from the * initial response and keep up to date with notifications, slot-deduplicating the two sources. * * @typeParam TInitialValue - The value type produced by `initialValueSource` (inside the {@link SolanaRpcResponse} envelope). * @typeParam TStreamValue - The value type emitted by `streamSource` (inside the {@link SolanaRpcResponse} envelope). * @typeParam TItem - The unified item type the store holds, produced by the two value mappers. * * @see {@link createReactiveStoreWithInitialValueAndSlotTracking} */ export type CreateReactiveStoreWithInitialValueAndSlotTrackingConfig<TInitialValue, TStreamValue, TItem> = Readonly<{ /** * Maps the value from the initial-value source's response to the item type stored in the * reactive store. */ initialValueMapper: (value: TInitialValue) => TItem; /** * A reactive action source whose dispatched value will be used to set the store's initial * state. The value must be a {@link SolanaRpcResponse} so that its slot can be compared with * stream notifications. Satisfied by `PendingRpcRequest`. */ initialValueSource: ReactiveActionSource<SolanaRpcResponse<TInitialValue>>; /** * A reactive stream source whose notifications will be used to keep the store up to date. Each * notification must be a {@link SolanaRpcResponse} so that its slot can be compared with the * initial value and with other notifications. Satisfied by `PendingRpcSubscriptionsRequest`. */ streamSource: ReactiveStreamSource<SolanaRpcResponse<TStreamValue>>; /** * Maps the value from a stream notification to the item type stored in the reactive store. */ streamValueMapper: (value: TStreamValue) => TItem; }>; /** * Creates a {@link ReactiveStreamStore} that combines an initial one-shot fetch with an ongoing * stream to keep its state up to date. * * The store uses slot-based comparison to ensure that only the most recent value is kept, * regardless of whether it came from the initial value source or a stream notification. This * prevents stale data from overwriting newer data when the two sources arrive out of order. * * The two sources are consumed via their {@link ReactiveActionSource.reactiveStore | `reactiveStore()`} * methods rather than by calling `send()` / `subscribe()` directly, so any object satisfying the * {@link ReactiveActionSource} / {@link ReactiveStreamSource} duck-types works — including * `PendingRpcRequest` / `PendingRpcSubscriptionsRequest` and plugin-authored wrappers. * * Things to note: * * - The returned store starts in `status: 'idle'`. Call * {@link ReactiveStreamStore.connect | `connect()`} to dispatch the initial-value source and open * the stream. * - The store transitions through `loading` until the first value or notification arrives, * then to `loaded` with a {@link SolanaRpcResponse} containing the value and the slot context * at which it was observed. * - On error from either source, the store transitions to `status: 'error'` preserving the last * known value. Only the first error per connection window is captured. * - A subsequent `connect()` aborts the current connection, transitions back to * `status: 'loading'` (preserving the last known `data` and `error` for stale-while-revalidate), * and re-builds both inner stores with a fresh inner abort signal. * - {@link ReactiveStreamStore.reset | `reset()`} aborts the current connection and returns the * store to `idle`, clearing `data` and `error`. * - Attach a caller-provided cancellation source via * {@link ReactiveStreamStore.withSignal | `withSignal()`} — `store.withSignal(signal).connect()` * composes the signal with the per-connection controller. Aborting the caller's signal * transitions the store to `error` with that abort reason. * * @param config * * @example * ```ts * import { * address, * createReactiveStoreWithInitialValueAndSlotTracking, * createSolanaRpc, * createSolanaRpcSubscriptions, * } from '@solana/kit'; * * const rpc = createSolanaRpc('http://127.0.0.1:8899'); * const rpcSubscriptions = createSolanaRpcSubscriptions('ws://127.0.0.1:8900'); * const myAddress = address('FnHyam9w4NZoWR6mKN1CuGBritdsEWZQa4Z4oawLZGxa'); * * const balanceStore = createReactiveStoreWithInitialValueAndSlotTracking({ * initialValueSource: rpc.getBalance(myAddress, { commitment: 'confirmed' }), * initialValueMapper: lamports => lamports, * streamSource: rpcSubscriptions.accountNotifications(myAddress), * streamValueMapper: ({ lamports }) => lamports, * }); * * const unsubscribe = balanceStore.subscribe(() => { * const state = balanceStore.getState(); * if (state.status === 'error') { * console.error('Error:', state.error); * balanceStore.connect(); * } else if (state.status === 'loaded') { * console.log(`Balance at slot ${state.data.context.slot}:`, state.data.value); * } * }); * balanceStore.withSignal(AbortSignal.timeout(60_000)).connect(); * ``` * * @see {@link ReactiveStreamStore} */ export declare function createReactiveStoreWithInitialValueAndSlotTracking<TInitialValue, TStreamValue, TItem>({ initialValueMapper, initialValueSource, streamSource, streamValueMapper, }: CreateReactiveStoreWithInitialValueAndSlotTrackingConfig<TInitialValue, TStreamValue, TItem>): ReactiveStreamStore<SolanaRpcResponse<TItem>>; //# sourceMappingURL=create-reactive-store-with-initial-value-and-slot-tracking.d.ts.map