These are chat archives for opal/opal

1st
Nov 2018
Bernhard Weichel
@bwl21
Nov 01 2018 15:52
while implementing the application, i am facing a problem. The worker API does a toll free bridging of Javascript objects. But when I send an Opal $Hash, it is received as $Object. I do not know how to convert this then to a $Hash. If anything goes wrong, I have to implement my own JSON-based serialization. Are there any other ideas?
Jamie Gaskins
@jgaskins
Nov 01 2018 20:06
Yeah, it loses the constructor and __proto__ properties. I ran into the same issue with Bowser's IndexedDB support so I added an API that lets you tell it what kind of object it is and it injects the properties into a freshly allocated instance.
You might be able to pass along a $$class_name property, then on the other side:
worker.on_message do |msg|
  object = `msg.$$class_name`
    .split('::')
    .inject(`Opal`) { |namespace, class_name|
      `namespace[class_name]`
        .allocate
        .tap { |obj| `Object.assign(obj, msg.payload)` }
    }

  # object is now a Ruby object of the correct class
end
Jamie Gaskins
@jgaskins
Nov 01 2018 20:13
But that means that Worker#post_message would need to transform the hash it receives into a POJO before sending it across the bridge.
Also, are you sure it's a toll-free bridge? Last I read, Safari was the only browser that had good performance in posting across the bridge. All the other browsers were effectively serializing and deserializing JSON.
Oh wait, looks like I was mistaken — other browsers were performing even worse across the bridge than serializing/deserializing JSON. https://nolanlawson.com/2016/02/29/high-performance-web-worker-messages/ But the article was updated since then and apparently Chrome has gotten better.
Bernhard Weichel
@bwl21
Nov 01 2018 22:08
I now have implemented another send message which serializes as json. It also sends a message type so I can install type specific handlers.
I will try with Firefox tomorrow.