aboutsummaryrefslogtreecommitdiffstats
path: root/packages/preload/src/contextBridge/SophieRendererImpl.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/preload/src/contextBridge/SophieRendererImpl.ts')
-rw-r--r--packages/preload/src/contextBridge/SophieRendererImpl.ts74
1 files changed, 74 insertions, 0 deletions
diff --git a/packages/preload/src/contextBridge/SophieRendererImpl.ts b/packages/preload/src/contextBridge/SophieRendererImpl.ts
new file mode 100644
index 0000000..5d29071
--- /dev/null
+++ b/packages/preload/src/contextBridge/SophieRendererImpl.ts
@@ -0,0 +1,74 @@
1/*
2 * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import type { IJsonPatch } from 'mobx-state-tree';
22import {
23 Action,
24 action,
25 sharedStore,
26 SharedStoreListener,
27 SophieRenderer,
28} from '@sophie/shared';
29
30import { RendererToMainIpcService } from '../services/RendererToMainIpcService';
31
32class SophieRendererImpl implements SophieRenderer {
33 readonly #ipcService: RendererToMainIpcService;
34
35 #listener: SharedStoreListener | null = null;
36
37 constructor(ipcService: RendererToMainIpcService) {
38 this.#ipcService = ipcService;
39 ipcService.onSharedStorePatch((patch) => {
40 // `mobx-state-tree` will validate the patch, so we can safely cast here.
41 this.#listener?.onPatch(patch as IJsonPatch);
42 });
43 }
44
45 onSharedStoreChange(listener: SharedStoreListener): void {
46 this.#ipcService.getSharedStoreSnapshot().then((snapshot) => {
47 if (sharedStore.is(snapshot)) {
48 listener.onSnapshot(snapshot);
49 this.#listener = listener;
50 } else {
51 console.error('Got invalid initial shared store snapshot', snapshot);
52 }
53 }).catch((err) => {
54 console.error('Failed set initial shared store snapshot', err);
55 });
56 }
57
58 dispatchAction(actionToDispatch: Action): void {
59 const parsedAction = action.safeParse(actionToDispatch);
60 if (parsedAction.success) {
61 this.#ipcService.dispatchAction(parsedAction.data);
62 } else {
63 console.error('Trying to dispatch invalid action', actionToDispatch, parsedAction.error);
64 }
65 }
66}
67
68export function createSophieRenderer(ipcService: RendererToMainIpcService): SophieRenderer {
69 const impl = new SophieRendererImpl(ipcService);
70 return {
71 onSharedStoreChange: impl.onSharedStoreChange.bind(impl),
72 dispatchAction: impl.dispatchAction.bind(impl),
73 };
74}