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

@@ -2,22 +2,21 @@ 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(:active, :context, :callback)
Request = Struct.new(:active, :context, :async, :callback)
Context = Data.define(
:request_id,
:method,
:url,
:headers,
:body,
:bearer_token
:body
)
def initialize
@requests = []
@running = true
Thread.new do
http_client = HttpClient.new
@thread = Thread.new do
@http_client = HttpClient.new
Sync do
while @running
@@ -33,18 +32,25 @@ class W3DHub
Async do |task|
assigned_request = request
result = http_client.handle(task, assigned_request)
result = if assigned_request.context.url.empty?
assigned_request.callback.call(nil)
else
@http_client.handle(task, assigned_request)
end
@requests.delete(assigned_request)
Store.main_thread_queue << -> { assigned_request.callback.call(result) }
# callback for this is already handled!
unless assigned_request.context.url.empty?
Store.main_thread_queue << -> { assigned_request.callback.call(result) }
end
end
end
end
end
end
def request(method, url, headers, body, bearer_token, &block)
def request(method, url, headers, body, async, &block)
request_id = SecureRandom.hex
request = Request.new(
@@ -54,15 +60,29 @@ class W3DHub
method,
url,
headers,
body,
bearer_token
body
),
async,
block
)
@requests << request
request_id
if async
request_id
else # Not async, process immediately.
raise "WTF? This should NOT happen!" unless Async::Task.current?
Sync do |task|
assigned_request = request
result = @http_client.handle(task, assigned_request)
@requests.delete(assigned_request)
# "return" callback "value"
assigned_request.callback.call(result)
end
end
end
def busy?