aboutsummaryrefslogtreecommitdiffstats
path: root/packages/preload/src/SophieRendererImpl.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/preload/src/SophieRendererImpl.ts')
-rw-r--r--packages/preload/src/SophieRendererImpl.ts112
1 files changed, 0 insertions, 112 deletions
diff --git a/packages/preload/src/SophieRendererImpl.ts b/packages/preload/src/SophieRendererImpl.ts
deleted file mode 100644
index a06433f..0000000
--- a/packages/preload/src/SophieRendererImpl.ts
+++ /dev/null
@@ -1,112 +0,0 @@
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 { ipcRenderer } from 'electron';
22import type { IJsonPatch } from 'mobx-state-tree';
23import {
24 BrowserViewBounds,
25 browserViewBounds,
26 MainToRendererIpcMessage,
27 RendererToMainIpcMessage,
28 sharedStore,
29 SharedStoreListener,
30 SharedStoreSnapshotIn,
31 SophieRenderer,
32 themeSource,
33 ThemeSource,
34} from '@sophie/shared';
35
36export type MessageSender = (channel: RendererToMainIpcMessage, ...args: unknown[]) => void;
37
38export class SophieRendererImpl implements SophieRenderer {
39 readonly #send: MessageSender;
40
41 #listener: SharedStoreListener | null = null;
42
43 #snapshot: SharedStoreSnapshotIn | null = null;
44
45 constructor(send: MessageSender) {
46 this.#send = send;
47 }
48
49 sharedStoreSnapshotReceived(snapshot: unknown): void {
50 if (sharedStore.is(snapshot)) {
51 if (this.#listener === null) {
52 this.#snapshot = snapshot;
53 } else {
54 this.#listener.onSnapshot(snapshot);
55 }
56 } else {
57 console.error('Received invalid snapshot', snapshot);
58 this.#snapshot = null;
59 }
60 }
61
62 sharedStorePatchReceived(patch: unknown): void {
63 if (this.#listener !== null) {
64 // `mobx-state-tree` will validate the patch, so we can safely cast here.
65 this.#listener.onPatch(patch as IJsonPatch);
66 }
67 }
68
69 setSharedStoreListener(listener: SharedStoreListener): void {
70 this.#listener = listener;
71 if (this.#snapshot !== null) {
72 listener.onSnapshot(this.#snapshot);
73 this.#snapshot = null;
74 }
75 this.#send(RendererToMainIpcMessage.SharedStoreSnapshotRequest);
76 }
77
78 setBrowserViewBounds(bounds: BrowserViewBounds): void {
79 if (browserViewBounds.safeParse(bounds).success) {
80 this.#send(RendererToMainIpcMessage.SetBrowserViewBounds, bounds);
81 }
82 }
83
84 setThemeSource(mode: ThemeSource): void {
85 if (themeSource.safeParse(mode).success) {
86 this.#send(RendererToMainIpcMessage.SetThemeSource, mode);
87 }
88 }
89
90 reloadAllServices(): void {
91 this.#send(RendererToMainIpcMessage.ReloadAllServices);
92 }
93}
94
95export function createSophieRenderer(): SophieRenderer {
96 const impl = new SophieRendererImpl(ipcRenderer.send);
97
98 ipcRenderer.on(MainToRendererIpcMessage.SharedStoreSnapshot, (_event, snapshot) => {
99 impl.sharedStoreSnapshotReceived(snapshot);
100 });
101
102 ipcRenderer.on(MainToRendererIpcMessage.SharedStorePatch, (_event, patch) => {
103 impl.sharedStorePatchReceived(patch);
104 });
105
106 return {
107 setSharedStoreListener: impl.setSharedStoreListener.bind(impl),
108 setBrowserViewBounds: impl.setBrowserViewBounds.bind(impl),
109 setThemeSource: impl.setThemeSource.bind(impl),
110 reloadAllServices: impl.reloadAllServices.bind(impl),
111 };
112}