Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
apply suggestion from CodeRabbit to initialize Map in constructor
  • Loading branch information
kevindavee committed Oct 3, 2024
commit 282a30b831341885beef0ced31e9b31f074f8d10
20 changes: 6 additions & 14 deletions packages/currency/src/currencyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export class CurrencyManager<TMeta = unknown> implements CurrencyTypes.ICurrency
private readonly legacyTokens: CurrencyTypes.LegacyTokenMap;
private readonly conversionPairs: CurrencyTypes.AggregatorsMap;

private readonly knownCurrenciesById: Map<string, CurrencyTypes.CurrencyDefinition<TMeta>> =
new Map();
private readonly knownCurrenciesById: Map<string, CurrencyTypes.CurrencyDefinition<TMeta>>;

private static defaultInstance: CurrencyManager;

Expand All @@ -44,6 +43,10 @@ export class CurrencyManager<TMeta = unknown> implements CurrencyTypes.ICurrency
}
this.knownCurrencies.push(currency);
}

this.knownCurrenciesById = new Map(
this.knownCurrencies.map((knownCurrency) => [knownCurrency.id, knownCurrency]),
);
this.legacyTokens = legacyTokens || CurrencyManager.getDefaultLegacyTokens();
this.conversionPairs = conversionPairs || CurrencyManager.getDefaultConversionPairs();
}
Expand Down Expand Up @@ -87,18 +90,7 @@ export class CurrencyManager<TMeta = unknown> implements CurrencyTypes.ICurrency
* Gets a supported currency from its CurrencyTypes.CurrencyDefinition id
*/
fromId(id: string): CurrencyTypes.CurrencyDefinition<TMeta> | undefined {
const cachedCurrency = this.knownCurrenciesById.get(id);
if (cachedCurrency) {
return cachedCurrency;
}

const currency = this.knownCurrencies.find((knownCurrency) => knownCurrency.id === id);
if (!currency) {
return undefined;
}

this.knownCurrenciesById.set(id, currency);
return currency;
return this.knownCurrenciesById.get(id);
}

/**
Expand Down
6 changes: 0 additions & 6 deletions packages/currency/test/currencyManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,6 @@ describe('CurrencyManager', () => {
symbol: 'USDC-multichain',
decimals: 6,
});

// Run the test twice to ensure caching result don't break things.
expect(currencyManager.fromId('USDC-multichain-moonbeam')).toMatchObject({
symbol: 'USDC-multichain',
decimals: 6,
});
});

it('access a common token by its symbol', () => {
Expand Down