git.fiddlerwoaroof.com
Browse code

Document entrypoint a bit

Ed Langley authored on 21/06/2019 17:45:01
Showing 1 changed files
... ...
@@ -17,34 +17,45 @@ import { put } from "redux-saga/effects";
17 17
 
18 18
 // Store Setup ==============================================================================
19 19
 
20
-// Make redux devtools work, if present
20
+// Get enhancer for redux devtools, if present
21 21
 const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
22 22
 
23
-// Make the redux store, with saga middleware enabled
23
+// Setup redux sagas
24 24
 const sagaMiddleware = createSagaMiddleware();
25
+
26
+// Setup redux
25 27
 export const store = createStore(
26 28
   rootReducer,
27 29
   composeEnhancers(applyMiddleware(sagaMiddleware))
28 30
 );
29 31
 
32
+// Map redux state to our root component using the `connect`
33
+// higher-order component.
30 34
 const ConnectedRoot = connect(
31 35
   // Map the store's state to the toplevel component's props
32 36
   R.pickAll(["name", "ip", "error"]),
33
-  // Map redux's dispatch function to props that will call it with a specific action
37
+  // Map redux's dispatch function to props that will call it with a
38
+  // specific action
34 39
   withDispatch({ getIp, updateName, fail, restart })
35 40
 )(Root);
36 41
 
42
+// This is the "main" function of our application
43
+//
44
+// It's primarily responsible for running the application's root saga,
45
+// rendering the application's react component and handling errors
46
+// that occur while the application's root saga is running.
37 47
 function* toplevel() {
38
-  // Run the root saga
48
+  // Start our root saga
39 49
   let rootTask = yield spawn(rootSaga);
40 50
 
41
-  // Connect Redux to Toplevel Component ======================================================
42
-  // Render Connected Component to the dom at #root ===========================================
51
+  // Wait for the root saga to generate the APP_INIT event
43 52
   yield take("co/fwoar/APP_INIT");
44 53
 
54
+  // Render react to the dom element #root
45 55
   const reactRender = new Promise(resolve => {
46 56
     ReactDOM.render(
47
-      // The Toplevel component is wrapped in a provider, to make the store available to connect
57
+      // The Toplevel component is wrapped in a provider, to make the
58
+      // store available to connect
48 59
       <Provider store={store}>
49 60
         <ConnectedRoot />
50 61
       </Provider>,
... ...
@@ -53,16 +64,23 @@ function* toplevel() {
53 64
     );
54 65
   });
55 66
 
67
+  // wait for react to finish rendering
56 68
   yield reactRender;
57 69
 
70
+  // wait for the root task to finish, handling errors and restarting
71
+  // it if it ever exits
58 72
   let quit = false;
59 73
   while (!quit) {
60 74
     try {
61 75
       yield rootTask.toPromise();
62 76
     } catch (err) {
63 77
       console.log("Error!", err);
78
+      // Give the reducers a chance to store the error somewhere
64 79
       yield put(recordError(err.toString()));
65 80
 
81
+      // Handle actions that can be fired in response to an error:
82
+      // - APP_QUIT just causes this saga to end
83
+      // - APP_RESTART tries to restart the root saga
66 84
       const { type } = yield take([
67 85
         "co/fwoar/APP_RESTART",
68 86
         "co/fwoar/APP_QUIT"