feat(@desktop/wallet): implement partial collectibles updates
Fixes #12150
This commit is contained in:
parent
db436b4e43
commit
52e6fddcab
|
@ -1,5 +1,5 @@
|
||||||
import NimQml, std/json, sequtils, sugar, strutils
|
import NimQml, std/json, sequtils, sugar, strutils
|
||||||
import stint, logging
|
import stint, logging, Tables
|
||||||
|
|
||||||
import app/modules/shared_models/collectibles_model
|
import app/modules/shared_models/collectibles_model
|
||||||
import app/modules/shared_models/collectibles_utils
|
import app/modules/shared_models/collectibles_utils
|
||||||
|
@ -22,6 +22,8 @@ QtObject:
|
||||||
addresses: seq[string]
|
addresses: seq[string]
|
||||||
chainIds: seq[int]
|
chainIds: seq[int]
|
||||||
|
|
||||||
|
stateTable: Table[string, Table[int, bool]] # Table[address][chainID] -> isUpdating
|
||||||
|
|
||||||
requestId: int32
|
requestId: int32
|
||||||
autofetch: bool
|
autofetch: bool
|
||||||
|
|
||||||
|
@ -40,7 +42,6 @@ QtObject:
|
||||||
QtProperty[QVariant] model:
|
QtProperty[QVariant] model:
|
||||||
read = getModelAsVariant
|
read = getModelAsVariant
|
||||||
|
|
||||||
|
|
||||||
proc loadMoreItems(self: Controller) {.slot.} =
|
proc loadMoreItems(self: Controller) {.slot.} =
|
||||||
if self.model.getIsFetching():
|
if self.model.getIsFetching():
|
||||||
return
|
return
|
||||||
|
@ -87,17 +88,47 @@ QtObject:
|
||||||
if self.autofetch:
|
if self.autofetch:
|
||||||
self.loadMoreItems()
|
self.loadMoreItems()
|
||||||
|
|
||||||
|
proc resetUpdateState*(self: Controller) =
|
||||||
|
# Initialize state table
|
||||||
|
# We assume that ownership is initially not being updated. This will change if an
|
||||||
|
# update starts or a partial update is received.
|
||||||
|
# TODO: Get the update state at the time of filter switch from the backend?
|
||||||
|
self.stateTable = initTable[string, Table[int, bool]]()
|
||||||
|
for address in self.addresses:
|
||||||
|
self.stateTable[address] = initTable[int, bool]()
|
||||||
|
for chainID in self.chainIds:
|
||||||
|
self.stateTable[address][chainID] = false
|
||||||
|
self.model.setIsUpdating(false)
|
||||||
|
|
||||||
|
proc setUpdateState*(self: Controller, address: string, chainID: int, isUpdating: bool) =
|
||||||
|
if not self.stateTable.hasKey(address) or not self.stateTable[address].hasKey(chainID):
|
||||||
|
return
|
||||||
|
self.stateTable[address][chainID] = isUpdating
|
||||||
|
|
||||||
|
# If any address+chainID is updating, then the whole model is updating
|
||||||
|
for address, chainIDsPerAddress in self.stateTable.pairs:
|
||||||
|
for chainID, isUpdating in chainIDsPerAddress:
|
||||||
|
if isUpdating:
|
||||||
|
self.model.setIsUpdating(true)
|
||||||
|
return
|
||||||
|
self.model.setIsUpdating(false)
|
||||||
|
|
||||||
proc setupEventHandlers(self: Controller) =
|
proc setupEventHandlers(self: Controller) =
|
||||||
self.eventsHandler.onOwnedCollectiblesFilteringDone(proc (jsonObj: JsonNode) =
|
self.eventsHandler.onOwnedCollectiblesFilteringDone(proc (jsonObj: JsonNode) =
|
||||||
self.processFilterOwnedCollectiblesResponse(jsonObj)
|
self.processFilterOwnedCollectiblesResponse(jsonObj)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.eventsHandler.onCollectiblesOwnershipUpdateStarted(proc () =
|
self.eventsHandler.onCollectiblesOwnershipUpdateStarted(proc (address: string, chainID: int) =
|
||||||
self.model.setIsUpdating(true)
|
self.setUpdateState(address, chainID, true)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.eventsHandler.onCollectiblesOwnershipUpdateFinished(proc () =
|
self.eventsHandler.onCollectiblesOwnershipUpdatePartial(proc (address: string, chainID: int) =
|
||||||
self.model.setIsUpdating(false)
|
self.setUpdateState(address, chainID, true)
|
||||||
|
self.resetModel()
|
||||||
|
)
|
||||||
|
|
||||||
|
self.eventsHandler.onCollectiblesOwnershipUpdateFinished(proc (address: string, chainID: int) =
|
||||||
|
self.setUpdateState(address, chainID, false)
|
||||||
self.resetModel()
|
self.resetModel()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -127,6 +158,8 @@ QtObject:
|
||||||
|
|
||||||
self.chainIds = chainIds
|
self.chainIds = chainIds
|
||||||
self.addresses = addresses
|
self.addresses = addresses
|
||||||
|
|
||||||
|
self.resetUpdateState()
|
||||||
|
|
||||||
self.eventsHandler.updateSubscribedAddresses(self.addresses)
|
self.eventsHandler.updateSubscribedAddresses(self.addresses)
|
||||||
self.eventsHandler.updateSubscribedChainIDs(self.chainIds)
|
self.eventsHandler.updateSubscribedChainIDs(self.chainIds)
|
||||||
|
|
|
@ -8,6 +8,7 @@ import backend/collectibles as backend_collectibles
|
||||||
|
|
||||||
type EventCallbackProc = proc (eventObject: JsonNode)
|
type EventCallbackProc = proc (eventObject: JsonNode)
|
||||||
type WalletEventCallbackProc = proc (data: WalletSignal)
|
type WalletEventCallbackProc = proc (data: WalletSignal)
|
||||||
|
type OwnershipUpdateCallbackProc = proc (address: string, chainID: int)
|
||||||
|
|
||||||
# EventsHandler responsible for catching collectibles related backend events and reporting them
|
# EventsHandler responsible for catching collectibles related backend events and reporting them
|
||||||
QtObject:
|
QtObject:
|
||||||
|
@ -20,8 +21,9 @@ QtObject:
|
||||||
subscribedAddresses: HashSet[string]
|
subscribedAddresses: HashSet[string]
|
||||||
subscribedChainIDs: HashSet[int]
|
subscribedChainIDs: HashSet[int]
|
||||||
|
|
||||||
collectiblesOwnershipUpdateStartedFn: proc()
|
collectiblesOwnershipUpdateStartedFn: OwnershipUpdateCallbackProc
|
||||||
collectiblesOwnershipUpdateFinishedFn: proc()
|
collectiblesOwnershipUpdatePartialFn: OwnershipUpdateCallbackProc
|
||||||
|
collectiblesOwnershipUpdateFinishedFn: OwnershipUpdateCallbackProc
|
||||||
|
|
||||||
requestId: int32
|
requestId: int32
|
||||||
|
|
||||||
|
@ -34,10 +36,13 @@ QtObject:
|
||||||
proc onOwnedCollectiblesFilteringDone*(self: EventsHandler, handler: EventCallbackProc) =
|
proc onOwnedCollectiblesFilteringDone*(self: EventsHandler, handler: EventCallbackProc) =
|
||||||
self.eventHandlers[backend_collectibles.eventOwnedCollectiblesFilteringDone] = handler
|
self.eventHandlers[backend_collectibles.eventOwnedCollectiblesFilteringDone] = handler
|
||||||
|
|
||||||
proc onCollectiblesOwnershipUpdateStarted*(self: EventsHandler, handler: proc()) =
|
proc onCollectiblesOwnershipUpdateStarted*(self: EventsHandler, handler: OwnershipUpdateCallbackProc) =
|
||||||
self.collectiblesOwnershipUpdateStartedFn = handler
|
self.collectiblesOwnershipUpdateStartedFn = handler
|
||||||
|
|
||||||
proc onCollectiblesOwnershipUpdateFinished*(self: EventsHandler, handler: proc()) =
|
proc onCollectiblesOwnershipUpdatePartial*(self: EventsHandler, handler: OwnershipUpdateCallbackProc) =
|
||||||
|
self.collectiblesOwnershipUpdatePartialFn = handler
|
||||||
|
|
||||||
|
proc onCollectiblesOwnershipUpdateFinished*(self: EventsHandler, handler: OwnershipUpdateCallbackProc) =
|
||||||
self.collectiblesOwnershipUpdateFinishedFn = handler
|
self.collectiblesOwnershipUpdateFinishedFn = handler
|
||||||
|
|
||||||
proc handleApiEvents(self: EventsHandler, e: Args) =
|
proc handleApiEvents(self: EventsHandler, e: Args) =
|
||||||
|
@ -72,12 +77,17 @@ QtObject:
|
||||||
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdateStarted] = proc (data: WalletSignal) =
|
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdateStarted] = proc (data: WalletSignal) =
|
||||||
if self.collectiblesOwnershipUpdateStartedFn == nil or self.shouldIgnoreEvent(data):
|
if self.collectiblesOwnershipUpdateStartedFn == nil or self.shouldIgnoreEvent(data):
|
||||||
return
|
return
|
||||||
self.collectiblesOwnershipUpdateStartedFn()
|
self.collectiblesOwnershipUpdateStartedFn(data.accounts[0], data.chainID)
|
||||||
|
|
||||||
|
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdatePartial] = proc (data: WalletSignal) =
|
||||||
|
if self.collectiblesOwnershipUpdatePartialFn == nil or self.shouldIgnoreEvent(data):
|
||||||
|
return
|
||||||
|
self.collectiblesOwnershipUpdatePartialFn(data.accounts[0], data.chainID)
|
||||||
|
|
||||||
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdateFinished] = proc (data: WalletSignal) =
|
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdateFinished] = proc (data: WalletSignal) =
|
||||||
if self.collectiblesOwnershipUpdateFinishedFn == nil or self.shouldIgnoreEvent(data):
|
if self.collectiblesOwnershipUpdateFinishedFn == nil or self.shouldIgnoreEvent(data):
|
||||||
return
|
return
|
||||||
self.collectiblesOwnershipUpdateFinishedFn()
|
self.collectiblesOwnershipUpdateFinishedFn(data.accounts[0], data.chainID)
|
||||||
|
|
||||||
proc newEventsHandler*(requestId: int32, events: EventEmitter): EventsHandler =
|
proc newEventsHandler*(requestId: int32, events: EventEmitter): EventsHandler =
|
||||||
new(result, delete)
|
new(result, delete)
|
||||||
|
|
|
@ -16,6 +16,7 @@ type
|
||||||
|
|
||||||
# Declared in services/wallet/collectibles/service.go
|
# Declared in services/wallet/collectibles/service.go
|
||||||
const eventCollectiblesOwnershipUpdateStarted*: string = "wallet-collectibles-ownership-update-started"
|
const eventCollectiblesOwnershipUpdateStarted*: string = "wallet-collectibles-ownership-update-started"
|
||||||
|
const eventCollectiblesOwnershipUpdatePartial*: string = "wallet-collectibles-ownership-update-partial"
|
||||||
const eventCollectiblesOwnershipUpdateFinished*: string = "wallet-collectibles-ownership-update-finished"
|
const eventCollectiblesOwnershipUpdateFinished*: string = "wallet-collectibles-ownership-update-finished"
|
||||||
const eventCollectiblesOwnershipUpdateFinishedWithError*: string = "wallet-collectibles-ownership-update-finished-with-error"
|
const eventCollectiblesOwnershipUpdateFinishedWithError*: string = "wallet-collectibles-ownership-update-finished-with-error"
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 5e8300d6a12fe22a97c2219dac17888499003b8f
|
Subproject commit e337ab4f13e196d2c83d9f99aef68b5b79066695
|
Loading…
Reference in New Issue