wilk / microjob

A tiny wrapper for turning Node.js worker threads into easy-to-use routines for heavy CPU loads.
https://wilk.github.io/microjob/
MIT License
2.02k stars 47 forks source link

Unexpected Date format inside worker #7

Closed maximelkin closed 5 years ago

maximelkin commented 6 years ago

Inside it will be string, not Date

wilk commented 6 years ago

Used in data or context?

maximelkin commented 6 years ago

In both cases, because JSON.stringify(new Date) gives Date in ISO format as string

wilk commented 6 years ago

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?

maximelkin commented 6 years ago

This problem not only for Dates, it's hard to pass any class objects

syberkitten commented 6 years ago

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?

wilk commented 6 years ago

@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...).

wilk commented 6 years ago

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.

vvscode commented 5 years ago

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.

https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#Transferring_data_to_and_from_workers_further_details

so that's expected behaviour

wilk commented 5 years ago

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.