developit / web-worker

Consistent Web Workers in browser and Node.
https://npm.im/web-worker
Apache License 2.0
1.06k stars 55 forks source link

fix: allow for nesting workers #40

Closed eshaz closed 11 months ago

eshaz commented 1 year ago

This fixes an issue where it's not possible to use this library inside of a NodeJS worker thread, since the check below is not accounting for where the thread originated from. When using this library within a worker thread, the workerThread() code is executed, rather than the mainThread() code which creates the worker polyfill.

https://github.com/developit/web-worker/blob/29fef9775702c91887d3d8733e595edf1a188f31/node.js#L73

This change executes workerThread() only if the mod property is present from the workerData, indicating that it's being executed from a thread created within this library. If mod isn't present, this means that the code is executing within the context of some other worker thread that wasn't created from this library, and it needs to treat the current thread as the main thread by executing mainThread().

One enhancement might be to randomize the mod property name. This change assumes that a parent thread won't send a mod property in the workerData...

chanmathew commented 1 year ago

Hi @eshaz - any update on merging this? Would this also address #42? This should also be updated in your other package https://github.com/eshaz/wasm-audio-decoders which is using this as a dependency.

developit commented 11 months ago

Thanks for taking the time to articulate the logic here, and sorry for taking forever to merge!

This was released in 1.3.0.