In this article, a DIRTY, unsafe, unstable and scary <em>eval</em> method will be described. So, if you are uncomfortable with that, stop reading right now. First off, some issues with convenience remained unresolved: in code sent to web web workers, closure can't be used. All of us like new technologies, and all of us like new technologies to be convenient to use. However, it's not exactly the case with web workers. web workers accept files or links to files, which isn't convenient. It would be good to be able to put any task into web workers, not just specifically planned code. What do we need to make web workers more convenient to operate? I believe, it's the following: Let's try to write it. For starters, we'll need a communication protocol between a web worker and the main window. In general, a protocol is just a structure and types of data used for communication between a browser window and a web worker. It's pretty straightforward. You can use this or write your own version. Every message will have an ID and data typical of a specific message type. Initially, we'll have two types of messages for web workers: Before writing a web worker, we need to describe a file that will be inside of it, supporting the protocol described above. I like object-oriented programming (OOP), so this will be a class named workerBody. This class has to subscribe to an event from the parent window. Now we can listen to events from the parent window. We have two types of events: those which imply a reply and all the rest. Let's do events: \
Libraries and files are added to a web worker using importScripts API. And now the scariest part: for launching a random function, we'll use eval. The method onMessage is responsible for receiving a message and choosing a handler, doWork launches a sent function and send sends a reply to the parent window. Now that we have the web worker's content, we need to learn to serialize and parse any data, so they could be sent to the web worker. Let's start with a serializer. We want to be able to send to the web worker any data, including class instances, classes and functions, while the web worker's native capacity enables sending only JSON-like data. To go around that, we'll need _eval_. We'll wrap all data that JSON can't accept into corresponding sting structures and launch them on the other side. To preserve immutability, received data will be cloned on the fly, replacing whatever cannot be serialized by ordinary methods with service objects, which will be replaced back on the other side by a parser. At first sight, this task isn't difficult, but there are many pitfalls. The scariest limitation of this approach is the inability to use closure, which leads to a slightly different code writing style. Let's start with the easiest part, the function. First, we need to learn to distinguish a function from a class constructor. Let's do that: First, we'll check if the function has a prototype. If it doesn't, this is certainly a function. Then, we look at the number of the prototype's features. If it only has a constructor and the function isn't a successor of another class, we consider it a function. When we discover a function, we just replace it with a service object with the fields __type = “serialized-function“ and template corresponds to the template of this function (func.toString()).  For now, we'll skip class and look at class instance. Later, we'll need to distinguish between regular objects and class instances. We believe that an object is regular if it doesn’t have a constructor or its constructor is a native function. Once we've discovered a class instance, we'll replace it with a service object with the following fields: To send data, we have to create an extra field, in which we will store a list of unique classes that we send. However, there is a challenge: discovering a class, we need to take not only its template, but also the templates of all parent classes and save them as independent classes, so every parent class gets sent only once, also saving instanceof proof. Discovering a class is easy: this is a function that failed our Serializer.isFunction proof. When adding a class, we check the presence of that class on the list of serialized data and add only unique classes. Code that assembles a class into a template is quite large and is available here. In the parser, we initially check all classes sent to us and compile them if they haven’t been sent. Then, we recursively check every data field and replace service objects with compiled data. The most interesting part is class instances. We have a class and data that were in its instance, but we can't just create an instance as a constructor request may require parameters that we don't have. We get that from the nearly forgotten Object.create method, which creates an object with a set prototype. This way, we avoid requesting a constructor, get a class instance and just copy properties into the instance. For a web worker to operate successfully, we need a parser and a serializer within the web worker and outside. So we take a serializer and turn it, parser and web worker body into a template. From the template, we make a blob and create a download link over URL.createObjectURL (this method may not work for some “Content-Security-Policy”). This method is also good for launching random code from a string. So, we got a simple-to-use library that can send any code to the web worker. It supports TypeScript classes, for instance: Unfortunately, this library is far from ideal. We need to add support of setters and getters for classes, objects, prototypes and static features. Also, we need to add caching, an alternative script launch without eval, using URL.createObjectURL instead. Finally, a file with the web worker content needs to be added to the assembly (in case on-the-fly creation is not available) etc. Visit the repository! javascript разработчик