refactor(browser): make dapp permissions lazy load

Fixes #9826
This commit is contained in:
Jonathan Rainville 2023-03-10 15:36:27 -05:00
parent 7cbe7332b8
commit 1f3aef3a0b
7 changed files with 44 additions and 3 deletions

View File

@ -10,6 +10,12 @@ method load*(self: AccessInterface) {.base.} =
method isLoaded*(self: AccessInterface): bool {.base.} = method isLoaded*(self: AccessInterface): bool {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method onActivated*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method loadDapps*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method hasPermission*(self: AccessInterface, hostname: string, address: string, permission: string): bool = method hasPermission*(self: AccessInterface, hostname: string, address: string, permission: string): bool =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")

View File

@ -18,6 +18,7 @@ type
view: View view: View
viewVariant: QVariant viewVariant: QVariant
moduleLoaded: bool moduleLoaded: bool
dappsLoaded: bool
controller: Controller controller: Controller
proc newModule*( proc newModule*(
@ -30,6 +31,7 @@ proc newModule*(
result.view = view.newView(result) result.view = view.newView(result)
result.viewVariant = newQVariant(result.view) result.viewVariant = newQVariant(result.view)
result.moduleLoaded = false result.moduleLoaded = false
result.dappsLoaded = false
result.controller = controller.newController(result, dappPermissionsService, walletAccountServive) result.controller = controller.newController(result, dappPermissionsService, walletAccountServive)
method delete*(self: Module) = method delete*(self: Module) =
@ -74,10 +76,21 @@ method isLoaded*(self: Module): bool =
return self.moduleLoaded return self.moduleLoaded
method viewDidLoad*(self: Module) = method viewDidLoad*(self: Module) =
self.fetchDapps()
self.moduleLoaded = true self.moduleLoaded = true
self.delegate.dappsDidLoad() self.delegate.dappsDidLoad()
method loadDapps*(self: Module) =
if self.dappsLoaded:
return
self.fetchDapps()
self.dappsLoaded = true
method onActivated*(self: Module) =
self.loadDapps()
method hasPermission*(self: Module, hostname: string, address: string, permission: string): bool = method hasPermission*(self: Module, hostname: string, address: string, permission: string): bool =
self.controller.hasPermission(hostname, address, permission.toPermission()) self.controller.hasPermission(hostname, address, permission.toPermission())

View File

@ -40,6 +40,9 @@ QtObject:
read = getDappsModel read = getDappsModel
notify = modelChanged notify = modelChanged
proc loadDapps(self: View) {.slot.} =
self.delegate.loadDapps()
proc hasPermission(self: View, hostname: string, address: string, permission: string): bool {.slot.} = proc hasPermission(self: View, hostname: string, address: string, permission: string): bool {.slot.} =
return self.delegate.hasPermission(hostname, address, permission) return self.delegate.hasPermission(hostname, address, permission)

View File

@ -71,6 +71,7 @@ method load*(self: Module) =
method onActivated*(self: Module) = method onActivated*(self: Module) =
self.bookmarkModule.onActivated() self.bookmarkModule.onActivated()
self.dappsModule.onActivated()
method isLoaded*(self: Module): bool = method isLoaded*(self: Module): bool =
return self.moduleLoaded return self.moduleLoaded

View File

@ -16,6 +16,7 @@ logScope:
type type
Service* = ref object Service* = ref object
dapps: Table[string, Dapp] dapps: Table[string, Dapp]
permissionsFetched: bool
type R = Result[Dapp, string] type R = Result[Dapp, string]
@ -25,8 +26,13 @@ proc delete*(self: Service) =
proc newService*(): Service = proc newService*(): Service =
result = Service() result = Service()
result.dapps = initTable[string, Dapp]() result.dapps = initTable[string, Dapp]()
result.permissionsFetched = false
proc init*(self: Service) = proc init*(self: Service) =
discard
proc fetchDappPermissions*(self: Service) =
# TODO later we can make this async, but it's not worth it for now
try: try:
let response = backend.getDappPermissions() let response = backend.getDappPermissions()
for dapp in response.result.getElems().mapIt(it.toDapp()): for dapp in response.result.getElems().mapIt(it.toDapp()):
@ -34,11 +40,13 @@ proc init*(self: Service) =
continue continue
self.dapps[dapp.name & dapp.address] = dapp self.dapps[dapp.name & dapp.address] = dapp
self.permissionsFetched = true
except Exception as e: except Exception as e:
let errDescription = e.msg error "error fetching permissions: ", msg=e.msg
error "error: ", errDescription
proc getDapps*(self: Service): seq[Dapp] = proc getDapps*(self: Service): seq[Dapp] =
if not self.permissionsFetched:
self.fetchDappPermissions()
return toSeq(self.dapps.values) return toSeq(self.dapps.values)
proc getDapp*(self: Service, name: string, address: string): Option[Dapp] = proc getDapp*(self: Service, name: string, address: string): Option[Dapp] =

View File

@ -49,4 +49,8 @@ QtObject {
function disconnectAddress(dappName, address) { function disconnectAddress(dappName, address) {
return dappPermissionsModule.disconnectAddress(dappName, address) return dappPermissionsModule.disconnectAddress(dappName, address)
} }
function loadDapps() {
dappPermissionsModule.loadDapps()
}
} }

View File

@ -20,6 +20,12 @@ Column {
signal goToAccountView(address: string) signal goToAccountView(address: string)
signal goToDappPermissionsView() signal goToDappPermissionsView()
Component.onCompleted: {
// TODO remove this call and handle it from the backend
// once the profile is refactored and the navigation is driven from the backend
root.walletStore.loadDapps()
}
Separator { Separator {
height: 17 height: 17
} }