feat: cache settings so only retreived when clean

This commit is contained in:
emizzle 2020-06-26 17:29:57 +10:00 committed by Iuri Matias
parent 88e5f1a46d
commit 8e6eb65509
2 changed files with 20 additions and 11 deletions

View File

@ -31,8 +31,8 @@ proc init*(self: ProfileController, account: Account) =
# Ideally, this module should call getSettings once, and fill the
# profile with all the information comming from the settings.
let response = status_settings.getSettings()
let pubKey = response["public-key"].getStr
let mnemonic = response["mnemonic"].getStr
let pubKey = status_settings.getSetting[string]("public-key", "0x0")
let mnemonic = status_settings.getSetting[string]("mnemonic", "")
profile.id = pubKey
self.view.setNewProfile(profile)

View File

@ -1,22 +1,31 @@
import core, ./types
import core, ./types, ../../signals/types as statusgo_types
import json, tables
import json_serialization
proc saveSettings*(key: string, value: string | JsonNode): string =
callPrivateRPC("settings_saveSetting", %* [
var settings: JsonNode = %*{}
var dirty: bool = false
proc saveSettings*(key: string, value: string | JsonNode): StatusGoError =
let response = callPrivateRPC("settings_saveSetting", %* [
key, value
])
try:
result = Json.decode($response, StatusGoError)
except:
dirty = true
proc getWeb3ClientVersion*(): string =
parseJson(callPrivateRPC("web3_clientVersion"))["result"].getStr
proc getSettings*(): JsonNode =
callPrivateRPC("settings_getSettings").parseJSON()["result"]
# TODO: return an Table/Object instead
proc getSettings*(useCached: bool = true): JsonNode =
if useCached and not dirty:
return settings
settings = callPrivateRPC("settings_getSettings").parseJSON()["result"]
dirty = false
result = settings
proc getSetting*[T](name: string, defaultValue: T): T =
let settings: JsonNode = getSettings()
proc getSetting*[T](name: string, defaultValue: T, useCached: bool = true): T =
let settings: JsonNode = getSettings(useCached)
if not settings.contains(name):
return defaultValue
result = Json.decode($settings{name}, T)