[Proposal] Add message handler to worker API to catch more than one message

The current Worker.onmessage only provides the oldest message to the worker even if there are several on the stack. I propose a function “onAllmessage” that would be called with the same MessageEvent but with an array of events instead of one.

Reasoning: When a worker receives a message it will immediately call the message handler, however if the worker is busy because of a calculation, then the even handler will be called after the worker is no longer busy. During that time the worker may receive several messages and that means that all of those messages will be called one by one every time the worker is free. Depending on the logic one might need some, all or perhaps only the last message from the stack when the first message arrives.

In my use case I only needed the last message and every time I got a message I would start an algorithm that would block the worker for a few seconds, which meant that it would take a considerable time to reach the message I actually needed.

My workaround was to initiate a setTimeout every time the worker received a message and cancel the previous one with the same call. The setTimeout allows the rest of the messages to be handled and only the callback of the last will be called. This works, but if my worker received a lot of messages very fast, it would simply stop doing anything. If I we had an event handler to catch all messages this would not happen and we wouldn’t need a workaround.