diff --git a/lib/cache.rb b/lib/cache.rb index ca2c46a..7bd1bda 100644 --- a/lib/cache.rb +++ b/lib/cache.rb @@ -7,16 +7,19 @@ class W3DHub end # Fetch a generic uri - def self.fetch(uri, force_fetch = false) + def self.fetch(uri:, force_fetch: false, async: true) path = path(uri) if !force_fetch && File.exist?(path) path - else + elsif async BackgroundWorker.job( -> { Async::HTTP::Internet.instance.get(uri, W3DHub::Api::DEFAULT_HEADERS) }, ->(response) { response.save(path, "wb") if response.success? } ) + else + response = Async::HTTP::Internet.instance.get(uri, W3DHub::Api::DEFAULT_HEADERS) + response.save(path, "wb") if response.success? end end diff --git a/lib/pages/community.rb b/lib/pages/community.rb index 97a1f99..d0b160b 100644 --- a/lib/pages/community.rb +++ b/lib/pages/community.rb @@ -70,7 +70,7 @@ class W3DHub return unless news news.items[0..9].each do |item| - Cache.fetch(item.image) + Cache.fetch(uri: item.image, async: false) end @w3dhub_news = news diff --git a/lib/pages/games.rb b/lib/pages/games.rb index b7f6880..7cf9c11 100644 --- a/lib/pages/games.rb +++ b/lib/pages/games.rb @@ -181,7 +181,7 @@ class W3DHub return unless news news.items[0..9].each do |item| - Cache.fetch(item.image) + Cache.fetch(uri: item.image, async: false) end @game_news[game.id] = news diff --git a/lib/pages/login.rb b/lib/pages/login.rb index 2a6edc0..417cdf5 100644 --- a/lib/pages/login.rb +++ b/lib/pages/login.rb @@ -46,7 +46,7 @@ class W3DHub Store.settings[:account][:data] = account Store.settings.save_settings - Cache.fetch(account.avatar_uri, true) if account + Cache.fetch(account.avatar_uri, force_fetch: true, async: false) if account applications = Api.applications if account end @@ -81,7 +81,7 @@ class W3DHub if Store.account BackgroundWorker.foreground_job( - -> { Cache.fetch(Store.account.avatar_uri) }, + -> { Cache.fetch(uri: Store.account.avatar_uri, async: false) }, ->(result) { populate_account_info page(W3DHub::Pages::Games) diff --git a/lib/states/boot.rb b/lib/states/boot.rb index b8301a1..889c249 100644 --- a/lib/states/boot.rb +++ b/lib/states/boot.rb @@ -71,7 +71,7 @@ class W3DHub end else - update_account_data(account) + BackgroundWorker.foreground_job(-> { update_account_data(account) }, ->(_) {}) end else @@ -85,7 +85,7 @@ class W3DHub Store.settings[:account][:data] = account - Cache.fetch(account.avatar_uri, true) + Cache.fetch(uri: account.avatar_uri, force_fetch: true, async: false) else Store.settings[:account] = {} end