20 ms·
> dumps constructors to text form and sends them to a worker Oh do tell more, that sounds devious.
by lioeters 3y ago
> dumps constructors to text form and sends them to a worker
Oh do tell more, that sounds devious.
- Dylan16807 3y agoIt's mostly a series of `foo=${JSON.stringify(foo)};` and Bar.prototype.constructor.toString(). There's one object that didn't cooperate with either of those, but it's only used in a very limited way, so I did a quick hack to pack its prototype up. At the end is a function that sets up self.onmessage, and a line that executes that function. Then I do .join('\r\n') on the mess, turn it into a Blob, and pass the URL of the blob into new Worker(). The code running in the worker is only a few classes so it's not worth the hassle of setting up code-copying workarounds or avoiding modules for that portion of the code. It'll be nice when I can cut that and have nothing more than a few imports and self.onmessage.
- lioeters 3y agoI see, a wonderous hack. Sounds like something a build tool/plugin or code transformation step could make it work magically behind the scenes. It's cool and also a bit horrifying (haha) that this is possible dynamically, to stringify a function and send it over to a worker thread to run. After all, code is data..