diff options
author | 2022-01-27 00:17:22 +0100 | |
---|---|---|
committer | 2022-02-08 21:43:17 +0100 | |
commit | 9546dc2aa39ab096ccc723786e718a739d0bdaf9 (patch) | |
tree | 9c3afc6155cc59f6dd1235397230aaa15a5f8cec /packages/preload/src/contextBridge | |
parent | refactor: Apply shared store patches in batches (diff) | |
download | sophie-9546dc2aa39ab096ccc723786e718a739d0bdaf9.tar.gz sophie-9546dc2aa39ab096ccc723786e718a739d0bdaf9.tar.zst sophie-9546dc2aa39ab096ccc723786e718a739d0bdaf9.zip |
refactor: Coding conventions
Make sure that files have a default import with the same name as the
file whenever possible to reduce surprise.
Also shuffles around some file names for better legibility.
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages/preload/src/contextBridge')
-rw-r--r-- | packages/preload/src/contextBridge/createSophieRenderer.ts | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/packages/preload/src/contextBridge/createSophieRenderer.ts b/packages/preload/src/contextBridge/createSophieRenderer.ts index 6003c8b..8bdf07e 100644 --- a/packages/preload/src/contextBridge/createSophieRenderer.ts +++ b/packages/preload/src/contextBridge/createSophieRenderer.ts | |||
@@ -20,7 +20,6 @@ | |||
20 | 20 | ||
21 | import { | 21 | import { |
22 | Action, | 22 | Action, |
23 | action, | ||
24 | MainToRendererIpcMessage, | 23 | MainToRendererIpcMessage, |
25 | RendererToMainIpcMessage, | 24 | RendererToMainIpcMessage, |
26 | SharedStoreListener, | 25 | SharedStoreListener, |
@@ -32,30 +31,33 @@ import log from 'loglevel'; | |||
32 | import type { IJsonPatch } from 'mobx-state-tree'; | 31 | import type { IJsonPatch } from 'mobx-state-tree'; |
33 | 32 | ||
34 | class SharedStoreConnector { | 33 | class SharedStoreConnector { |
35 | private onSharedStoreChangeCalled = false; | 34 | readonly #allowReplaceListener: boolean; |
36 | 35 | ||
37 | private listener: SharedStoreListener | undefined; | 36 | #onSharedStoreChangeCalled = false; |
38 | 37 | ||
39 | constructor(private readonly allowReplaceListener: boolean) { | 38 | #listener: SharedStoreListener | undefined; |
39 | |||
40 | constructor(allowReplaceListener: boolean) { | ||
41 | this.#allowReplaceListener = allowReplaceListener; | ||
40 | ipcRenderer.on( | 42 | ipcRenderer.on( |
41 | MainToRendererIpcMessage.SharedStorePatch, | 43 | MainToRendererIpcMessage.SharedStorePatch, |
42 | (_event, patch) => { | 44 | (_event, patch) => { |
43 | try { | 45 | try { |
44 | // `mobx-state-tree` will validate the patch, so we can safely cast here. | 46 | // `mobx-state-tree` will validate the patch, so we can safely cast here. |
45 | this.listener?.onPatch(patch as IJsonPatch[]); | 47 | this.#listener?.onPatch(patch as IJsonPatch[]); |
46 | } catch (error) { | 48 | } catch (error) { |
47 | log.error('Shared store listener onPatch failed', error); | 49 | log.error('Shared store listener onPatch failed', error); |
48 | this.listener = undefined; | 50 | this.#listener = undefined; |
49 | } | 51 | } |
50 | }, | 52 | }, |
51 | ); | 53 | ); |
52 | } | 54 | } |
53 | 55 | ||
54 | async onSharedStoreChange(listener: SharedStoreListener): Promise<void> { | 56 | async onSharedStoreChange(listener: SharedStoreListener): Promise<void> { |
55 | if (this.onSharedStoreChangeCalled && !this.allowReplaceListener) { | 57 | if (this.#onSharedStoreChangeCalled && !this.#allowReplaceListener) { |
56 | throw new Error('Shared store change listener was already set'); | 58 | throw new Error('Shared store change listener was already set'); |
57 | } | 59 | } |
58 | this.onSharedStoreChangeCalled = true; | 60 | this.#onSharedStoreChangeCalled = true; |
59 | let success = false; | 61 | let success = false; |
60 | let snapshot: unknown; | 62 | let snapshot: unknown; |
61 | try { | 63 | try { |
@@ -71,14 +73,14 @@ class SharedStoreConnector { | |||
71 | } | 73 | } |
72 | // `mobx-state-tree` will validate the snapshot, so we can safely cast here. | 74 | // `mobx-state-tree` will validate the snapshot, so we can safely cast here. |
73 | listener.onSnapshot(snapshot as SharedStoreSnapshotIn); | 75 | listener.onSnapshot(snapshot as SharedStoreSnapshotIn); |
74 | this.listener = listener; | 76 | this.#listener = listener; |
75 | } | 77 | } |
76 | } | 78 | } |
77 | 79 | ||
78 | function dispatchAction(actionToDispatch: Action): void { | 80 | function dispatchAction(actionToDispatch: Action): void { |
79 | // Let the full zod parse error bubble up to the main world, | 81 | // Let the full zod parse error bubble up to the main world, |
80 | // since all data it may contain was provided from the main world. | 82 | // since all data it may contain was provided from the main world. |
81 | const parsedAction = action.parse(actionToDispatch); | 83 | const parsedAction = Action.parse(actionToDispatch); |
82 | try { | 84 | try { |
83 | ipcRenderer.send(RendererToMainIpcMessage.DispatchAction, parsedAction); | 85 | ipcRenderer.send(RendererToMainIpcMessage.DispatchAction, parsedAction); |
84 | } catch (error) { | 86 | } catch (error) { |