aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/infrastructure/electron/RendererBridge.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/infrastructure/electron/RendererBridge.ts')
-rw-r--r--packages/main/src/infrastructure/electron/RendererBridge.ts39
1 files changed, 11 insertions, 28 deletions
diff --git a/packages/main/src/infrastructure/electron/RendererBridge.ts b/packages/main/src/infrastructure/electron/RendererBridge.ts
index 8633b9d..921f09a 100644
--- a/packages/main/src/infrastructure/electron/RendererBridge.ts
+++ b/packages/main/src/infrastructure/electron/RendererBridge.ts
@@ -22,16 +22,12 @@ import type { SharedStoreSnapshotOut } from '@sophie/shared';
22import { 22import {
23 addMiddleware, 23 addMiddleware,
24 getSnapshot, 24 getSnapshot,
25 IJsonPatch, 25 type IJsonPatch,
26 IMiddlewareEvent,
27 onPatch, 26 onPatch,
28} from 'mobx-state-tree'; 27} from 'mobx-state-tree';
29 28
30import type MainStore from '../../stores/MainStore'; 29import type MainStore from '../../stores/MainStore.js';
31import Disposer from '../../utils/Disposer'; 30import Disposer from '../../utils/Disposer.js';
32import { getLogger } from '../../utils/log';
33
34const log = getLogger('RendererBridge');
35 31
36export type PatchListener = (patch: IJsonPatch[]) => void; 32export type PatchListener = (patch: IJsonPatch[]) => void;
37 33
@@ -45,41 +41,28 @@ export default class RendererBridge {
45 constructor(store: MainStore, listener: PatchListener) { 41 constructor(store: MainStore, listener: PatchListener) {
46 this.snapshot = getSnapshot(store.shared); 42 this.snapshot = getSnapshot(store.shared);
47 43
48 // The call for the currently pending action, if any.
49 let topLevelCall: IMiddlewareEvent | undefined;
50 // An array of accumulated patches if we're in an action, `undefined` otherwise. 44 // An array of accumulated patches if we're in an action, `undefined` otherwise.
51 let patches: IJsonPatch[] | undefined; 45 let patches: IJsonPatch[] | undefined;
52 46
53 this.disposeOnPatch = onPatch(store.shared, (patch) => { 47 this.disposeOnPatch = onPatch(store.shared, (patch) => {
54 if (patches === undefined) { 48 if (patches === undefined) {
55 // Update unprotected stores (outside an action) right away. 49 // Update unprotected stores (outside an action) right away.
56 listener([patch]); 50 try {
51 listener([patch]);
52 } finally {
53 this.snapshot = getSnapshot(store.shared);
54 }
57 } else { 55 } else {
58 patches.push(patch); 56 patches.push(patch);
59 } 57 }
60 }); 58 });
61 59
62 this.disposeMiddleware = addMiddleware(store, (call, next, abort) => { 60 this.disposeMiddleware = addMiddleware(store, (call, next) => {
63 if (call.parentActionEvent !== undefined) { 61 if (patches !== undefined) {
64 // We're already in an action, there's no need to enter one. 62 // We're already in an action, there's no need to enter one.
65 next(call); 63 next(call);
66 return; 64 return;
67 } 65 }
68 if (patches !== undefined) {
69 log.error(
70 'Unexpected call',
71 call,
72 'during dispatching another call',
73 topLevelCall,
74 'with accumulated patches',
75 patches,
76 );
77 abort(undefined);
78 return;
79 }
80 // Make shure that the saved snapshot is consistent with the patches we're going to send.
81 this.snapshot = getSnapshot(store.shared);
82 topLevelCall = call;
83 patches = []; 66 patches = [];
84 try { 67 try {
85 next(call); 68 next(call);
@@ -89,8 +72,8 @@ export default class RendererBridge {
89 listener(patches); 72 listener(patches);
90 } 73 }
91 } finally { 74 } finally {
92 topLevelCall = undefined;
93 patches = undefined; 75 patches = undefined;
76 this.snapshot = getSnapshot(store.shared);
94 } 77 }
95 } 78 }
96 }); 79 });