webtoon / psd

Fast zero-dependency PSD parser for the web and Node.js
https://webtoon.github.io/psd
MIT License
1.21k stars 55 forks source link

Investigate passing to/from web workers #36

Open pastelmind opened 2 years ago

pastelmind commented 2 years ago

A task that parses a large PSD file can often exceed the 16ms budget in web browsers and block UI updates. To avoid this, it's natural to attempt parsing a PSD file in a Web Worker via postMessage() and sending the results back. It would be nice to support this use case.

Unfortunately, this is currently impossible because the Psd object cannot be reliably (de)serialized with the structured clone algorithm. Psd provides instance methods and getters, which are lost when passed to/from a Web Worker.

We have some ideas, but would still like feedback:

  1. Convert the Psd class (and other classes) into plain, structured clone-able objects
    • Instance methods will have to be converted to plain functions
    • This will be quite disruptive as it is backwards incompatible
  2. Create Psd.prototype.serialize() that generates a structured clone-able implementation, as well as Psd.deserialize() that reconstructs the original Psd object
    • This will be clunky and leak implementation details all over