Though global variables and methods are discouraged, the Mystery Master application defines the following globals.
let worker = new Worker("WebWorker.js");
The UI and the WW communicate via messages. The message could be a number, a string, or an object literal, which is like a structure. Here are some of the important methods to use for correspondence.
Here are some importants points about the two threads.
Because the web worker must communicate with the page via messages, there is no reason to have DOM-related code in the web worker. In fact, the web worker cannot access the window object, the document object, or the parent object.
Messages are passed as object literals, where the first field is named key. The defined messages are given below, sorted by key.Messages from UI to WW
Note: Setup options for the Solver, Finder, and Lawyer are not included in the above list.Messages from WW to UI
The purpose of the web worker is simple; it finds the solution to the puzzle. The web worker does not need to pause; and it does not stop working until one of the following conditions is met.
This is in direct contrast to what the UI needs to do: The user can ask the application to pause on various conditions. To reconcile these conflicting desires, the following is performed.
This process is similar to the Producer-Consumer model.
First, let's call each unit of work a task. So each task created by the worker (the Producer) is sent via a message to the master. The master (the Consumer) must unpack the task given by this message, and place the task in the queue. The master must also remove each task from the queue, and update the UI by performing the task. Now, there are some timing issues that need to be worked out.
There are three main components: the Viewer, the Solver, and the Puzzle. The Viewer object is instantiated in the UI thread, the Solver object is instantiated in the WW thread, and the Puzzle object is instantiated in both threads. The viewer (UI) and the solver (WW) communicate via the messenging process described earlier.
|Helper||UI, WW||Defines global objects and static methods.|
|NounType||UI, WW||Noun Type class.|
|Noun||UI, WW||Noun class.|
|Verb||UI, WW||Verb class.|
|Link||UI, WW||Link class.|
|Fact||UI, WW||Fact class.|
|Rule||UI, WW||Rule class.|
|Mark||UI, WW||Mark class.|
|SmartLink||UI, WW||Smart Link static class.|
|SmartRule||UI, WW||Smart Rule static class.|
|Puzzle||UI, WW||Base class that defines data and methods to solve the puzzle.|
|Solver||UI, WW||Calls the Finder and the Lawyer (not instantiated in UI).|
|Viewer||UI||Handles the User Interface (UI).|
|Locker||UI||Manager to save/read data from storage.|
|Board||UI||Manager to update the Board form.|
|Tabby||UI||Manager to update the tabbed interface.|
|Setup||UI||Manager to update the Setup form.|
|Stats||UI||Manager to update the Statistics form.|
|FileSaver||UI||Creates PHP files (for development only).|
|Finder||WW||Examines the facts of a puzzle to find marks.|
|Lawyer||WW||Examines marks to find additional marks.|
The following topics concern the UI.
Pausing is a concern only on the UI thread. Therefore, the marks that are entered into the queue via the solver.addMark method on the master thread must not update the viewer. Instead, the master thread needs to separately call the viewer for each mark in the queue because it may need to pause until the user clicks the Resume button. The user may request the program to pause in the situations given below.
Here are all of the possible states for the Work and Quit buttons.