The dispatcher pattern
We have seen previously that either our parent process or the web worker process can initiate communication by posting (sending) a message to each other. As long as the proper listener (onmessage
) has been defined, either one can receive and react to these events. In the Dispatcher pattern, these messages contain information related to the event, such as data. The key factor that separates this design pattern is that the event messages must be published between threads and scheduled for execution upon arrival. Of course, this scheduling can also include the “immediate execution” of some task or function.
The implementation of this pattern is rather trivial, and you may think of it as akin to the Command Pattern that we saw in Chapter 2, Software Design Principles and Patterns, so we will not see this again. Instead, we will take these concepts of communication between threads, scheduling, and events with data to create our solution to work with...