Skip to content

Commit 24782fe

Browse files
committed
fixup! Reorganize & clean exchange-rate logic
1 parent 85ddffe commit 24782fe

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/actions/ExchangeRateActions.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type AssetPair = ReturnType<typeof asAssetPair>
3535
type ExchangeRateCache = ReturnType<typeof asExchangeRateCache>
3636
type ExchangeRateCacheFile = ReturnType<typeof asExchangeRateCacheFile>
3737

38-
const exchangeRateCache: ExchangeRateCache = {}
38+
let exchangeRateCache: ExchangeRateCache = {}
3939

4040
const asRatesResponse = asObject({
4141
data: asArray(
@@ -74,8 +74,18 @@ async function buildExchangeRates(state: RootState): Promise<GuiExchangeRates> {
7474
let hasWallets = false
7575
let hasCachedRates = false
7676

77-
// Load exchange rate cache off disk, if we haven't done that yet:
78-
if (Object.keys(exchangeRateCache).length === 0) {
77+
// If we have loaded the cache before, keep any un-expired entries:
78+
const rateCache: ExchangeRateCache = {}
79+
const cachedKeys = Object.keys(exchangeRateCache)
80+
if (cachedKeys.length > 0) {
81+
for (const key of cachedKeys) {
82+
if (exchangeRateCache[key].expiration > now) {
83+
rateCache[key] = exchangeRateCache[key]
84+
hasCachedRates = true
85+
}
86+
}
87+
} else {
88+
// Load exchange rate cache off disk, since we haven't done that yet:
7989
try {
8090
const raw = await disklet.getText(EXCHANGE_RATES_FILENAME)
8191
const json = JSON.parse(raw)
@@ -84,7 +94,7 @@ async function buildExchangeRates(state: RootState): Promise<GuiExchangeRates> {
8494
// Keep un-expired rates:
8595
for (const key of Object.keys(rates)) {
8696
if (rates[key].expiration > now) {
87-
exchangeRateCache[key] = rates[key]
97+
rateCache[key] = rates[key]
8898
hasCachedRates = true
8999
}
90100
}
@@ -185,13 +195,13 @@ async function buildExchangeRates(state: RootState): Promise<GuiExchangeRates> {
185195
const key = isHistorical ? `${currencyPair}_${date}` : currencyPair
186196

187197
if (exchangeRate != null) {
188-
exchangeRateCache[key] = {
198+
rateCache[key] = {
189199
expiration: rateExpiration,
190200
rate: exchangeRate
191201
}
192-
} else if (exchangeRateCache[key] == null) {
202+
} else if (rateCache[key] == null) {
193203
// We at least need a placeholder:
194-
exchangeRateCache[key] = {
204+
rateCache[key] = {
195205
expiration: 0,
196206
rate: '0'
197207
}
@@ -208,18 +218,19 @@ async function buildExchangeRates(state: RootState): Promise<GuiExchangeRates> {
208218
// Save exchange rate cache to disk:
209219
try {
210220
const exchangeRateCacheFile: ExchangeRateCacheFile = {
211-
rates: exchangeRateCache,
221+
rates: rateCache,
212222
assetPairs: filteredAssetPairs
213223
}
214224
await disklet.setText(EXCHANGE_RATES_FILENAME, JSON.stringify(exchangeRateCacheFile))
215225
} catch (e) {
216226
datelog('Error saving exchange rate cache:', String(e))
217227
}
228+
exchangeRateCache = rateCache
218229

219230
// Build the GUI rate structure:
220231
const serverRates: GuiExchangeRates = { 'iso:USD_iso:USD': '1' }
221-
for (const key of Object.keys(exchangeRateCache)) {
222-
const { rate } = exchangeRateCache[key]
232+
for (const key of Object.keys(rateCache)) {
233+
const { rate } = rateCache[key]
223234
serverRates[key] = rate
224235

225236
// Include reverse rates:

0 commit comments

Comments
 (0)