diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-12-23 13:40:47 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-12-23 15:48:30 +0100 |
commit | 950fb9be8061e2a26e0536b98c6a3ee230618f54 (patch) | |
tree | b136dcc9add0d268a2e7a6288ec934a86d03b652 /packages/renderer/src/index.tsx | |
parent | feat: Add shared package for electron ipc (diff) | |
download | sophie-950fb9be8061e2a26e0536b98c6a3ee230618f54.tar.gz sophie-950fb9be8061e2a26e0536b98c6a3ee230618f54.tar.zst sophie-950fb9be8061e2a26e0536b98c6a3ee230618f54.zip |
feat: Main to renderer store synchronization
Patches are send in one direction only, from the main to the renderer,
so all actions have to go through the context bridge and the renderer
IPC to modify the store in the renderer. This makes the store in the
main process a single source of truth, which simplifies debugging and
state persistence.
The store in the renderer is connected to redux devtools for inspection,
but playing back the state in the devtools won't change the sotre in
main process.
Diffstat (limited to 'packages/renderer/src/index.tsx')
-rw-r--r-- | packages/renderer/src/index.tsx | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/packages/renderer/src/index.tsx b/packages/renderer/src/index.tsx index 452448c..37daaa6 100644 --- a/packages/renderer/src/index.tsx +++ b/packages/renderer/src/index.tsx | |||
@@ -2,6 +2,7 @@ import '@fontsource/roboto/300.css'; | |||
2 | import '@fontsource/roboto/400.css'; | 2 | import '@fontsource/roboto/400.css'; |
3 | import '@fontsource/roboto/500.css'; | 3 | import '@fontsource/roboto/500.css'; |
4 | import '@fontsource/roboto/700.css'; | 4 | import '@fontsource/roboto/700.css'; |
5 | import { applyPatch, applySnapshot } from 'mobx-state-tree'; | ||
5 | import Button from "@mui/material/Button"; | 6 | import Button from "@mui/material/Button"; |
6 | import CssBaseline from "@mui/material/CssBaseline"; | 7 | import CssBaseline from "@mui/material/CssBaseline"; |
7 | import { | 8 | import { |
@@ -10,6 +11,26 @@ import { | |||
10 | } from '@mui/material/styles'; | 11 | } from '@mui/material/styles'; |
11 | import React from 'react'; | 12 | import React from 'react'; |
12 | import { render } from 'react-dom'; | 13 | import { render } from 'react-dom'; |
14 | import { sharedStore } from '@sophie/shared'; | ||
15 | |||
16 | import { exposeToReduxDevtools } from './devTools'; | ||
17 | |||
18 | const isDevelopment = import.meta.env.MODE === 'development'; | ||
19 | |||
20 | const store = sharedStore.create(); | ||
21 | |||
22 | if (isDevelopment) { | ||
23 | exposeToReduxDevtools(store); | ||
24 | } | ||
25 | |||
26 | window.sophieRenderer.setSharedStoreListener({ | ||
27 | onSnapshot(snapshot) { | ||
28 | applySnapshot(store, snapshot); | ||
29 | }, | ||
30 | onPatch(patch) { | ||
31 | applyPatch(store, patch); | ||
32 | }, | ||
33 | }); | ||
13 | 34 | ||
14 | const theme = createTheme({ | 35 | const theme = createTheme({ |
15 | palette: { | 36 | palette: { |
@@ -24,7 +45,7 @@ function App() { | |||
24 | <CssBaseline enableColorScheme /> | 45 | <CssBaseline enableColorScheme /> |
25 | <Button | 46 | <Button |
26 | variant="contained" | 47 | variant="contained" |
27 | onClick={window.sophieRenderer.buttonClicked} | 48 | onClick={window.sophieRenderer.buttonClick} |
28 | > | 49 | > |
29 | Hello Sophie! | 50 | Hello Sophie! |
30 | </Button> | 51 | </Button> |