| 1 |
--- |
| 2 |
subtitle: Application Design |
| 3 |
--- |
| 4 |
|
| 5 |
This tutorial introduces how to design a **Dome** application within the |
| 6 |
**Electron** & **React** frameworks. You shall be reasonably familiar with |
| 7 |
**React** concepts, but no knowledge of **Electron** is required. |
| 8 |
|
| 9 |
A desktop **Dome** application looks like a native application, but is actually |
| 10 |
a static web page rendered in a **Chrome** web browser. This is what provides |
| 11 |
the **Electron** framework by default. **Dome** is simply a library of **React** |
| 12 |
components and development templates tuned together to enable professional |
| 13 |
application development. |
| 14 |
|
| 15 |
This tutorial provides an overview of this environment and how to design a typical |
| 16 |
**Dome** application. |
| 17 |
|
| 18 |
## System Architecture |
| 19 |
|
| 20 |
Following the **Electron** framework design, your application will consists of |
| 21 |
two different processes interacting with each others. |
| 22 |
One is responsible for the management of the main GUI resources |
| 23 |
(windows, menu-bar, desktop interaction, user settings, etc.) |
| 24 |
and is named the `Main` process. |
| 25 |
The second one will be responsible for running the |
| 26 |
web page holding the main application window, and is named the `Renderer` process. |
| 27 |
Both kind of process communicates through the message-passing API provided by |
| 28 |
the **Electron** framework. |
| 29 |
|
| 30 |
When several instances of your application are running simultaneously, each invokation |
| 31 |
have its own window, running its owan, separate `Renderer` process. However, the |
| 32 |
**Dome** framework automatically makes them sharing the same `Main` process. |
| 33 |
|
| 34 |
Hence, each application instance has its own working directory and command-line arguments, |
| 35 |
depending on how its has been launched by the user. The **Dome** framework build |
| 36 |
both command-line and desktop entry points, depending on each target platform. |
| 37 |
|
| 38 |
## Event Driven Design |
| 39 |
|
| 40 |
On the renderer process side, the **React** framework induces a natural design where |
| 41 |
_Application State_ is separated from _Application Rendering_. Moreover, |
| 42 |
following a popular design introduced with **Redux**, application rendering tend |
| 43 |
to be written like a pure function (or _view_) on the state, where user |
| 44 |
actions are just state updater callbacks. Each time the state is modified, the |
| 45 |
entire application rendering is recomputed, and **React** computes a minimal |
| 46 |
diff to apply on the web page displayed in the main application window. |
| 47 |
|
| 48 |
Putting everything together, its is recommended to design **Dome** application |
| 49 |
into two different parts, both running in the `Renderer` process of each |
| 50 |
application instance: |
| 51 |
|
| 52 |
- **Application Internals (State)** |
| 53 |
holding your application state and data and updating it in response to user or |
| 54 |
system events; |
| 55 |
|
| 56 |
- **Application Components (View)** |
| 57 |
responsible for rendering the application main window and |
| 58 |
binding callbacks to application services. |
| 59 |
|
| 60 |
<img src="dataflow.png" style="float: right; width: 30em"/> |
| 61 |
|
| 62 |
Such an architecture is typical of a _Model-View-Controller_ design, but |
| 63 |
revisited to scale. In particular, data flow between those three different |
| 64 |
layers shall follow a unique-direction pattern in order to avoid the |
| 65 |
combinatorial explosion of interactions between components that is generally |
| 66 |
observed in most Model-View-Controller designs. |
| 67 |
|
| 68 |
Hence, data-flow shall follow one of the following routes between these |
| 69 |
three layers, illustrated above: |
| 70 |
|
| 71 |
- from `State` to `View` : your rendering components shall only access the |
| 72 |
current application state and data; |
| 73 |
- from `View` to `State` or `System` : user action callbacks shall only trigger |
| 74 |
state update or system operation, not any other view direct modification. |
| 75 |
- from `System` to `State` : upon completion, system services shall trigger state |
| 76 |
updates that would in turn trigger new requests and view re-rendering. |
| 77 |
|
| 78 |
## Data Management |
| 79 |
|
| 80 |
To implement the recommended data-flow described above, you may use a full |
| 81 |
featured [Redux](https://redux.js.org) platform or any other framework of your |
| 82 |
choice. However, **Dome** provides you with many facilities for implementing |
| 83 |
your data flow. |
| 84 |
|
| 85 |
- **Global States** are necessary to implement the unidirectional data-flow. These |
| 86 |
data are stored in the renderer process, but outside of the view hierarchy of |
| 87 |
**React** components actually mounted in the DOM. Hence, it remains consistent whatever |
| 88 |
the evolution of the view. See `dome/state` module and the associated custom **React** hooks |
| 89 |
to implement global states. You may also use global JavaScript variables and emit events |
| 90 |
on your own. |
| 91 |
|
| 92 |
- **View Updates** to make your views listening for updates of the various data |
| 93 |
sources, we encourage to use the **React** hooks we provide, since they |
| 94 |
transparently make your components to re-render when required. However, |
| 95 |
sometimes you will need to respond to special events by hand. For this purpose, |
| 96 |
you can use **Dome** as a central event emitter, by using `Dome.emit()`, |
| 97 |
`Dome.on()` and `Dome.off()` functions. Moreover, the `Dome.useUpdate()` and |
| 98 |
`Dome.useEvent()` hooks can be used to make your components being notified by |
| 99 |
events. |
| 100 |
|
| 101 |
- **Window Settings** are stored a local file at the root of user's project, and |
| 102 |
remains generally unnoticed by most users. They typically include the window's |
| 103 |
position and dimension, resizable items position, fold/unfold states, |
| 104 |
presentation options, etc. Most |
| 105 |
**Dome** components with presentation options can be assigned a `settings` key |
| 106 |
to make their state persistent. Contrary to Global Settings, however, they are |
| 107 |
not shared across several windows. You may also access these data by using |
| 108 |
`Settings.setWindowSetting()` and `Settings.getWindowSetting()`, or the **React** hook |
| 109 |
`Settings.useWindowSetting()`. See also helpers `Dome.useXxxSettings()`. |
| 110 |
It is possible, from the application main menu, to reset all the window settings to their |
| 111 |
default values. |
| 112 |
|
| 113 |
- **Local Storage** are stored in the same file than window settings, although |
| 114 |
they are not automatically reset to their initial values. |
| 115 |
This is very convenient to store persistent user data on a per-project basis. |
| 116 |
See `Settings.xxxLocalStorage()` functions for more details. |
| 117 |
|
| 118 |
- **Global Settings** are stored in the user's home directory and automatically |
| 119 |
saved and load with your application; they are typically modified _via_ the |
| 120 |
Settings Window, which is accessible from the application menubar. In **Dome**, |
| 121 |
you can shall define a global settings by creating an instance of |
| 122 |
`Settings.GlobalSettings` class and use it with |
| 123 |
the `Settings.useGlobalSettings()` hook. |