aboutsummaryrefslogtreecommitdiffstats
path: root/packages/shared
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-01-23 17:12:47 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-02-08 21:43:17 +0100
commit044b2de8c7861504704468ba441d4a6a37eed8f7 (patch)
tree940d2b7946d07a0c69b5e3ad46c25c599cf2aca7 /packages/shared
parentfeat: Add selected service field to SharedStore (diff)
downloadsophie-044b2de8c7861504704468ba441d4a6a37eed8f7.tar.gz
sophie-044b2de8c7861504704468ba441d4a6a37eed8f7.tar.zst
sophie-044b2de8c7861504704468ba441d4a6a37eed8f7.zip
refactor: Move runtime state into shared models
Now the runtime state lives inside the model (instead of being associated to the static settings via a map), which simplifies state management. Static settings are now located inside the runtime models, so we must create tests to make sure that the settings are being persisted correctly. The contents of the config file are now generated as a view of store (instead of a snapshot), which adds flexibility. Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages/shared')
-rw-r--r--packages/shared/src/contextBridge/SophieRenderer.ts2
-rw-r--r--packages/shared/src/index.ts41
-rw-r--r--packages/shared/src/stores/GlobalSettings.ts (renamed from packages/shared/src/stores/Config.ts)17
-rw-r--r--packages/shared/src/stores/Profile.ts10
-rw-r--r--packages/shared/src/stores/ProfileSettings.ts (renamed from packages/shared/src/stores/RuntimeService.ts)28
-rw-r--r--packages/shared/src/stores/Service.ts23
-rw-r--r--packages/shared/src/stores/ServiceSettings.ts38
-rw-r--r--packages/shared/src/stores/SharedStore.ts9
8 files changed, 95 insertions, 73 deletions
diff --git a/packages/shared/src/contextBridge/SophieRenderer.ts b/packages/shared/src/contextBridge/SophieRenderer.ts
index 9858aa9..28dc0b7 100644
--- a/packages/shared/src/contextBridge/SophieRenderer.ts
+++ b/packages/shared/src/contextBridge/SophieRenderer.ts
@@ -21,7 +21,7 @@
21import { Action } from '../schemas'; 21import { Action } from '../schemas';
22import { SharedStoreListener } from '../stores/SharedStore'; 22import { SharedStoreListener } from '../stores/SharedStore';
23 23
24export interface SophieRenderer { 24export default interface SophieRenderer {
25 onSharedStoreChange(this: void, listener: SharedStoreListener): Promise<void>; 25 onSharedStoreChange(this: void, listener: SharedStoreListener): Promise<void>;
26 26
27 dispatchAction(this: void, action: Action): void; 27 dispatchAction(this: void, action: Action): void;
diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts
index df02854..55cf5ce 100644
--- a/packages/shared/src/index.ts
+++ b/packages/shared/src/index.ts
@@ -18,7 +18,7 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21export type { SophieRenderer } from './contextBridge/SophieRenderer'; 21export type { default as SophieRenderer } from './contextBridge/SophieRenderer';
22 22
23export { MainToRendererIpcMessage, RendererToMainIpcMessage } from './ipc'; 23export { MainToRendererIpcMessage, RendererToMainIpcMessage } from './ipc';
24 24
@@ -26,34 +26,33 @@ export type { Action, BrowserViewBounds, ThemeSource } from './schemas';
26export { action, browserViewBounds, themeSource } from './schemas'; 26export { action, browserViewBounds, themeSource } from './schemas';
27 27
28export type { 28export type {
29 Config, 29 GlobalSettings,
30 ConfigSnapshotIn, 30 GlobalSettingsSnapshotIn,
31 ConfigSnapshotOut, 31 GlobalSettingsSnapshotOut,
32} from './stores/Config'; 32} from './stores/GlobalSettings';
33export { config } from './stores/Config'; 33export { globalSettings } from './stores/GlobalSettings';
34 34
35export type { 35export type { Profile } from './stores/Profile';
36 Profile,
37 ProfileSnapshotIn,
38 ProfileSnapshotOut,
39} from './stores/Profile';
40export { profile } from './stores/Profile'; 36export { profile } from './stores/Profile';
41 37
42export type { 38export type {
43 RuntimeService, 39 ProfileSettings,
44 RuntimeServiceSnapshotIn, 40 ProfileSettingsSnapshotIn,
45 RuntimeServiceSnapshotOut, 41 ProfileSettingsSnapshotOut,
46} from './stores/RuntimeService'; 42} from './stores/ProfileSettings';
47export { runtimeService } from './stores/RuntimeService'; 43export { profileSettings } from './stores/ProfileSettings';
48 44
49export type { 45export type { Service } from './stores/Service';
50 Service,
51 ServiceSnapshotIn,
52 ServiceSnapshotOut,
53} from './stores/Service';
54export { service } from './stores/Service'; 46export { service } from './stores/Service';
55 47
56export type { 48export type {
49 ServiceSettings,
50 ServiceSettingsSnapshotIn,
51 ServiceSettingsSnapshotOut,
52} from './stores/ServiceSettings';
53export { serviceSettings } from './stores/ServiceSettings';
54
55export type {
57 SharedStore, 56 SharedStore,
58 SharedStoreListener, 57 SharedStoreListener,
59 SharedStoreSnapshotIn, 58 SharedStoreSnapshotIn,
diff --git a/packages/shared/src/stores/Config.ts b/packages/shared/src/stores/GlobalSettings.ts
index 1d97e7d..bd0155a 100644
--- a/packages/shared/src/stores/Config.ts
+++ b/packages/shared/src/stores/GlobalSettings.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 *
@@ -22,17 +22,14 @@ import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree';
22 22
23import { themeSource } from '../schemas'; 23import { themeSource } from '../schemas';
24 24
25import { profile } from './Profile'; 25export const globalSettings = types.model('GlobalSettings', {
26import { service } from './Service';
27
28export const config = types.model('Config', {
29 profiles: types.array(profile),
30 services: types.array(service),
31 themeSource: types.optional(types.enumeration(themeSource.options), 'system'), 26 themeSource: types.optional(types.enumeration(themeSource.options), 'system'),
32}); 27});
33 28
34export interface Config extends Instance<typeof config> {} 29export interface GlobalSettings extends Instance<typeof globalSettings> {}
35 30
36export interface ConfigSnapshotIn extends SnapshotIn<typeof config> {} 31export interface GlobalSettingsSnapshotIn
32 extends SnapshotIn<typeof globalSettings> {}
37 33
38export interface ConfigSnapshotOut extends SnapshotOut<typeof config> {} 34export interface GlobalSettingsSnapshotOut
35 extends SnapshotOut<typeof globalSettings> {}
diff --git a/packages/shared/src/stores/Profile.ts b/packages/shared/src/stores/Profile.ts
index 88a0f4d..bb058f6 100644
--- a/packages/shared/src/stores/Profile.ts
+++ b/packages/shared/src/stores/Profile.ts
@@ -18,15 +18,13 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree'; 21import { Instance, types } from 'mobx-state-tree';
22
23import { profileSettings } from './ProfileSettings';
22 24
23export const profile = types.model('Profile', { 25export const profile = types.model('Profile', {
24 id: types.identifier, 26 id: types.identifier,
25 name: types.string, 27 settings: profileSettings,
26}); 28});
27 29
28export interface Profile extends Instance<typeof profile> {} 30export interface Profile extends Instance<typeof profile> {}
29
30export interface ProfileSnapshotIn extends SnapshotIn<typeof profile> {}
31
32export interface ProfileSnapshotOut extends SnapshotOut<typeof profile> {}
diff --git a/packages/shared/src/stores/RuntimeService.ts b/packages/shared/src/stores/ProfileSettings.ts
index c5b9031..ec8da5f 100644
--- a/packages/shared/src/stores/RuntimeService.ts
+++ b/packages/shared/src/stores/ProfileSettings.ts
@@ -20,28 +20,14 @@
20 20
21import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree'; 21import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree';
22 22
23export const runtimeService = types.model({ 23export const profileSettings = types.model('ProfileSettings', {
24 url: types.maybe(types.string), 24 name: types.string,
25 canGoBack: false,
26 canGoForward: false,
27 title: types.maybe(types.string),
28 state: types.optional(
29 types.enumeration('ServiceState', [
30 'hibernated',
31 'loading',
32 'loaded',
33 'crashed',
34 ]),
35 'hibernated',
36 ),
37 directMessageCount: 0,
38 indirectMessageCount: 0,
39}); 25});
40 26
41export interface RuntimeService extends Instance<typeof runtimeService> {} 27export interface ProfileSettings extends Instance<typeof profileSettings> {}
42 28
43export interface RuntimeServiceSnapshotIn 29export interface ProfileSettingsSnapshotIn
44 extends SnapshotIn<typeof runtimeService> {} 30 extends SnapshotIn<typeof profileSettings> {}
45 31
46export interface RuntimeServiceSnapshotOut 32export interface ProfileSettingsSnapshotOut
47 extends SnapshotOut<typeof runtimeService> {} 33 extends SnapshotOut<typeof profileSettings> {}
diff --git a/packages/shared/src/stores/Service.ts b/packages/shared/src/stores/Service.ts
index ed2cd9a..36acd3d 100644
--- a/packages/shared/src/stores/Service.ts
+++ b/packages/shared/src/stores/Service.ts
@@ -18,20 +18,23 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree'; 21import { Instance, types } from 'mobx-state-tree';
22 22
23import { profile } from './Profile'; 23import { serviceSettings } from './ServiceSettings';
24 24
25export const service = types.model('Service', { 25export const service = types.model('Service', {
26 id: types.identifier, 26 id: types.identifier,
27 name: types.string, 27 settings: serviceSettings,
28 profile: types.reference(profile), 28 currentUrl: types.maybe(types.string),
29 // TODO: Remove this once recipes are added. 29 canGoBack: false,
30 url: types.string, 30 canGoForward: false,
31 title: types.maybe(types.string),
32 state: types.optional(
33 types.enumeration('ServiceState', ['loading', 'loaded', 'crashed']),
34 'loading',
35 ),
36 directMessageCount: 0,
37 indirectMessageCount: 0,
31}); 38});
32 39
33export interface Service extends Instance<typeof service> {} 40export interface Service extends Instance<typeof service> {}
34
35export interface ServiceSnapshotIn extends SnapshotIn<typeof service> {}
36
37export interface ServiceSnapshotOut extends SnapshotOut<typeof service> {}
diff --git a/packages/shared/src/stores/ServiceSettings.ts b/packages/shared/src/stores/ServiceSettings.ts
new file mode 100644
index 0000000..54cd7eb
--- /dev/null
+++ b/packages/shared/src/stores/ServiceSettings.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
21import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree';
22
23import { profile } from './Profile';
24
25export const serviceSettings = types.model('ServiceSettings', {
26 name: types.string,
27 profile: types.reference(profile),
28 // TODO: Remove this once recipes are added.
29 url: types.string,
30});
31
32export interface ServiceSettings extends Instance<typeof serviceSettings> {}
33
34export interface ServiceSettingsSnapshotIn
35 extends SnapshotIn<typeof serviceSettings> {}
36
37export interface ServiceSettingsSnapshotOut
38 extends SnapshotOut<typeof serviceSettings> {}
diff --git a/packages/shared/src/stores/SharedStore.ts b/packages/shared/src/stores/SharedStore.ts
index e6e2cad..a04f4bf 100644
--- a/packages/shared/src/stores/SharedStore.ts
+++ b/packages/shared/src/stores/SharedStore.ts
@@ -26,13 +26,14 @@ import {
26 SnapshotOut, 26 SnapshotOut,
27} from 'mobx-state-tree'; 27} from 'mobx-state-tree';
28 28
29import { config } from './Config'; 29import { globalSettings } from './GlobalSettings';
30import { runtimeService } from './RuntimeService'; 30import { profile } from './Profile';
31import { service } from './Service'; 31import { service } from './Service';
32 32
33export const sharedStore = types.model('SharedStore', { 33export const sharedStore = types.model('SharedStore', {
34 config: types.optional(config, {}), 34 settings: types.optional(globalSettings, {}),
35 runtimeServices: types.map(runtimeService), 35 profiles: types.array(profile),
36 services: types.array(service),
36 selectedService: types.safeReference(service), 37 selectedService: types.safeReference(service),
37 shouldUseDarkColors: false, 38 shouldUseDarkColors: false,
38}); 39});