diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-04-05 02:18:56 +0200 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2022-05-16 00:55:00 +0200 |
commit | 7cea467cab1d72869bd1dcd6a430540871a0bdd2 (patch) | |
tree | 97a3da2a9067d556992b9f046684c3113075bee4 /packages/renderer/src | |
parent | build: Properly shut down watch mode (diff) | |
download | sophie-7cea467cab1d72869bd1dcd6a430540871a0bdd2.tar.gz sophie-7cea467cab1d72869bd1dcd6a430540871a0bdd2.tar.zst sophie-7cea467cab1d72869bd1dcd6a430540871a0bdd2.zip |
fix(renderer): Consistent store initialization
Make sure the RendererStore is in a state consistent with the MainStore
before attempting to initialize the application.
This avoids, e.g., race conditions when trying to load the default
locale spuriously before loading the actually selected locale.
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages/renderer/src')
-rw-r--r-- | packages/renderer/src/index.tsx | 2 | ||||
-rw-r--r-- | packages/renderer/src/stores/RendererStore.ts | 15 |
2 files changed, 9 insertions, 8 deletions
diff --git a/packages/renderer/src/index.tsx b/packages/renderer/src/index.tsx index 60ef714..a408c4d 100644 --- a/packages/renderer/src/index.tsx +++ b/packages/renderer/src/index.tsx | |||
@@ -46,7 +46,7 @@ if (isDevelopment) { | |||
46 | 46 | ||
47 | const { sophieRenderer: ipc } = window; | 47 | const { sophieRenderer: ipc } = window; |
48 | 48 | ||
49 | const store = createAndConnectRendererStore(ipc); | 49 | const store = await createAndConnectRendererStore(ipc); |
50 | 50 | ||
51 | if (isDevelopment) { | 51 | if (isDevelopment) { |
52 | exposeToReduxDevtools(store).catch((error) => { | 52 | exposeToReduxDevtools(store).catch((error) => { |
diff --git a/packages/renderer/src/stores/RendererStore.ts b/packages/renderer/src/stores/RendererStore.ts index f48d484..7052162 100644 --- a/packages/renderer/src/stores/RendererStore.ts +++ b/packages/renderer/src/stores/RendererStore.ts | |||
@@ -66,27 +66,28 @@ export default RendererStore; | |||
66 | * the newly created store via `ipc`. | 66 | * the newly created store via `ipc`. |
67 | * | 67 | * |
68 | * @param ipc The `sophieRenderer` context bridge. | 68 | * @param ipc The `sophieRenderer` context bridge. |
69 | * @returns A promise that resolves to the store once it was initialized. | ||
69 | */ | 70 | */ |
70 | export function createAndConnectRendererStore( | 71 | export async function createAndConnectRendererStore( |
71 | ipc: SophieRenderer, | 72 | ipc: SophieRenderer, |
72 | ): RendererStore { | 73 | ): Promise<RendererStore> { |
73 | const env: RendererEnv = { | 74 | const env: RendererEnv = { |
74 | dispatchMainAction: ipc.dispatchAction, | 75 | dispatchMainAction: ipc.dispatchAction, |
75 | }; | 76 | }; |
76 | const store = RendererStore.create({}, env); | 77 | const store = RendererStore.create({}, env); |
77 | 78 | ||
78 | ipc | 79 | try { |
79 | .onSharedStoreChange({ | 80 | await ipc.onSharedStoreChange({ |
80 | onSnapshot(snapshot) { | 81 | onSnapshot(snapshot) { |
81 | applySnapshot(store.shared, snapshot); | 82 | applySnapshot(store.shared, snapshot); |
82 | }, | 83 | }, |
83 | onPatch(patch) { | 84 | onPatch(patch) { |
84 | applyPatch(store.shared, patch); | 85 | applyPatch(store.shared, patch); |
85 | }, | 86 | }, |
86 | }) | ||
87 | .catch((error) => { | ||
88 | log.error('Failed to connect to shared store', error); | ||
89 | }); | 87 | }); |
88 | } catch (error) { | ||
89 | log.error('Failed to connect to shared store', error); | ||
90 | } | ||
90 | 91 | ||
91 | return store; | 92 | return store; |
92 | } | 93 | } |