Coding task · JavaScript and React course · Module 9: Modern React Hooks
Practice: Web Workers
You talk to a worker thread only through messages: worker.postMessage(data) sends, worker.onmessage = (event) => ... receives, and the data is in event.data. Create the thread in useEffect with an empty dependency list and stop it in the cleanup function with terminate().
Task
The hyperspace jump calculator computes the fuel use in a worker thread, so that the interface does not freeze. The thread has the Web Worker API: postMessage sends data, onmessage receives the result and terminate stops the thread (in the preview the createWorker function imitates it). Fill in the blanks: ___BLANK1___ is the field of the event object that carries the result, ___BLANK2___ is the method the effect cleanup uses to stop the thread, and ___BLANK3___ is the method the click uses to send the distance to the thread. The thread should be created once after mounting, the button should stay disabled until the answer comes, and the result should appear on the screen.