Open
Description
This will likely have a few challenges associated with it. Specifically, self
in a WebWorker
points to the global context (like window
in the UI thread), but Opal overrides it in all compiled Ruby code. This may mean it will need to be written completely in JS (not even contained within a Ruby file).
What I'd like is to be able to do something like this:
Main app
require 'opal'
require 'bowser/worker'
worker = Bowser::Worker.new('worker.rb')
worker.on :message do |event|
puts event.data # prints "lol" to the console
end
worker.post_message "LOL"
worker.rb
require 'opal' # Yeah, it'll most likely need its own Opal runtime. :-\
require 'bowser/worker/process' # Should not be the same file the main app loads, so we'll need to figure out where to put this.
Bowser::Worker.on :message do |event|
post_message event.data.downcase # Multithreaded downcasing FTW
end
Main things:
- Proxy JS
MessageEvent
with aBowser::Event
on both sides. It's possible a lot of the message-passing code will be identical on both sides, now that I think of it. - A
WebWorker
is more like how we think of processes rather than threads. No memory is shared between the two. This will need to be documented. - Only JSON primitives can go between threads.
- We may need to
JSON.dump
whatever gets passed in (orto_n
, whichever is more reliable and/or consumes less CPU/RAM). Maybe allow native objects through unmodified. Cross-browser benchmarks needed before any optimization happens. Worker#post_message
will need to warn if anything other than aHash
,Array
,String
, orNumber
is passed to it. Raising an exception might be a good idea, too, but all Ruby objects can come through, but it will have its constructor/prototype properties mangled.
- We may need to
If we can solve this, supporting ServiceWorker
will be a nearly identical process.
Metadata
Metadata
Assignees
Labels
No labels