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
# 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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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