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)
end
return unless W3DHub.windows?
return #unless W3DHub.windows?
W3DHub::BackgroundWorker.foreground_parallel_job(
lambda do

View File

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

View File

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

View File

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

View File

@@ -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) {
Cache.fetch(uri: Store.account.avatar_uri, backend: :w3dhub) do |result|
populate_account_info
page(W3DHub::Pages::Games)
}
)
end
end
end
@@ -153,11 +150,9 @@ class W3DHub
Store.settings.save_settings
Store.account = nil
BackgroundWorker.foreground_job(
-> { Api._applications },
lambda do |applications|
if applications
Store.applications = applications
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
@@ -175,7 +170,6 @@ class W3DHub
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|
update_account_data(refreshed_account)
end
else
BackgroundWorker.foreground_job(-> { update_account_data(account) }, ->(_) {})
Store.main_thread_queue << -> { update_account_data(account) }
end
else

View File

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