Skip to content

Commit

Permalink
fix: preserve order of providers as passed into `useInitializeProvide…
Browse files Browse the repository at this point in the history
…rs` (#111)

* fix: preserve order of providers as passed into `useInitializeProviders`

Closes #104

* refactor: move code to preserve order of providers into `initializeProviders()`

This change makes the separation of concerns clearer.
  • Loading branch information
No-Cash-7970 authored Sep 22, 2023
1 parent b8bf195 commit 35a6333
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/utils/initializeProviders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('initializeProviders', () => {
const result = await initializeProviders(providers)

// Check if the returned object has the correct keys
expect(Object.keys(result)).toEqual(expect.arrayContaining(providers))
expect(Object.keys(result)).toEqual(providers)

// Check if the returned clients are instances of BaseClient
for (const clientInstance of Object.values(result)) {
Expand Down
10 changes: 8 additions & 2 deletions src/utils/initializeProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ export const initializeProviders = async <T extends keyof ProviderConfigMapping>
nodeConfig?: NodeConfig,
algosdkStatic?: typeof algosdk
): Promise<SupportedProviders> => {
const initializedProviders: SupportedProviders = {}

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

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

const {
network = DEFAULT_NETWORK,
nodeServer = DEFAULT_NODE_BASEURL,
Expand Down

0 comments on commit 35a6333

Please sign in to comment.