Skip to content

Commit

Permalink
Make MarketDataCache a singleton with a clear() method that can be us…
Browse files Browse the repository at this point in the history
…ed when switching networks.

Signed-off-by: Simon Viénot <[email protected]>
  • Loading branch information
svienot committed Nov 23, 2023
1 parent 7886895 commit c013a27
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/components/dashboard/HbarMarketDashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ export default defineComponent({
const hbarTotalLabel = 'HBAR TOTAL'

// marketDataCache
const marketDataCache = new MarketDataCache()
onMounted(() => marketDataCache.mount())
onBeforeUnmount(() => marketDataCache.unmount())
onMounted(() => MarketDataCache.instance.mount())
onBeforeUnmount(() => MarketDataCache.instance.unmount())

return {
isMainNetwork,
Expand All @@ -123,13 +122,13 @@ export default defineComponent({
hbarMarketCapLabel,
hbarReleasedLabel,
hbarTotalLabel,
marketDataCache, // For testing purpose
hbarReleased: marketDataCache.hbarReleased,
hbarTotal: marketDataCache.hbarTotal,
hbarPrice: marketDataCache.hbarPrice,
hbarPriceVariation: marketDataCache.hbarPriceVariation,
hbarMarketCap: marketDataCache.hbarMarketCap,
hbarMarketCapVariation: marketDataCache.hbarMarketCapVariation,
marketDataCache: MarketDataCache.instance, // For testing purpose
hbarReleased: MarketDataCache.instance.hbarReleased,
hbarTotal: MarketDataCache.instance.hbarTotal,
hbarPrice: MarketDataCache.instance.hbarPrice,
hbarPriceVariation: MarketDataCache.instance.hbarPriceVariation,
hbarMarketCap: MarketDataCache.instance.hbarMarketCap,
hbarMarketCapVariation: MarketDataCache.instance.hbarMarketCapVariation,
}
},
});
Expand Down
9 changes: 9 additions & 0 deletions src/components/dashboard/MarketDataCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export class MarketDataCache {
//
// Public
//
public static readonly instance = new MarketDataCache()

public readonly hbarPriceCache = new HbarPriceLoader()
public readonly hbarPrice24hCache = new HbarPriceLoader(86400)
public readonly hbarSupplyCache = new HbarSupplyLoader()
Expand Down Expand Up @@ -91,4 +93,11 @@ export class MarketDataCache {
this.hbarSupplyCache.mounted.value = false
this.hbarSupply24hCache.mounted.value = false
}

public clear(): void {
this.hbarPriceCache.requestLoad()
this.hbarPrice24hCache.requestLoad()
this.hbarSupplyCache.requestLoad()
this.hbarSupply24hCache.requestLoad()
}
}
2 changes: 2 additions & 0 deletions src/utils/cache/CacheUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {ContractResultByTransactionIdCache} from "@/utils/cache/ContractResultBy
import {ContractResultByTsCache} from "@/utils/cache/ContractResultByTsCache";
import {SourcifyCache} from "@/utils/cache/SourcifyCache";
import {TokenAssociationCache} from "@/utils/cache/TokenAssociationCache";
import {MarketDataCache} from "@/components/dashboard/MarketDataCache";

export class CacheUtils {

Expand All @@ -62,6 +63,7 @@ export class CacheUtils {
ContractResultByHashCache.instance.clear()
ContractResultByTransactionIdCache.instance.clear()
ContractResultByTsCache.instance.clear()
MarketDataCache.instance.clear()
HbarPriceCache.instance.clear()
// IPFSCache.instance => no clear: we preserve it because IPFS content is valid for all networks
NetworkCache.instance.clear()
Expand Down

0 comments on commit c013a27

Please sign in to comment.