Add a function createReactiveStoreFromRpcAndSubscription#1528
Conversation
🦋 Changeset detectedLatest commit: 7fc0245 The changes in this PR will be included in the next version bump. This PR includes changesets to release 46 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This stack of pull requests is managed by Graphite. Learn more about stacking. |
BundleMonFiles updated (4)
Unchanged files (138)
Total files change +1.72KB +0.35% Final result: ✅ View report in BundleMon website ➡️ |
|
Documentation Preview: https://kit-docs-nofv92u3r-anza-tech.vercel.app |
lorisleiva
left a comment
There was a problem hiding this comment.
Nice! I love that you can wrap a reactive store in another reactive store whilst providing an RPC pending request for the initial fetch. 👌
2cfa212 to
cbdcf38
Compare
632c232 to
20263d3
Compare
20263d3 to
f53ce07
Compare
cbdcf38 to
efcc9b8
Compare
This creates a reactive store that combines an RPC and RPC Subscription, that can both map to the same data type. It exposes the latest value from either source, by slot comparison.
efcc9b8 to
7fc0245
Compare
|
Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up. |

Problem
In my earlier PR I added an API to get a reactive store (subscribe, getState, getError) for RPC subscriptions. But another case, which is more complex for apps, is where you want to get the current state and then keep it updated with a subscription. An example of this was the balance functionality in our react app, which makes a call to
getBalanceand then subscribes to account updates, and keeps track of the slot to always accept only the most recent data. Just having a reactive subscription is insufficient for this use case.Summary of Changes
This PR adds a new function
createReactiveStoreFromRpcAndSubscription. It's added topakcages/kitsince it combines RPC and subscriptions.It takes as input:
SolanaRpcResponse, ie provides a slot in contextTItemSolanaRpcResponse, providing a slot in contextTItemIt then returns a
ReactiveStoreas defined in the previous PR which provides the latest data (by slot number) from either data source, and notifies subscribers only when newer data is available (or an error occurs). It handles the unified abort signal and error handling.This generalises the approach used in
balance.ts, while providing an output shape compatible with any reactive framework.The PR also updates the
balance.tsin the react-app to demonstrate this, binding the store to an SWR subscription.This would enable frameworks targeting react, SWR, or any other reactive library etc to provide bindings like
useBalance()with minimal framework logic.Outside of balance, it would be useful for any case where we have an RPC and a subscription providing the same data that can change. Anything on accounts (getAccountInfo + account notifications), and potentially also transaction signatures (
getSignatureStatuses+ signature notifications).I also realised afterward that we could provide an async generator API for this, which only yields values when the slot is newer. Same logic, but presented as an async generator API. I'll add that later as a follow up PR.