Discussions

Ask a Question
Back to All

Is there a way to differentiate contexts?

So essentially I am looking to set a flag in one of my files to ensure code beat understands that the file actually executes inside a different context. My project is unique in the fact that it actually uses multiple threads inside your web browser, therefore there are some functions that are duplicated out of necessity and there is no way to avoid duplicating them because the duplicated logic lives at run time in a completely separate execution context and there is no way to reference the logic otherwise while adhering to the HTML web worker specifications as web workers are sand boxed environments. Using proxy objects or something might be a potential work around but that would violate the security and specs for worker threads. Workers should not under any circumstances have access to variables on the main thread, everything must be sent as a message. Sending the duplicated functions as a message would also be impractical since it would kill performance due to increased communication overhead and the need to parse the now serialized functions back into functions using new Function or eval.

Ideally there should be a flag I can set in my file to make sure code beat understands these functions / this logic doesn't actually execute on the main thread alongside the rest of the app.