From ebc045019a6c56267db7d606d91cc17a87a1f748 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Sat, 31 Jan 2026 22:33:14 -0600 Subject: [PATCH] More work on network handling refactor --- lib/api/server_list_server.rb | 2 +- lib/cache.rb | 14 ++++----- lib/network_manager.rb | 13 ++++++-- lib/pages/games.rb | 36 +++++++++++----------- lib/pages/login.rb | 58 ++++++++++++++++------------------- lib/states/boot.rb | 3 +- lib/window.rb | 2 ++ 7 files changed, 64 insertions(+), 64 deletions(-) diff --git a/lib/api/server_list_server.rb b/lib/api/server_list_server.rb index 3aa1e30..3c5ffa6 100644 --- a/lib/api/server_list_server.rb +++ b/lib/api/server_list_server.rb @@ -61,7 +61,7 @@ class W3DHub States::Interface.instance&.update_server_ping(self) end - return unless W3DHub.windows? + return #unless W3DHub.windows? W3DHub::BackgroundWorker.foreground_parallel_job( lambda do diff --git a/lib/cache.rb b/lib/cache.rb index 0aa2273..0e265e8 100644 --- a/lib/cache.rb +++ b/lib/cache.rb @@ -9,19 +9,17 @@ class W3DHub end # Fetch a generic uri - def self.fetch(uri:, force_fetch: false, async: true, backend: :w3dhub) + def self.fetch(uri:, force_fetch: false, backend: :w3dhub) path = path(uri) if !force_fetch && File.exist?(path) path - elsif async - BackgroundWorker.job( - -> { Api.fetch(uri, W3DHub::Api::DEFAULT_HEADERS, nil, backend) }, - ->(response) { File.open(path, "wb") { |f| f.write response.body } if response.status == 200 } - ) else - response = Api.fetch(uri, W3DHub::Api::DEFAULT_HEADERS, nil, backend) - File.open(path, "wb") { |f| f.write response.body } if response.status == 200 + Api.fetch(uri, W3DHub::Api::DEFAULT_HEADERS, nil, backend) do |result| + if result.okay? + File.open(path, "wb") { |f| f.write result.data } + end + end end end diff --git a/lib/network_manager.rb b/lib/network_manager.rb index 131a748..7e77e96 100644 --- a/lib/network_manager.rb +++ b/lib/network_manager.rb @@ -2,7 +2,7 @@ class W3DHub # all http(s) requests for API calls and downloading images run through here class NetworkManager NetworkEvent = Data.define(:context, :result) - Request = Struct.new(:context, :callback) + Request = Struct.new(:active, :context, :callback) Context = Data.define( :request_id, :method, @@ -21,7 +21,7 @@ class W3DHub Sync do while @running - request = @requests.shift + request = @requests.find { |r| !r.active } # goto sleep for an second if there is no work to be doing unless request @@ -29,11 +29,13 @@ class W3DHub next end + request.active = true + Async do |task| assigned_request = request result = http_client.handle(task, assigned_request) - pp [assigned_request, result] + @requests.delete(assigned_request) Store.main_thread_queue << -> { assigned_request.callback.call(result) } end @@ -46,6 +48,7 @@ class W3DHub request_id = SecureRandom.hex request = Request.new( + false, Context.new( request_id, method, @@ -61,5 +64,9 @@ class W3DHub request_id end + + def busy? + @requests.any?(&:active) + end end end diff --git a/lib/pages/games.rb b/lib/pages/games.rb index 5b99848..81389ed 100644 --- a/lib/pages/games.rb +++ b/lib/pages/games.rb @@ -295,15 +295,15 @@ class W3DHub 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 - ) + # 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 @@ -315,15 +315,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 - ) + # BackgroundWorker.foreground_job( + # -> { fetch_game_news(game) }, + # lambda do |result| + # if result + # populate_game_news(game) + # Cache.release_net_lock(result) + # end + # end + # ) end end end diff --git a/lib/pages/login.rb b/lib/pages/login.rb index 0b9afb1..b78cd55 100644 --- a/lib/pages/login.rb +++ b/lib/pages/login.rb @@ -34,9 +34,8 @@ class W3DHub # Do network stuff - BackgroundWorker.foreground_job( - lambda do - account = Api.user_login(@username.value, @password.value) + Api.user_login(@username.value, @password.value) do |result| + if result.okay? applications = nil if account @@ -45,14 +44,15 @@ class W3DHub Store.settings.save_settings if account - Cache.fetch(uri: account.avatar_uri, force_fetch: true, async: false, backend: :w3dhub) - applications = Api._applications + Cache.fetch(uri: account.avatar_uri, force_fetch: true, backend: :w3dhub) {} + Api._applications do |r| + applications = r.result if r.okay? + end end end [account, applications] - end, - lambda do |result| + else account, applications = result if account @@ -70,7 +70,7 @@ class W3DHub @error_label.value = "Incorrect username or password.\nOr too many failed login attempts, try again in a few minutes." end end - ) + end end @error_label = caption "", width: 1.0, text_align: :center, color: 0xff_800000 @@ -80,13 +80,10 @@ class W3DHub end if Store.account - BackgroundWorker.foreground_job( - -> { Cache.fetch(uri: Store.account.avatar_uri, async: false, backend: :w3dhub) }, - ->(result) { - populate_account_info - page(W3DHub::Pages::Games) - } - ) + Cache.fetch(uri: Store.account.avatar_uri, backend: :w3dhub) do |result| + populate_account_info + page(W3DHub::Pages::Games) + end end end @@ -153,29 +150,26 @@ class W3DHub Store.settings.save_settings Store.account = nil - BackgroundWorker.foreground_job( - -> { Api._applications }, - lambda do |applications| - if applications - Store.applications = applications - page(W3DHub::Pages::Games) if @host.current_page.is_a?(W3DHub::Pages::Games) - page(W3DHub::Pages::ServerBrowser) if @host.current_page.is_a?(W3DHub::Pages::ServerBrowser) - end + Api._applications do |result| + if result.okay? + Store.applications = result.data + page(W3DHub::Pages::Games) if @host.current_page.is_a?(W3DHub::Pages::Games) + page(W3DHub::Pages::ServerBrowser) if @host.current_page.is_a?(W3DHub::Pages::ServerBrowser) + end - @host.instance_variable_get(:"@account_container").clear do - stack(width: 1.0, height: 1.0) do - tagline "#{I18n.t(:"interface.not_logged_in")}", text_wrap: :none + @host.instance_variable_get(:"@account_container").clear do + stack(width: 1.0, height: 1.0) do + tagline "#{I18n.t(:"interface.not_logged_in")}", text_wrap: :none - flow(width: 1.0) do - link(I18n.t(:"interface.log_in"), text_size: 22, width: 0.5) { page(W3DHub::Pages::Login) } - link I18n.t(:"interface.register"), text_size: 22, width: 0.49 do - W3DHub.url("https://secure.w3dhub.com/forum/index.php?app=core&module=global§ion=register") - end + flow(width: 1.0) do + link(I18n.t(:"interface.log_in"), text_size: 22, width: 0.5) { page(W3DHub::Pages::Login) } + link I18n.t(:"interface.register"), text_size: 22, width: 0.49 do + W3DHub.url("https://secure.w3dhub.com/forum/index.php?app=core&module=global§ion=register") end end end end - ) + end end end end diff --git a/lib/states/boot.rb b/lib/states/boot.rb index 80b82d0..04f686a 100644 --- a/lib/states/boot.rb +++ b/lib/states/boot.rb @@ -115,9 +115,8 @@ class W3DHub Api.on_thread(:refresh_user_login, account.refresh_token) do |refreshed_account| update_account_data(refreshed_account) end - else - BackgroundWorker.foreground_job(-> { update_account_data(account) }, ->(_) {}) + Store.main_thread_queue << -> { update_account_data(account) } end else diff --git a/lib/window.rb b/lib/window.rb index 21c392e..245bd93 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -36,6 +36,8 @@ class W3DHub while (block = Store.main_thread_queue.shift) block&.call end + + sleep(update_interval / 1000.0) if Store.application_manager.busy? || Store.network_manager.busy? end def needs_redraw?