8000 Add WebWorker support · Issue #3 · clearwater-rb/bowser · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Add WebWorker support #3
Open
Open
@jgaskins

Description

@jgaskins

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 a Bowser::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 (or to_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 a Hash, Array, String, or Number 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.

If we can solve this, supporting ServiceWorker will be a nearly identical process.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0