diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-01-25 17:56:28 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2022-02-08 21:43:17 +0100 |
commit | c5f213df7b0d207667692395738c92c01f7e0837 (patch) | |
tree | 9a1220f67ea71df13bd01482eff0f0dc4c8a8c8a /packages/main/src/index.ts | |
parent | refactor: Store services in a map (diff) | |
download | sophie-c5f213df7b0d207667692395738c92c01f7e0837.tar.gz sophie-c5f213df7b0d207667692395738c92c01f7e0837.tar.zst sophie-c5f213df7b0d207667692395738c92c01f7e0837.zip |
refactor: Apply shared store patches in batches
Makes sure that the renderer always sees a consistent state.
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages/main/src/index.ts')
-rw-r--r-- | packages/main/src/index.ts | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts index 0978cd9..bcdc3d7 100644 --- a/packages/main/src/index.ts +++ b/packages/main/src/index.ts | |||
@@ -36,7 +36,7 @@ import { | |||
36 | import { app, BrowserView, BrowserWindow, ipcMain } from 'electron'; | 36 | import { app, BrowserView, BrowserWindow, ipcMain } from 'electron'; |
37 | import { ensureDirSync, readFile, readFileSync } from 'fs-extra'; | 37 | import { ensureDirSync, readFile, readFileSync } from 'fs-extra'; |
38 | import { autorun } from 'mobx'; | 38 | import { autorun } from 'mobx'; |
39 | import { getSnapshot, onPatch } from 'mobx-state-tree'; | 39 | import { getSnapshot, onAction, onPatch } from 'mobx-state-tree'; |
40 | import osName from 'os-name'; | 40 | import osName from 'os-name'; |
41 | 41 | ||
42 | import { | 42 | import { |
@@ -292,9 +292,23 @@ async function createWindow(): Promise<unknown> { | |||
292 | } | 292 | } |
293 | }); | 293 | }); |
294 | 294 | ||
295 | const batchedPatches: unknown[] = []; | ||
295 | onPatch(store.shared, (patch) => { | 296 | onPatch(store.shared, (patch) => { |
296 | webContents.send(MainToRendererIpcMessage.SharedStorePatch, patch); | 297 | batchedPatches.push(patch); |
297 | }); | 298 | }); |
299 | onAction( | ||
300 | store, | ||
301 | () => { | ||
302 | if (batchedPatches.length > 0) { | ||
303 | webContents.send( | ||
304 | MainToRendererIpcMessage.SharedStorePatch, | ||
305 | batchedPatches, | ||
306 | ); | ||
307 | batchedPatches.splice(0); | ||
308 | } | ||
309 | }, | ||
310 | true, | ||
311 | ); | ||
298 | 312 | ||
299 | ipcMain.handle(ServiceToMainIpcMessage.ApiExposedInMainWorld, (event) => { | 313 | ipcMain.handle(ServiceToMainIpcMessage.ApiExposedInMainWorld, (event) => { |
300 | if (event.sender.id !== browserView.webContents.id) { | 314 | if (event.sender.id !== browserView.webContents.id) { |