aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/index.ts
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-01-25 17:56:28 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-02-08 21:43:17 +0100
commitc5f213df7b0d207667692395738c92c01f7e0837 (patch)
tree9a1220f67ea71df13bd01482eff0f0dc4c8a8c8a /packages/main/src/index.ts
parentrefactor: Store services in a map (diff)
downloadsophie-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.ts18
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 {
36import { app, BrowserView, BrowserWindow, ipcMain } from 'electron'; 36import { app, BrowserView, BrowserWindow, ipcMain } from 'electron';
37import { ensureDirSync, readFile, readFileSync } from 'fs-extra'; 37import { ensureDirSync, readFile, readFileSync } from 'fs-extra';
38import { autorun } from 'mobx'; 38import { autorun } from 'mobx';
39import { getSnapshot, onPatch } from 'mobx-state-tree'; 39import { getSnapshot, onAction, onPatch } from 'mobx-state-tree';
40import osName from 'os-name'; 40import osName from 'os-name';
41 41
42import { 42import {
@@ -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) {