fix: handle exposing multiple workers sending message to wrong worker #681
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello! I will be up front, for 95% of this change, I used AI to do the work. I do not fully understand why workers are receiving messages intended for other workers, but I have verified in my codebase that this fix does work.
I have a main thread object,
ServiceA, that creates a worker from another class,MainWorker. This worker creates three of its own workers,WorkerA,WorkerB, andWorkerC. Each of these expose an instance of their respective types:Comlink.expose(new <worker class>())in their own file. The problem I encounter is, method calls toWorkerAalso get sent toWorkerBandWorkerC, which throw errors inComlink.exposedue torawValue.applycallingapplyon undefined, because it can't findmethodAon the other two workers.I've added a console.warn, and I can see that those warnings are written when multiple workers are registered. This leads me to think that
exposeis still attempting to register incorrectly(?), but the fix does prevent those method calls on the incorrect worker. I would love to get this problem resolved in the main library, currently I'm using my own fork.Thank you for your time! Please let me know if you have any comments, questions, etc.
Fixes #294, #450, #470, #568