Skip to content
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

Reduce dependency on the VM #1287

Merged
merged 18 commits into from
Aug 27, 2024
Merged

Reduce dependency on the VM #1287

merged 18 commits into from
Aug 27, 2024

Conversation

gagdiez
Copy link
Collaborator

@gagdiez gagdiez commented Aug 9, 2024

Currently, the BOS VM is loaded and initialized on every single page - even those that do not depend on it. This creates a lot of delay for page loading, and even slows down the initialization of the wallet (which should be instant, and instead takes a couple of seconds waiting for the VM to load).

This PR separates the wallet selector (used to login) from the loading and initialization of the VM (used to render components), and makes sure that the VM is only loaded when a component has to be rendered.

Separating the VM from the wallet selector allows a couple of improvements:

  • The user can see that they are logged in without needing to wait for the VM to be loaded
  • Pages that do not rely on the VM load much faster, since they do not need to wait for the VM to load

closes #1264

Copy link

vercel bot commented Aug 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
beta.near.org ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 26, 2024 5:44pm
dev-near-org ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 26, 2024 5:44pm
near-discovery-testnet ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 26, 2024 5:44pm

@gagdiez gagdiez marked this pull request as draft August 9, 2024 17:32
@gagdiez gagdiez removed the enhancement To highlight a PR's description in the 'Enhancements' changelog section label Aug 10, 2024
Copy link
Collaborator

@calebjacob calebjacob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on @gagdiez! One thing I'm hesitant on with this refactoring is the confusion it creates for knowing when to use useAuthStore() vs useContext(NearContext). I'm pretty familiar with this codebase and I have to admit that I'm already a bit confused.

I might suggest picking one of these routes to continue the refactor before merging:

  1. Completely remove stores/auth.ts and refactor away all uses of useAuthStore().
  2. Rework your initialization logic to store state in stores/auth.ts so that we can continue with our familiar pattern of useAuthStore() - in this way we wouldn't be calling useContext(NearContext) directly and would probably delete NearContext all together.

I would probably lean towards option 2 since Zustand tends to make our interactions more consistent as complexities grow.

What do you think @shelegdmitriy?

@shelegdmitriy
Copy link
Contributor

I like the usage of useContext but I'd stick with our familiar pattern of useAuthStore()

@gagdiez
Copy link
Collaborator Author

gagdiez commented Aug 12, 2024

Sure, I can go back to the useStore, I used context because I thought it was more standard practice, but happy to use the existing pattern

@calebjacob
Copy link
Collaborator

Either way works for me @gagdiez! I think the only important thing is to make sure we only have one source of truth for auth/wallet state and interactions (right now it's kind of split between store/auth and the NearContext).

@gagdiez
Copy link
Collaborator Author

gagdiez commented Aug 14, 2024

@calebjacob migrated everything to use context. I might need some help with the rxjs since I have no idea what it is actually doing.

@calebjacob
Copy link
Collaborator

@gagdiez Nice! This is how I've always interacted with it without rxjs:

const subscription = selector.store.observable.subscribe(async (value) => {
  setState(value);
  setAccount(value?.accounts[0] ?? null);

  if (value.accounts.length > 0 && value.selectedWalletId && selector) {
    const wallet = await selector.wallet();
    setWallet(wallet);
  } else {
    setWallet(null);
  }
});

Wherever you have cleanup or unmount logic, you can unsubscribe:

subscription.unsubscribe();

@gagdiez
Copy link
Collaborator Author

gagdiez commented Aug 26, 2024

@calebjacob thanks! I changed the code to not use rxjs, this is ready for a final review

@gagdiez gagdiez merged commit b2d9170 into develop Aug 27, 2024
7 checks passed
@gagdiez gagdiez deleted the use-less-vm branch August 27, 2024 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Highlight a PR's description in the 'Break Changes' changelog section
Projects
Status: Shipped 🚀
Development

Successfully merging this pull request may close these issues.

3 participants