Fixed race condition where images for user avatar or news icons were not blocking execution until after they were downloaded causing a possible crash due to trying to save and load at the same time (sometimes also causing images to be only partly saved making them corrupt causing another crash)

This commit is contained in:
2022-03-20 13:51:42 -05:00
parent 76ca7e369f
commit 8766ed7d86
5 changed files with 11 additions and 8 deletions

View File

@@ -7,16 +7,19 @@ class W3DHub
end end
# Fetch a generic uri # Fetch a generic uri
def self.fetch(uri, force_fetch = false) def self.fetch(uri:, force_fetch: false, async: true)
path = path(uri) path = path(uri)
if !force_fetch && File.exist?(path) if !force_fetch && File.exist?(path)
path path
else elsif async
BackgroundWorker.job( BackgroundWorker.job(
-> { Async::HTTP::Internet.instance.get(uri, W3DHub::Api::DEFAULT_HEADERS) }, -> { Async::HTTP::Internet.instance.get(uri, W3DHub::Api::DEFAULT_HEADERS) },
->(response) { response.save(path, "wb") if response.success? } ->(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
end end

View File

@@ -70,7 +70,7 @@ class W3DHub
return unless news return unless news
news.items[0..9].each do |item| news.items[0..9].each do |item|
Cache.fetch(item.image) Cache.fetch(uri: item.image, async: false)
end end
@w3dhub_news = news @w3dhub_news = news

View File

@@ -181,7 +181,7 @@ class W3DHub
return unless news return unless news
news.items[0..9].each do |item| news.items[0..9].each do |item|
Cache.fetch(item.image) Cache.fetch(uri: item.image, async: false)
end end
@game_news[game.id] = news @game_news[game.id] = news

View File

@@ -46,7 +46,7 @@ class W3DHub
Store.settings[:account][:data] = account Store.settings[:account][:data] = account
Store.settings.save_settings 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 applications = Api.applications if account
end end
@@ -81,7 +81,7 @@ class W3DHub
if Store.account if Store.account
BackgroundWorker.foreground_job( BackgroundWorker.foreground_job(
-> { Cache.fetch(Store.account.avatar_uri) }, -> { Cache.fetch(uri: Store.account.avatar_uri, async: false) },
->(result) { ->(result) {
populate_account_info populate_account_info
page(W3DHub::Pages::Games) page(W3DHub::Pages::Games)

View File

@@ -71,7 +71,7 @@ class W3DHub
end end
else else
update_account_data(account) BackgroundWorker.foreground_job(-> { update_account_data(account) }, ->(_) {})
end end
else else
@@ -85,7 +85,7 @@ class W3DHub
Store.settings[:account][:data] = account Store.settings[:account][:data] = account
Cache.fetch(account.avatar_uri, true) Cache.fetch(uri: account.avatar_uri, force_fetch: true, async: false)
else else
Store.settings[:account] = {} Store.settings[:account] = {}
end end