Progress towards background work replacement, work getting new NetworkManager and HttpClient up and running

This commit is contained in:
2026-02-01 15:12:24 -06:00
parent ebc045019a
commit 3e4b25f0d4
9 changed files with 273 additions and 358 deletions

View File

@@ -65,15 +65,7 @@ class W3DHub
para I18n.t(:"games.fetching_news"), padding: 8
end
BackgroundWorker.foreground_job(
-> { fetch_w3dhub_news },
lambda do |result|
if result
populate_w3dhub_news
Cache.release_net_lock(result)
end
end
)
fetch_w3dhub_news
end
end
@@ -89,15 +81,7 @@ class W3DHub
title I18n.t(:"games.fetching_news"), padding: 8
end
BackgroundWorker.foreground_job(
-> { fetch_w3dhub_news },
lambda do |result|
if result
populate_w3dhub_news
Cache.release_net_lock(result)
end
end
)
fetch_w3dhub_news
end
end
@@ -105,19 +89,24 @@ class W3DHub
lock = Cache.acquire_net_lock("w3dhub_news")
return false unless lock
news = Api.news("launcher-home")
Cache.release_net_lock("w3dhub_news") unless news
Api.news("launcher-home") do |result|
news = result.data
return unless news
Cache.release_net_lock("w3dhub_news") unless news
news.items[0..15].each do |item|
Cache.fetch(uri: item.image, async: false, backend: :w3dhub)
next false unless news
news.items[0..15].each do |item|
Cache.fetch(uri: item.image, backend: :w3dhub)
end
@w3dhub_news = news
@w3dhub_news_expires = Gosu.milliseconds + (60 * 60 * 1000) # 1 hour (in ms)
populate_w3dhub_news
ensure
Cache.release_net_lock("w3dhub_news")
end
@w3dhub_news = news
@w3dhub_news_expires = Gosu.milliseconds + (60 * 60 * 1000) # 1 hour (in ms)
"w3dhub_news"
end
def populate_w3dhub_news
@@ -125,37 +114,7 @@ class W3DHub
if (feed = @w3dhub_news)
@wd3hub_news_container.clear do
# feed.items.sort_by { |i| i.timestamp }.reverse[0..9].each do |item|
# flow(width: 0.5, max_width: 312, height: 128, margin: 4) do
# # background 0x88_000000
# path = Cache.path(item.image)
# if File.exist?(path)
# image path, height: 1.0, padding: 4
# else
# image BLACK_IMAGE, height: 1.0, padding: 4
# end
# stack(width: 0.6, height: 1.0) do
# stack(width: 1.0, height: 112) do
# link "<b>#{item.title}</b>", text_size: 22 do
# W3DHub.url(item.uri)
# end
# para item.blurb.gsub(/\n+/, "\n").strip[0..180]
# end
# flow(width: 1.0) do
# para item.timestamp.strftime("%Y-%m-%d"), width: 0.499
# link I18n.t(:"games.read_more"), width: 0.5, text_align: :right, text_size: 22 do
# W3DHub.url(item.uri)
# end
# end
# end
# end
# end
feed.items.sort_by { |i| i.timestamp }.reverse[0..9].each do |item|
feed.items.sort_by(&:timestamp).reverse[0..9].each do |item|
image_path = Cache.path(item.image)
flow(width: 1.0, max_width: 1230, height: 200, margin: 8, border_thickness: 1, border_color: lighten(Gosu::Color.new(0xff_252525))) do

View File

