diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-01-20 18:06:23 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2022-02-08 21:43:15 +0100 |
commit | d24734ac4cd1c6ddda5ba39f033ce9eaa4dcda01 (patch) | |
tree | b696243e5bf35ee19237c149da3d779239e2a7d7 /packages/renderer/src/stores | |
parent | fix: Do not access localStorage in service-preload (diff) | |
download | sophie-d24734ac4cd1c6ddda5ba39f033ce9eaa4dcda01.tar.gz sophie-d24734ac4cd1c6ddda5ba39f033ce9eaa4dcda01.tar.zst sophie-d24734ac4cd1c6ddda5ba39f033ce9eaa4dcda01.zip |
feat: Add RuntimeService store
Stores transient state for services shared between the main and renderer
processes.
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages/renderer/src/stores')
-rw-r--r-- | packages/renderer/src/stores/Config.ts (renamed from packages/renderer/src/stores/RendererEnv.ts) | 27 | ||||
-rw-r--r-- | packages/renderer/src/stores/RendererStore.ts | 21 | ||||
-rw-r--r-- | packages/renderer/src/stores/Service.ts | 38 | ||||
-rw-r--r-- | packages/renderer/src/stores/SharedStore.ts | 35 | ||||
-rw-r--r-- | packages/renderer/src/stores/__tests__/Service.spec.ts | 63 |
5 files changed, 155 insertions, 29 deletions
diff --git a/packages/renderer/src/stores/RendererEnv.ts b/packages/renderer/src/stores/Config.ts index f0a5a51..070c4ec 100644 --- a/packages/renderer/src/stores/RendererEnv.ts +++ b/packages/renderer/src/stores/Config.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com> | 2 | * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com> |
3 | * | 3 | * |
4 | * This file is part of Sophie. | 4 | * This file is part of Sophie. |
5 | * | 5 | * |
@@ -18,20 +18,15 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import type { Action } from '@sophie/shared'; | 21 | import { config as originalConfig } from '@sophie/shared'; |
22 | import { getEnv as getAnyEnv, IAnyStateTreeNode } from 'mobx-state-tree'; | 22 | import { Instance, types } from 'mobx-state-tree'; |
23 | 23 | ||
24 | export default interface RendererEnv { | 24 | import { service } from './Service'; |
25 | dispatchMainAction(action: Action): void; | ||
26 | } | ||
27 | 25 | ||
28 | /** | 26 | export const config = originalConfig.props({ |
29 | * Gets a well-typed environment from `model`. | 27 | services: types.array(service), |
30 | * | 28 | }); |
31 | * Only useable inside state trees created by `createAndConnectRootStore`. | 29 | |
32 | * | 30 | export interface Config extends Instance<typeof config> {} |
33 | * @param model The state tree node. | 31 | |
34 | */ | 32 | export type { ConfigSnapshotIn, ConfigSnapshotOut } from '@sophie/shared'; |
35 | export function getEnv(model: IAnyStateTreeNode): RendererEnv { | ||
36 | return getAnyEnv<RendererEnv>(model); | ||
37 | } | ||
diff --git a/packages/renderer/src/stores/RendererStore.ts b/packages/renderer/src/stores/RendererStore.ts index f1915c9..d0e7843 100644 --- a/packages/renderer/src/stores/RendererStore.ts +++ b/packages/renderer/src/stores/RendererStore.ts | |||
@@ -18,20 +18,16 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { | 21 | import { BrowserViewBounds, SophieRenderer, ThemeSource } from '@sophie/shared'; |
22 | BrowserViewBounds, | ||
23 | Config, | ||
24 | Service, | ||
25 | sharedStore, | ||
26 | SophieRenderer, | ||
27 | ThemeSource, | ||
28 | } from '@sophie/shared'; | ||
29 | import { applySnapshot, applyPatch, Instance, types } from 'mobx-state-tree'; | 22 | import { applySnapshot, applyPatch, Instance, types } from 'mobx-state-tree'; |
30 | 23 | ||
24 | import getEnv from '../env/getEnv'; | ||
25 | import RendererEnvImpl from '../env/impl/RendererEnvImpl'; | ||
31 | import { getLogger } from '../utils/log'; | 26 | import { getLogger } from '../utils/log'; |
32 | 27 | ||
33 | import type RendererEnv from './RendererEnv'; | 28 | import type { Config } from './Config'; |
34 | import { getEnv } from './RendererEnv'; | 29 | import type { Service } from './Service'; |
30 | import { sharedStore } from './SharedStore'; | ||
35 | 31 | ||
36 | const log = getLogger('RendererStore'); | 32 | const log = getLogger('RendererStore'); |
37 | 33 | ||
@@ -82,10 +78,9 @@ export interface RendererStore extends Instance<typeof rendererStore> {} | |||
82 | export function createAndConnectRendererStore( | 78 | export function createAndConnectRendererStore( |
83 | ipc: SophieRenderer, | 79 | ipc: SophieRenderer, |
84 | ): RendererStore { | 80 | ): RendererStore { |
85 | const env: RendererEnv = { | 81 | const env = new RendererEnvImpl(ipc); |
86 | dispatchMainAction: ipc.dispatchAction, | ||
87 | }; | ||
88 | const store = rendererStore.create({}, env); | 82 | const store = rendererStore.create({}, env); |
83 | env.setStore(store); | ||
89 | 84 | ||
90 | ipc | 85 | ipc |
91 | .onSharedStoreChange({ | 86 | .onSharedStoreChange({ |
diff --git a/packages/renderer/src/stores/Service.ts b/packages/renderer/src/stores/Service.ts new file mode 100644 index 0000000..2f45106 --- /dev/null +++ b/packages/renderer/src/stores/Service.ts | |||
@@ -0,0 +1,38 @@ | |||
1 | /* | ||
2 | * Copyright (C) 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 | |||
21 | import { | ||
22 | runtimeService, | ||
23 | RuntimeService, | ||
24 | service as originalService, | ||
25 | } from '@sophie/shared'; | ||
26 | import { Instance } from 'mobx-state-tree'; | ||
27 | |||
28 | import getEnv from '../env/getEnv'; | ||
29 | |||
30 | export const service = originalService.views((self) => ({ | ||
31 | get runtime(): RuntimeService { | ||
32 | return getEnv(self).getRuntimeService(self.id) ?? runtimeService.create(); | ||
33 | }, | ||
34 | })); | ||
35 | |||
36 | export interface Service extends Instance<typeof service> {} | ||
37 | |||
38 | export type { ServiceSnapshotIn, ServiceSnapshotOut } from '@sophie/shared'; | ||
diff --git a/packages/renderer/src/stores/SharedStore.ts b/packages/renderer/src/stores/SharedStore.ts new file mode 100644 index 0000000..baaf061 --- /dev/null +++ b/packages/renderer/src/stores/SharedStore.ts | |||
@@ -0,0 +1,35 @@ | |||
1 | /* | ||
2 | * Copyright (C) 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 | |||
21 | import { sharedStore as originalSharedStore } from '@sophie/shared'; | ||
22 | import { Instance, types } from 'mobx-state-tree'; | ||
23 | |||
24 | import { config } from './Config'; | ||
25 | |||
26 | export const sharedStore = originalSharedStore.props({ | ||
27 | config: types.optional(config, {}), | ||
28 | }); | ||
29 | |||
30 | export interface SharedStore extends Instance<typeof sharedStore> {} | ||
31 | |||
32 | export type { | ||
33 | SharedStoreSnapshotIn, | ||
34 | SharedStoreSnapshotOut, | ||
35 | } from '@sophie/shared'; | ||
diff --git a/packages/renderer/src/stores/__tests__/Service.spec.ts b/packages/renderer/src/stores/__tests__/Service.spec.ts new file mode 100644 index 0000000..f835d41 --- /dev/null +++ b/packages/renderer/src/stores/__tests__/Service.spec.ts | |||
@@ -0,0 +1,63 @@ | |||
1 | /* | ||
2 | * Copyright (C) 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 | |||
21 | import { jest } from '@jest/globals'; | ||
22 | import { runtimeService } from '@sophie/shared'; | ||
23 | import { mocked } from 'jest-mock'; | ||
24 | |||
25 | import type RendererEnv from '../../env/RendererEnv'; | ||
26 | import { service, Service } from '../Service'; | ||
27 | |||
28 | const env: RendererEnv = { | ||
29 | dispatchMainAction: jest.fn(), | ||
30 | getRuntimeService: jest.fn(), | ||
31 | }; | ||
32 | let sut: Service; | ||
33 | |||
34 | beforeEach(() => { | ||
35 | sut = service.create( | ||
36 | { | ||
37 | id: 'serviceId', | ||
38 | name: 'Foo', | ||
39 | url: 'https://example.com', | ||
40 | profile: 'profileId', | ||
41 | }, | ||
42 | env, | ||
43 | ); | ||
44 | }); | ||
45 | |||
46 | describe('runtime', () => { | ||
47 | it('should return the runtime service with for the service ID', () => { | ||
48 | const runtimeServiceStore = runtimeService.create({}, env); | ||
49 | mocked(env.getRuntimeService).mockReturnValueOnce(runtimeServiceStore); | ||
50 | const returnedStore = sut.runtime; | ||
51 | expect(env.getRuntimeService).toHaveBeenCalledWith('serviceId'); | ||
52 | expect(returnedStore).toBe(runtimeServiceStore); | ||
53 | }); | ||
54 | |||
55 | it('should return a valid runtime service even if none exists in the environment', () => { | ||
56 | /* | ||
57 | eslint-disable-next-line unicorn/no-useless-undefined -- | ||
58 | `mockReturnValueOnce` expects 1 parameter. | ||
59 | */ | ||
60 | mocked(env.getRuntimeService).mockReturnValueOnce(undefined); | ||
61 | expect(sut.runtime).toHaveProperty('state', 'hibernated'); | ||
62 | }); | ||
63 | }); | ||