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/main | |
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/main')
-rw-r--r-- | packages/main/src/stores/RuntimeService.ts | 74 | ||||
-rw-r--r-- | packages/main/src/stores/SharedStore.ts | 2 |
2 files changed, 76 insertions, 0 deletions
diff --git a/packages/main/src/stores/RuntimeService.ts b/packages/main/src/stores/RuntimeService.ts new file mode 100644 index 0000000..ecb1942 --- /dev/null +++ b/packages/main/src/stores/RuntimeService.ts | |||
@@ -0,0 +1,74 @@ | |||
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 { UnreadCount } from '@sophie/service-shared'; | ||
22 | import { runtimeService as originalRuntimeService } from '@sophie/shared'; | ||
23 | import { Instance } from 'mobx-state-tree'; | ||
24 | |||
25 | export const runtimeService = originalRuntimeService.actions((self) => ({ | ||
26 | setLocation({ | ||
27 | url, | ||
28 | canGoBack, | ||
29 | canGoForward, | ||
30 | }: { | ||
31 | url: string; | ||
32 | canGoBack: boolean; | ||
33 | canGoForward: boolean; | ||
34 | }): void { | ||
35 | self.url = url; | ||
36 | self.canGoBack = canGoBack; | ||
37 | self.canGoForward = canGoForward; | ||
38 | }, | ||
39 | setTitle(title: string): void { | ||
40 | self.title = title; | ||
41 | }, | ||
42 | hibernated(): void { | ||
43 | self.canGoBack = false; | ||
44 | self.canGoForward = false; | ||
45 | self.state = 'hibernated'; | ||
46 | }, | ||
47 | startedLoading(): void { | ||
48 | self.state = 'loading'; | ||
49 | }, | ||
50 | finishedLoading(): void { | ||
51 | if (self.state === 'loading') { | ||
52 | // Do not overwrite crashed state if the service haven't been reloaded yet. | ||
53 | self.state = 'loaded'; | ||
54 | } | ||
55 | }, | ||
56 | crashed(): void { | ||
57 | self.state = 'crashed'; | ||
58 | }, | ||
59 | setUnreadCount({ direct, indirect }: UnreadCount): void { | ||
60 | if (direct !== undefined) { | ||
61 | self.directMessageCount = direct; | ||
62 | } | ||
63 | if (indirect !== undefined) { | ||
64 | self.indirectMessageCount = indirect; | ||
65 | } | ||
66 | }, | ||
67 | })); | ||
68 | |||
69 | export interface RuntimeService extends Instance<typeof runtimeService> {} | ||
70 | |||
71 | export type { | ||
72 | RuntimeServiceSnapshotIn, | ||
73 | RuntimeServiceSnapshotOut, | ||
74 | } from '@sophie/shared'; | ||
diff --git a/packages/main/src/stores/SharedStore.ts b/packages/main/src/stores/SharedStore.ts index 73245cd..76ce05c 100644 --- a/packages/main/src/stores/SharedStore.ts +++ b/packages/main/src/stores/SharedStore.ts | |||
@@ -22,6 +22,7 @@ import { sharedStore as originalSharedStore } from '@sophie/shared'; | |||
22 | import { Instance, types } from 'mobx-state-tree'; | 22 | import { Instance, types } from 'mobx-state-tree'; |
23 | 23 | ||
24 | import { config } from './Config'; | 24 | import { config } from './Config'; |
25 | import { runtimeService } from './RuntimeService'; | ||
25 | 26 | ||
26 | export type { | 27 | export type { |
27 | SharedStoreSnapshotIn, | 28 | SharedStoreSnapshotIn, |
@@ -30,6 +31,7 @@ export type { | |||
30 | 31 | ||
31 | export const sharedStore = originalSharedStore.props({ | 32 | export const sharedStore = originalSharedStore.props({ |
32 | config: types.optional(config, {}), | 33 | config: types.optional(config, {}), |
34 | runtimeServices: types.map(runtimeService), | ||
33 | }); | 35 | }); |
34 | 36 | ||
35 | export interface SharedStore extends Instance<typeof sharedStore> {} | 37 | export interface SharedStore extends Instance<typeof sharedStore> {} |