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

fix: preserve order of providers as passed into useInitializeProviders #111

Merged
merged 2 commits into from
Sep 22, 2023

Conversation

No-Cash-7970
Copy link
Contributor

Description

This is a fix for issue #104. With this fix, the order of the providers when passed into the useInitializeProviders hook will determine the order of providers in the providers array returned by the useWallet hook.

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary

@No-Cash-7970
Copy link
Contributor Author

This was an issue that bugged me, so I was determined to fix it. 😂

@drichar
Copy link
Collaborator

drichar commented Sep 22, 2023

This was an issue that bugged me, so I was determined to fix it. 😂

Love this!! Thank you so much for taking the initiative to provide a solution. In the NFDomains wallet menu, I work around this by using a sort function and an array of PROVIDER_IDs in the order I want the providers to be in. After this change none of that will be necessary!

You've got the right idea here. The goal is to set the order of initializedProviders properties up front, instead of letting the order of promises resolving determine the order of the properties in the returned object.

But instead of doing this by introducing a side effect to the map function (which is only meant to produce an array of promises), I think a better approach would be to reduce the providers array when initializeProviders is defined:

if (typeof window === 'undefined') {
  debugLog('Window object is not available, skipping initialization')
  return {} as SupportedProviders
}

const initializedProviders = providers.reduce((acc, provider) => {
  const providerId = typeof provider === 'string'? provider : provider.id
  acc[providerId] = null  // Set to null to preserve order of providers
  return acc
}, {} as SupportedProviders)

This achieves the same result, while being more readable and a clearer separation of concerns. What do you think?

@drichar
Copy link
Collaborator

drichar commented Sep 22, 2023

You can implement the proposed change here if you like, or I can add a commit if that's easier. Excited to get this merged and released.

…oviders()`

This change makes the separation of concerns clearer.
@No-Cash-7970
Copy link
Contributor Author

@drichar
Thanks for the suggestion! I agree your solution has a clearer separation of concerns, so I made the change.

@drichar drichar added the feature request New feature or enhancement label Sep 22, 2023
@drichar drichar merged commit 35a6333 into TxnLab:main Sep 22, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants