1+ proc getTokensMarketValuesLoading * (self: Service ): bool =
2+ return self.tokensPricesLoading or self.tokensMarketDetailsLoading
3+
4+ # resolveTokensMarketValuesLoadingStateAndNotify ensures that only a single signal is emitted when:
5+ # - either tokens prices or tokens market details are about to be updated
6+ # - tokens prices and tokens market details are updated (when both are loaded)
7+ proc resolveTokensMarketValuesLoadingStateAndNotify (self: Service , tokensPricesLoading: bool , tokensMarketDetailsLoading: bool ) =
8+ if not self.getTokensMarketValuesLoading ():
9+ self.tokensPricesLoading = tokensPricesLoading
10+ self.tokensMarketDetailsLoading = tokensMarketDetailsLoading
11+ if self.getTokensMarketValuesLoading ():
12+ self.events.emit (SIGNAL_TOKENS_MARKET_VALUES_ABOUT_TO_BE_UPDATED , Args ())
13+ return
14+ self.tokensPricesLoading = tokensPricesLoading
15+ self.tokensMarketDetailsLoading = tokensMarketDetailsLoading
16+ if not self.getTokensMarketValuesLoading ():
17+ self.events.emit (SIGNAL_TOKENS_MARKET_VALUES_UPDATED , Args ())
18+
19+ proc setTokensMarketDetailsLoadingStateAndNotify (self: Service , state: bool ) =
20+ self.resolveTokensMarketValuesLoadingStateAndNotify (self.tokensPricesLoading, state)
21+
22+ proc setTokensPricesLoadingStateAndNotify (self: Service , state: bool ) =
23+ self.resolveTokensMarketValuesLoadingStateAndNotify (state, self.tokensMarketDetailsLoading)
24+
25+ proc updateTokenPrices * (self: Service , updatedPrices: Table [string , float64 ]) =
26+ var anyUpdated = false
27+ for tokenKey, price in updatedPrices:
28+ if not self.tokenPriceTable.hasKey (tokenKey) or self.tokenPriceTable[tokenKey] != price:
29+ anyUpdated = true
30+ self.tokenPriceTable[tokenKey] = price
31+ if anyUpdated:
32+ self.events.emit (SIGNAL_TOKENS_MARKET_VALUES_UPDATED , Args ())
33+
134# if tokensKeys is empty, market values for all tokens will be fetched
235proc fetchTokensMarketValues (self: Service , tokensKeys: seq [string ] = @ []) =
3- self.tokensMarketDetailsLoading = true
4- defer : self.events.emit (SIGNAL_TOKENS_MARKET_VALUES_ABOUT_TO_BE_UPDATED , Args ())
36+ defer : self.setTokensMarketDetailsLoadingStateAndNotify (true )
537 let arg = FetchTokensMarketValuesTaskArg (
638 tptr: fetchTokensMarketValuesTask,
739 vptr: cast [uint ](self.vptr),
@@ -13,8 +45,7 @@ proc fetchTokensMarketValues(self: Service, tokensKeys: seq[string] = @[]) =
1345
1446proc tokensMarketValuesRetrieved (self: Service , response: string ) {.slot .} =
1547 # this is emited so that the models can notify about market values being available
16- self.tokensMarketDetailsLoading = false
17- defer : self.events.emit (SIGNAL_TOKENS_MARKET_VALUES_UPDATED , Args ())
48+ defer : self.setTokensMarketDetailsLoadingStateAndNotify (false )
1849 try :
1950 let parsedJson = response.parseJson
2051 var errorString: string
@@ -82,8 +113,7 @@ proc tokensDetailsRetrieved(self: Service, response: string) {.slot.} =
82113
83114# if tokensKeys is empty, prices for all tokens will be fetched
84115proc fetchTokensPrices (self: Service , tokensKeys: seq [string ] = @ []) =
85- self.tokensPricesLoading = true
86- defer : self.events.emit (SIGNAL_TOKENS_PRICES_ABOUT_TO_BE_UPDATED , Args ())
116+ defer : self.setTokensPricesLoadingStateAndNotify (true )
87117 let arg = FetchTokensPricesTaskArg (
88118 tptr: fetchTokensPricesTask,
89119 vptr: cast [uint ](self.vptr),
@@ -94,9 +124,8 @@ proc fetchTokensPrices(self: Service, tokensKeys: seq[string] = @[]) =
94124 self.threadpool.start (arg)
95125
96126proc tokensPricesRetrieved (self: Service , response: string ) {.slot .} =
97- self.tokensPricesLoading = false
98127 # this is emited so that the models can notify about prices being available
99- defer : self.events. emit ( SIGNAL_TOKENS_PRICES_UPDATED , Args () )
128+ defer : self.setTokensPricesLoadingStateAndNotify ( false )
100129 try :
101130 let parsedJson = response.parseJson
102131 var errorString: string
0 commit comments