aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-01-20 18:06:23 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-02-08 21:43:15 +0100
commitd24734ac4cd1c6ddda5ba39f033ce9eaa4dcda01 (patch)
treeb696243e5bf35ee19237c149da3d779239e2a7d7 /packages/main
parentfix: Do not access localStorage in service-preload (diff)
downloadsophie-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.ts74
-rw-r--r--packages/main/src/stores/SharedStore.ts2
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
21import { UnreadCount } from '@sophie/service-shared';
22import { runtimeService as originalRuntimeService } from '@sophie/shared';
23import { Instance } from 'mobx-state-tree';
24
25export 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
69export interface RuntimeService extends Instance<typeof runtimeService> {}
70
71export 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';
22import { Instance, types } from 'mobx-state-tree'; 22import { Instance, types } from 'mobx-state-tree';
23 23
24import { config } from './Config'; 24import { config } from './Config';
25import { runtimeService } from './RuntimeService';
25 26
26export type { 27export type {
27 SharedStoreSnapshotIn, 28 SharedStoreSnapshotIn,
@@ -30,6 +31,7 @@ export type {
30 31
31export const sharedStore = originalSharedStore.props({ 32export const sharedStore = originalSharedStore.props({
32 config: types.optional(config, {}), 33 config: types.optional(config, {}),
34 runtimeServices: types.map(runtimeService),
33}); 35});
34 36
35export interface SharedStore extends Instance<typeof sharedStore> {} 37export interface SharedStore extends Instance<typeof sharedStore> {}