diff options
author | 2021-12-23 13:40:47 +0100 | |
---|---|---|
committer | 2021-12-23 15:48:30 +0100 | |
commit | 950fb9be8061e2a26e0536b98c6a3ee230618f54 (patch) | |
tree | b136dcc9add0d268a2e7a6288ec934a86d03b652 /packages/renderer/src/devTools.ts | |
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/devTools.ts')
-rw-r--r-- | packages/renderer/src/devTools.ts | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/packages/renderer/src/devTools.ts b/packages/renderer/src/devTools.ts new file mode 100644 index 0000000..5930c48 --- /dev/null +++ b/packages/renderer/src/devTools.ts | |||
@@ -0,0 +1,37 @@ | |||
1 | import type { IAnyStateTreeNode } from 'mobx-state-tree'; | ||
2 | |||
3 | /** | ||
4 | * Connects the `model` to the redux devtools extension after loading the required | ||
5 | * dependencies asynchronously. | ||
6 | * | ||
7 | * We have to apply a workaround to load `remotedev` by shimming the `global` object, | ||
8 | * because `remotedev` uses an old version of `socketcluster-client` that refers to | ||
9 | * `global` instead of `globalThis`. | ||
10 | * | ||
11 | * Due to the old dependencies, this function is not safe to call in production. | ||
12 | * However, we don't bundle `remotedev` in production, so the call would fail anyways. | ||
13 | * | ||
14 | * @param model The store to connect to the redux devtools. | ||
15 | * @see https://github.com/SocketCluster/socketcluster-client/issues/118#issuecomment-469064682 | ||
16 | */ | ||
17 | async function exposeToReduxDevtoolsAsync(model: IAnyStateTreeNode): Promise<void> { | ||
18 | (window as { global?: unknown }).global = window; | ||
19 | |||
20 | const [remotedev, { connectReduxDevtools }] = await Promise.all([ | ||
21 | // @ts-ignore | ||
22 | import('remotedev'), | ||
23 | import('mst-middlewares'), | ||
24 | ]); | ||
25 | connectReduxDevtools(remotedev, model); | ||
26 | } | ||
27 | |||
28 | /** | ||
29 | * Connects the `model` to the redux devtools extension. | ||
30 | * | ||
31 | * @param model The store to connect to the redux devtools. | ||
32 | */ | ||
33 | export function exposeToReduxDevtools(model: IAnyStateTreeNode): void { | ||
34 | exposeToReduxDevtoolsAsync(model).catch((err) => { | ||
35 | console.error('Could not connect to Redux devtools', err); | ||
36 | }); | ||
37 | } | ||