From 11cf57ce92aff4dcdb504194ea5c381ac400c5c5 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Mon, 27 Dec 2021 02:24:22 +0100 Subject: refactor: Simplify IpcRendererService and its spec --- .../src/contextBridge/SophieRendererImpl.ts | 13 ++++--- .../__tests__/SophieRendererImpl.spec.ts | 15 ++++---- packages/preload/src/index.ts | 3 +- .../preload/src/services/IpcRendererService.ts | 42 ++++++++++++++++++++++ .../src/services/RendererToMainIpcService.ts | 42 ---------------------- 5 files changed, 59 insertions(+), 56 deletions(-) create mode 100644 packages/preload/src/services/IpcRendererService.ts delete mode 100644 packages/preload/src/services/RendererToMainIpcService.ts (limited to 'packages') diff --git a/packages/preload/src/contextBridge/SophieRendererImpl.ts b/packages/preload/src/contextBridge/SophieRendererImpl.ts index 61b01e9..bbb4f65 100644 --- a/packages/preload/src/contextBridge/SophieRendererImpl.ts +++ b/packages/preload/src/contextBridge/SophieRendererImpl.ts @@ -27,16 +27,17 @@ import { SophieRenderer, } from '@sophie/shared'; -import { RendererToMainIpcService } from '../services/RendererToMainIpcService'; +import { IpcRendererService } from '../services/IpcRendererService'; class SophieRendererImpl implements SophieRenderer { - readonly #ipcService = new RendererToMainIpcService(); + readonly #ipcService; #onSharedStoreChangeCalled: boolean = false; #listener: SharedStoreListener | null = null; - constructor() { + constructor(ipcService: IpcRendererService) { + this.#ipcService = ipcService; this.#ipcService.onSharedStorePatch((patch) => { try { // `mobx-state-tree` will validate the patch, so we can safely cast here. @@ -87,8 +88,10 @@ class SophieRendererImpl implements SophieRenderer { } } -export function createSophieRenderer(): SophieRenderer { - const impl = new SophieRendererImpl(); +export function createSophieRenderer( + ipcService: IpcRendererService, +): SophieRenderer { + const impl = new SophieRendererImpl(ipcService); return { onSharedStoreChange: impl.onSharedStoreChange.bind(impl), dispatchAction: impl.dispatchAction.bind(impl), diff --git a/packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts b/packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts index c0e5ec2..bd2b1d5 100644 --- a/packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts +++ b/packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts @@ -22,10 +22,10 @@ import { mocked } from 'jest-mock'; import type { IJsonPatch } from 'mobx-state-tree'; import type { Action, SharedStoreSnapshotIn, SophieRenderer } from '@sophie/shared'; -import { RendererToMainIpcService } from '../../services/RendererToMainIpcService'; +import { IpcRendererService } from '../../services/IpcRendererService'; import { createSophieRenderer } from '../SophieRendererImpl'; -jest.mock('../../services/RendererToMainIpcService'); +jest.mock('../../services/IpcRendererService'); const snapshot: SharedStoreSnapshotIn = { shouldUseDarkColors: true, @@ -52,16 +52,15 @@ const invalidAction = { describe('constructor', () => { it('registers a shared store patch listener', () => { - createSophieRenderer(); - expect(RendererToMainIpcService).toHaveBeenCalledTimes(1); - const service = mocked(RendererToMainIpcService).mock.instances[0]; + const service = new IpcRendererService(); + createSophieRenderer(service); expect(service.onSharedStorePatch).toHaveBeenCalledTimes(1); }); }); describe('instance', () => { let sut: SophieRenderer; - let service: RendererToMainIpcService; + let service: IpcRendererService; let onSharedStorePatch: (patch: unknown) => void; let listener = { onSnapshot: jest.fn((_snapshot: SharedStoreSnapshotIn) => {}), @@ -69,8 +68,8 @@ describe('instance', () => { }; beforeEach(() => { - sut = createSophieRenderer(); - service = mocked(RendererToMainIpcService).mock.instances[0]; + service = new IpcRendererService(); + sut = createSophieRenderer(service); onSharedStorePatch = mocked(service.onSharedStorePatch).mock.calls[0][0]; }); diff --git a/packages/preload/src/index.ts b/packages/preload/src/index.ts index ef466b4..2703d40 100644 --- a/packages/preload/src/index.ts +++ b/packages/preload/src/index.ts @@ -21,7 +21,8 @@ import { contextBridge } from 'electron'; import { createSophieRenderer } from './contextBridge/SophieRendererImpl'; +import { IpcRendererService } from './services/IpcRendererService'; -const sophieRenderer = createSophieRenderer(); +const sophieRenderer = createSophieRenderer(new IpcRendererService()); contextBridge.exposeInMainWorld('sophieRenderer', sophieRenderer); diff --git a/packages/preload/src/services/IpcRendererService.ts b/packages/preload/src/services/IpcRendererService.ts new file mode 100644 index 0000000..7f51351 --- /dev/null +++ b/packages/preload/src/services/IpcRendererService.ts @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { ipcRenderer } from 'electron'; +import { + Action, + MainToRendererIpcMessage, + RendererToMainIpcMessage, +} from '@sophie/shared'; + +export class IpcRendererService { + getSharedStoreSnapshot(): Promise { + return ipcRenderer.invoke(RendererToMainIpcMessage.GetSharedStoreSnapshot); + } + + dispatchAction(actionToDispatch: Action): void { + ipcRenderer.send(RendererToMainIpcMessage.DispatchAction, actionToDispatch); + } + + onSharedStorePatch(callback: (patch: unknown) => void): void { + ipcRenderer.on(MainToRendererIpcMessage.SharedStorePatch, (_event, patch) => { + callback(patch); + }) + } +} diff --git a/packages/preload/src/services/RendererToMainIpcService.ts b/packages/preload/src/services/RendererToMainIpcService.ts deleted file mode 100644 index 40f1339..0000000 --- a/packages/preload/src/services/RendererToMainIpcService.ts +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2021-2022 Kristóf Marussy - * - * This file is part of Sophie. - * - * Sophie is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * SPDX-License-Identifier: AGPL-3.0-only - */ - -import { ipcRenderer } from 'electron'; -import { - Action, - MainToRendererIpcMessage, - RendererToMainIpcMessage, -} from '@sophie/shared'; - -export class RendererToMainIpcService { - getSharedStoreSnapshot(): Promise { - return ipcRenderer.invoke(RendererToMainIpcMessage.GetSharedStoreSnapshot); - } - - dispatchAction(actionToDispatch: Action): void { - ipcRenderer.send(RendererToMainIpcMessage.DispatchAction, actionToDispatch); - } - - onSharedStorePatch(callback: (patch: unknown) => void): void { - ipcRenderer.on(MainToRendererIpcMessage.SharedStorePatch, (_event, patch) => { - callback(patch); - }) - } -} -- cgit v1.2.3-70-g09d2