More work on network handling refactor

This commit is contained in:
2026-01-31 22:33:14 -06:00
parent 79858a02ce
commit ebc045019a
7 changed files with 64 additions and 64 deletions

View File

@@ -61,7 +61,7 @@ class W3DHub
States::Interface.instance&.update_server_ping(self) States::Interface.instance&.update_server_ping(self)
end end
return unless W3DHub.windows? return #unless W3DHub.windows?
W3DHub::BackgroundWorker.foreground_parallel_job( W3DHub::BackgroundWorker.foreground_parallel_job(
lambda do lambda do

View File

@@ -9,19 +9,17 @@ class W3DHub
end end
# Fetch a generic uri # 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) path = path(uri)
if !force_fetch && File.exist?(path) if !force_fetch && File.exist?(path)
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 else
response = Api.fetch(uri, W3DHub::Api::DEFAULT_HEADERS, nil, backend) Api.fetch(uri, W3DHub::Api::DEFAULT_HEADERS, nil, backend) do |result|
File.open(path, "wb") { |f| f.write response.body } if response.status == 200 if result.okay?
File.open(path, "wb") { |f| f.write result.data }
end
end
end end
end end

View File

@@ -2,7 +2,7 @@ class W3DHub
# all http(s) requests for API calls and downloading images run through here # all http(s) requests for API calls and downloading images run through here
class NetworkManager class NetworkManager
NetworkEvent = Data.define(:context, :result) NetworkEvent = Data.define(:context, :result)
Request = Struct.new(:context, :callback) Request = Struct.new(:active, :context, :callback)
Context = Data.define( Context = Data.define(
:request_id, :request_id,
:method, :method,
@@ -21,7 +21,7 @@ class W3DHub
Sync do Sync do
while @running while @running
request = @requests.shift request = @requests.find { |r| !r.active }
# goto sleep for an second if there is no work to be doing # goto sleep for an second if there is no work to be doing
unless request unless request
@@ -29,11 +29,13 @@ class W3DHub
next next
end end
request.active = true
Async do |task| Async do |task|
assigned_request = request assigned_request = request
result = http_client.handle(task, assigned_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) } Store.main_thread_queue << -> { assigned_request.callback.call(result) }
end end
@@ -46,6 +48,7 @@ class W3DHub
request_id = SecureRandom.hex request_id = SecureRandom.hex
request = Request.new( request = Request.new(
false,
Context.new( Context.new(
request_id, request_id,
method, method,
@@ -61,5 +64,9 @@ class W3DHub
request_id request_id
end end
def busy?
@requests.any?(&:active)
end
end end
end end

View File

@@ -295,15 +295,15 @@ class W3DHub
if @game_events[game.id] if @game_events[game.id]
populate_game_events(game) populate_game_events(game)
else else
BackgroundWorker.foreground_job( # BackgroundWorker.foreground_job(
-> { fetch_game_events(game) }, # -> { fetch_game_events(game) },
lambda do |result| # lambda do |result|
if result # if result
populate_game_events(game) # populate_game_events(game)
Cache.release_net_lock(result) # Cache.release_net_lock(result)
end # end
end # end
) # )
end end
end end
@@ -315,15 +315,15 @@ class W3DHub
title I18n.t(:"games.fetching_news"), padding: 8 title I18n.t(:"games.fetching_news"), padding: 8
end end
BackgroundWorker.foreground_job( # BackgroundWorker.foreground_job(
-> { fetch_game_news(game) }, # -> { fetch_game_news(game) },
lambda do |result| # lambda do |result|
if result # if result
populate_game_news(game) # populate_game_news(game)
Cache.release_net_lock(result) # Cache.release_net_lock(result)
end # end
end # end
) # )
end end
end end
end end

View File

@@ -34,9 +34,8 @@ class W3DHub
# Do network stuff # Do network stuff
BackgroundWorker.foreground_job( Api.user_login(@username.value, @password.value) do |result|
lambda do if result.okay?
account = Api.user_login(@username.value, @password.value)
applications = nil applications = nil
if account if account
@@ -45,14 +44,15 @@ class W3DHub
Store.settings.save_settings Store.settings.save_settings
if account if account
Cache.fetch(uri: account.avatar_uri, force_fetch: true, async: false, backend: :w3dhub) Cache.fetch(uri: account.avatar_uri, force_fetch: true, backend: :w3dhub) {}
applications = Api._applications Api._applications do |r|
applications = r.result if r.okay?
end
end end
end end
[account, applications] [account, applications]
end, else
lambda do |result|
account, applications = result account, applications = result
if account 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." @error_label.value = "Incorrect username or password.\nOr too many failed login attempts, try again in a few minutes."
end end
end end
) end
end end
@error_label = caption "", width: 1.0, text_align: :center, color: 0xff_800000 @error_label = caption "", width: 1.0, text_align: :center, color: 0xff_800000
@@ -80,13 +80,10 @@ class W3DHub
end end
if Store.account if Store.account
BackgroundWorker.foreground_job( Cache.fetch(uri: Store.account.avatar_uri, backend: :w3dhub) do |result|
-> { Cache.fetch(uri: Store.account.avatar_uri, async: false, backend: :w3dhub) },
->(result) {
populate_account_info populate_account_info
page(W3DHub::Pages::Games) page(W3DHub::Pages::Games)
} end
)
end end
end end
@@ -153,11 +150,9 @@ class W3DHub
Store.settings.save_settings Store.settings.save_settings
Store.account = nil Store.account = nil
BackgroundWorker.foreground_job( Api._applications do |result|
-> { Api._applications }, if result.okay?
lambda do |applications| Store.applications = result.data
if applications
Store.applications = applications
page(W3DHub::Pages::Games) if @host.current_page.is_a?(W3DHub::Pages::Games) 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) page(W3DHub::Pages::ServerBrowser) if @host.current_page.is_a?(W3DHub::Pages::ServerBrowser)
end end
@@ -175,7 +170,6 @@ class W3DHub
end end
end end
end end
)
end end
end end
end end

View File

@@ -115,9 +115,8 @@ class W3DHub
Api.on_thread(:refresh_user_login, account.refresh_token) do |refreshed_account| Api.on_thread(:refresh_user_login, account.refresh_token) do |refreshed_account|
update_account_data(refreshed_account) update_account_data(refreshed_account)
end end
else else
BackgroundWorker.foreground_job(-> { update_account_data(account) }, ->(_) {}) Store.main_thread_queue << -> { update_account_data(account) }
end end
else else

View File

@@ -36,6 +36,8 @@ class W3DHub
while (block = Store.main_thread_queue.shift) while (block = Store.main_thread_queue.shift)
block&.call block&.call
end end
sleep(update_interval / 1000.0) if Store.application_manager.busy? || Store.network_manager.busy?
end end
def needs_redraw? def needs_redraw?