Closed maximelkin closed 5 years ago
Used in data or context?
In both cases, because JSON.stringify(new Date) gives Date in ISO format as string
Good catch! 👍
The problem is indeed with JSON.stringify. Now, for worker's data I've already found a solution via v8.serialize but with worker's context I cannot use it because it would prevent the user to pass complex objects such as functions and classes. I'm trying to figure out a way to parse dates back into Date object but there's a problem with nested objects.
Here two examples:
However there are some limitations.
Suggestions?
This problem not only for Dates, it's hard to pass any class objects
I like this project and would like to recommend for ease of use not to try to serialize the whole universe into the worker.
try find what is most essentially needed in most cases and solve this.
If we need to have a specific function inside the worker, it can be scoped in or required, why the need to serialize Class instance and / or dates?
@maximelkin : if you have some explicit example, please share. it will be useful to use it as a test.
@syberkitten : I don't want to put any limit, for now.
The code passed to job
is evaluated in a separated context and if you need a function declared in the scope where is invoked job, then you need to pass it as ctx
.
The same issue applies to any kind of variable: objects, classes, etc.
In any case, if you don't want to serialize anything, just don't use data
and ctx
and you're done.
Mind that this lib is based on an experimental api: it may change in future (maybe shared memory with shared context? who knows...).
I suggest to add specific unit tests to test out different data structures, both inside context and data. At least one for each type, both primitives and composites, such as classes and deep nested objects.
Then, let's figure out how to handle the exceptions.
Data passed between the main page and workers is copied, not shared. Objects are serialized as they're handed to the worker, and subsequently, de-serialized on the other end. The page and worker do not share the same instance, so the end result is that a duplicate is created on each end. Most browsers implement this feature as structured cloning.
so that's expected behaviour
Currently it's not possible to pass class instances via message passing.
For more info take a look to the passing runtime context
paragraph: https://wilk.github.io/microjob/GUIDE.html#job-context
I'm closing it but I'll reopen if someone finds a way to do it.
Inside it will be string, not Date