@@ -31,30 +31,16 @@ class W3DHub
def update
super
@game_news.each do |key, value|
next if key.end_with?("_expires")
next unless key.end_with?("_expires")
if Gosu.milliseconds >= @game_news["#{key}_expires"]
@game_news.delete(key)
@game_news["#{key}_expires"] = Gosu.milliseconds + 30_000 # seconds
next unless Gosu.milliseconds >= value
if @focused_game && @focused_game.id == key
@game_news_container.clear do
title I18n.t(:"games.fetching_news"), padding: 8
end
# try to refresh game news after last data 'expired', every 30 seconds until success
@game_news[key] = Gosu.milliseconds + 30_000 # seconds
BackgroundWorker.foreground_job(
-> { fetch_game_news(@focused_game) },
lambda do |result|
if result
populate_game_news(@focused_game)
Cache.release_net_lock(result)
end
end
)
end
end
game = Store.applications.games.find { |g| g.id == key.split("_").first }
fetch_game_news(game)
end
end
@@ -292,22 +278,6 @@ class W3DHub
return if Store.offline_mode
unless Cache.net_lock?("game_news_#{game.id}")
if @game_events[game.id]
populate_game_events(game)
else
# BackgroundWorker.foreground_job(
# -> { fetch_game_events(game) },
# lambda do |result|
# if result
# populate_game_events(game)
# Cache.release_net_lock(result)
# end
# end
# )
end
end
unless Cache.net_lock?("game_events_#{game.id}")
if @game_news[game.id]
populate_game_news(game)
else
@@ -315,15 +285,15 @@ class W3DHub
title I18n.t(:"games.fetching_news"), padding: 8
end
# BackgroundWorker.foreground_job(
# -> { fetch_game_news(game) },
# lambda do |result|
# if result
# populate_game_news(game)
# Cache.release_net_lock(result)
# end
# end
# )
fetch_game_news(game)
end
end
unless Cache.net_lock?("game_events_#{game.id}")
if @game_events[game.id]
populate_game_events(game)
else
fetch_game_events(game)
end
end
end
@@ -413,19 +383,25 @@ class W3DHub
lock = Cache.acquire_net_lock("game_news_#{game.id}")
return false unless lock
news = Api.news(game.id)
Cache.release_net_lock("game_news_#{game.id}") unless news
Api.news(game.id) do |result|
news = result.data
return false unless news
unless news
@game_news["#{game.id}_expires"] = Gosu.milliseconds + 30_000 # retry in 30 seconds
next false
end
news.items[0..15].each do |item|
Cache.fetch(uri: item.image, async: false, backend: :w3dhub)
news.items[0..15].each do |item|
Cache.fetch(uri: item.image, backend: :w3dhub)
end
@game_news[game.id] = news
@game_news["#{game.id}_expires"] = Gosu.milliseconds + (60 * 60 * 1000) # 1 hour (in ms)
populate_game_news(@focused_game)
ensure
Cache.release_net_lock("game_news_#{game.id}")
end
@game_news[game.id] = news
@game_news["#{game.id}_expires"] = Gosu.milliseconds + (60 * 60 * 1000) # 1 hour (in ms)
"game_news_#{game.id}"
end
def populate_game_news(game)
@@ -436,38 +412,11 @@ class W3DHub
game_color.alpha = 0xaa
@game_news_container.clear do
# Patch Notes
if false # Patch notes
flow(width: 1.0, max_width: 346 * 3 + (8 * 4), height: 346, margin: 8, margin_right: 32, border_thickness: 1, border_color: darken(Gosu::Color.new(game.color))) do
background darken(Gosu::Color.new(game.color), 10)
stack(width: 346, height: 1.0, padding: 8) do
background 0xff_181d22
para "Patch Notes"
tagline "<b>Patch 2.0 is now out!</b>"
para "words go here " * 20
flow(fill: true)
button "Read More", width: 1.0
end
flow(fill: true)
title "Eye Candy Banner Goes Here."
end
end
feed.items.sort_by { |i| i.timestamp }.reverse[0..9].each do |item|
feed.items.sort_by(&:timestamp).reverse[0..9].each do |item|
image_path = Cache.path(item.image)
flow(width: 1.0, max_width: 869, height: 200, margin: 8, background: game_color, border_thickness: 1, border_color: lighten(Gosu::Color.new(game.color))) do
if File.file?(image_path)
image image_path, height: 1.0
end
image image_path, height: 1.0 if File.file?(image_path)
stack(fill: true, height: 1.0, padding: 4, border_thickness_left: 1, border_color_left: lighten(Gosu::Color.new(game.color))) do
tagline "<b>#{item.title}</b>", width: 1.0
@@ -494,14 +443,14 @@ class W3DHub
lock = Cache.acquire_net_lock("game_events_#{game.id}")
return false unless lock
events = Api.events(game.id)
Cache.release_net_lock("game_events_#{game.id}") unless events
Api.events(game.id) do |result|
next unless result.okay?
return false unless events
@game_events[game.id] = events
"game_events_#{game.id}"
@game_events[game.id] = result.data
populate_game_events(game)
ensure
Cache.release_net_lock("game_events_#{game.id}")
end
end
def populate_game_events(game)

View File

@@ -145,15 +145,7 @@ class W3DHub
reorder_server_list
if @selected_server&.id == @refresh_server&.id
if @refresh_server
BackgroundWorker.foreground_job(
-> { fetch_server_details(@refresh_server) },
->(result) {
populate_server_info(@refresh_server) if @refresh_server == @selected_server
@refresh_server = nil
}
)
end
fetch_server_details(@refresh_server) if @refresh_server
end
end
end
@@ -353,10 +345,7 @@ class W3DHub
reorder_server_list if @selected_server_container
BackgroundWorker.foreground_job(
-> { fetch_server_details(server) },
->(result) { populate_server_info(server) if server == @selected_server }
)
fetch_server_details(server)
end
stylize_selected_server(server_container) if server.id == @selected_server&.id
@@ -523,10 +512,13 @@ class W3DHub
end
def fetch_server_details(server)
BackgroundWorker.foreground_job(
-> { Api.server_details(server.id, 2) },
->(server_data) { server.update(server_data) if server_data }
)
Api.server_details(server.id, 2) do |result|
if result.okay?
server.update(result.data)
populate_server_info(server) if server == @selected_server
@refresh_server = nil
end
end
end
def game_icon(server)