mirror of
https://github.com/cyberarm/w3d_hub_linux_launcher.git
synced 2025-12-15 16:52:34 +00:00
Window is no longer a fiber, should prevent window from locking up due to a fiber not yielding, replaced ui's direct async calls with BackgroundWorker.foreground_job, show pulsing circle behind app logo on boot
This commit is contained in:
77
lib/background_worker.rb
Normal file
77
lib/background_worker.rb
Normal file
@@ -0,0 +1,77 @@
|
||||
class W3DHub
|
||||
class BackgroundWorker
|
||||
@@instance = nil
|
||||
@@alive = false
|
||||
|
||||
def self.create
|
||||
raise "BackgroundWorker instance already exists!" if @@instance
|
||||
|
||||
@@alive = true
|
||||
@@instance = self.new
|
||||
|
||||
Async do
|
||||
@@instance.handle_jobs
|
||||
end
|
||||
end
|
||||
|
||||
def self.instance
|
||||
@@instance
|
||||
end
|
||||
|
||||
def self.alive?
|
||||
@@alive
|
||||
end
|
||||
|
||||
def self.shutdown!
|
||||
@@alive = false
|
||||
end
|
||||
|
||||
def self.job(job, callback)
|
||||
@@instance.add_job(Job.new(job, callback))
|
||||
end
|
||||
|
||||
def self.foreground_job(job, callback)
|
||||
@@instance.add_job(Job.new(job, callback, true))
|
||||
end
|
||||
|
||||
def initialize
|
||||
@jobs = []
|
||||
end
|
||||
|
||||
def handle_jobs
|
||||
while BackgroundWorker.alive?
|
||||
job = @jobs.shift
|
||||
|
||||
job&.do
|
||||
|
||||
sleep 0.1
|
||||
end
|
||||
end
|
||||
|
||||
def add_job(job)
|
||||
@jobs << job
|
||||
end
|
||||
|
||||
class Job
|
||||
def initialize(job, callback, deliver_to_queue = false)
|
||||
@job = job
|
||||
@callback = callback
|
||||
|
||||
@deliver_to_queue = deliver_to_queue
|
||||
end
|
||||
|
||||
def do
|
||||
result = @job.call
|
||||
deliver(result)
|
||||
end
|
||||
|
||||
def deliver(result)
|
||||
if @deliver_to_queue
|
||||
$window.main_thread_queue << -> { @callback.call(result) }
|
||||
else
|
||||
@callback.call(result)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user