aboutsummaryrefslogtreecommitdiffstats
path: root/packages/shared/src
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-02-27 00:57:44 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-03-06 18:56:46 +0100
commitf05d54406c9bc4b69609a4935132ff17b8e28824 (patch)
treee7ffde8f8b3433e004932a6e068dedbb4f2196da /packages/shared/src
parentdesign: Simpler message count indicators (diff)
downloadsophie-f05d54406c9bc4b69609a4935132ff17b8e28824.tar.gz
sophie-f05d54406c9bc4b69609a4935132ff17b8e28824.tar.zst
sophie-f05d54406c9bc4b69609a4935132ff17b8e28824.zip
refactor: Shared model type factories
Allows customization of stores both in the renderer and in the main process. Instead of exposing a basic model type from the shared module (which was be overwritted with more specific props in the main package), we expose factory function that can create specific model types in both the renderer and the main process. Using these package-specific customization to stores, the renderer package can attach IPC calls directly to store objects, which the main package can attach the handlers for IPC calls and other internal actions. Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages/shared/src')
-rw-r--r--packages/shared/src/contextBridge/SophieRenderer.ts2
-rw-r--r--packages/shared/src/index.ts26
-rw-r--r--packages/shared/src/stores/GlobalSettingsBase.ts (renamed from packages/shared/src/stores/GlobalSettings.ts)32
-rw-r--r--packages/shared/src/stores/ServiceBase.ts (renamed from packages/shared/src/stores/Service.ts)20
-rw-r--r--packages/shared/src/stores/ServiceSettingsBase.ts (renamed from packages/shared/src/stores/ServiceSettings.ts)32
-rw-r--r--packages/shared/src/stores/SharedStoreBase.ts (renamed from packages/shared/src/stores/SharedStore.ts)42
6 files changed, 102 insertions, 52 deletions
diff --git a/packages/shared/src/contextBridge/SophieRenderer.ts b/packages/shared/src/contextBridge/SophieRenderer.ts
index 28dc0b7..9e087da 100644
--- a/packages/shared/src/contextBridge/SophieRenderer.ts
+++ b/packages/shared/src/contextBridge/SophieRenderer.ts
@@ -19,7 +19,7 @@
19 */ 19 */
20 20
21import { Action } from '../schemas'; 21import { Action } from '../schemas';
22import { SharedStoreListener } from '../stores/SharedStore'; 22import { SharedStoreListener } from '../stores/SharedStoreBase';
23 23
24export default interface SophieRenderer { 24export default interface SophieRenderer {
25 onSharedStoreChange(this: void, listener: SharedStoreListener): Promise<void>; 25 onSharedStoreChange(this: void, listener: SharedStoreListener): Promise<void>;
diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts
index 3d30488..66debf7 100644
--- a/packages/shared/src/index.ts
+++ b/packages/shared/src/index.ts
@@ -27,8 +27,11 @@ export { Action, BrowserViewBounds, ThemeSource } from './schemas';
27export type { 27export type {
28 GlobalSettingsSnapshotIn, 28 GlobalSettingsSnapshotIn,
29 GlobalSettingsSnapshotOut, 29 GlobalSettingsSnapshotOut,
30} from './stores/GlobalSettings'; 30} from './stores/GlobalSettingsBase';
31export { default as GlobalSettings } from './stores/GlobalSettings'; 31export {
32 default as GlobalSettingsBase,
33 defineGlobalSettingsModel,
34} from './stores/GlobalSettingsBase';
32 35
33export { default as Profile } from './stores/Profile'; 36export { default as Profile } from './stores/Profile';
34 37
@@ -38,17 +41,26 @@ export type {
38} from './stores/ProfileSettings'; 41} from './stores/ProfileSettings';
39export { default as ProfileSettings } from './stores/ProfileSettings'; 42export { default as ProfileSettings } from './stores/ProfileSettings';
40 43
41export { default as Service } from './stores/Service'; 44export {
45 default as ServiceBase,
46 defineServiceModel,
47} from './stores/ServiceBase';
42 48
43export type { 49export type {
44 ServiceSettingsSnapshotIn, 50 ServiceSettingsSnapshotIn,
45 ServiceSettingsSnapshotOut, 51 ServiceSettingsSnapshotOut,
46} from './stores/ServiceSettings'; 52} from './stores/ServiceSettingsBase';
47export { default as ServiceSettings } from './stores/ServiceSettings'; 53export {
54 default as ServiceSettingsBase,
55 defineServiceSettingsModel,
56} from './stores/ServiceSettingsBase';
48 57
49export type { 58export type {
50 SharedStoreListener, 59 SharedStoreListener,
51 SharedStoreSnapshotIn, 60 SharedStoreSnapshotIn,
52 SharedStoreSnapshotOut, 61 SharedStoreSnapshotOut,
53} from './stores/SharedStore'; 62} from './stores/SharedStoreBase';
54export { default as SharedStore } from './stores/SharedStore'; 63export {
64 default as SharedStoreBase,
65 defineSharedStoreModel,
66} from './stores/SharedStoreBase';
diff --git a/packages/shared/src/stores/GlobalSettings.ts b/packages/shared/src/stores/GlobalSettingsBase.ts
index f316af9..48092fd 100644
--- a/packages/shared/src/stores/GlobalSettings.ts
+++ b/packages/shared/src/stores/GlobalSettingsBase.ts
@@ -18,32 +18,44 @@
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 {
22 Instance,
23 types,
24 SnapshotIn,
25 SnapshotOut,
26 IAnyModelType,
27} from 'mobx-state-tree';
22 28
23import { ThemeSource } from '../schemas'; 29import { ThemeSource } from '../schemas';
24 30
25import Service from './Service'; 31import ServiceBase from './ServiceBase';
26 32
27const GlobalSettings = /* @__PURE__ */ (() => 33export function defineGlobalSettingsModel<TS extends IAnyModelType>(
28 types.model('GlobalSettings', { 34 service: TS,
35) {
36 return types.model('GlobalSettings', {
29 themeSource: types.optional( 37 themeSource: types.optional(
30 types.enumeration(ThemeSource.options), 38 types.enumeration(ThemeSource.options),
31 'system', 39 'system',
32 ), 40 ),
33 showLocationBar: false, 41 showLocationBar: false,
34 selectedService: types.safeReference(Service), 42 selectedService: types.safeReference(service),
35 }))(); 43 });
44}
45
46const GlobalSettingsBase = /* @__PURE__ */ (() =>
47 defineGlobalSettingsModel(ServiceBase))();
36 48
37/* 49/*
38 eslint-disable-next-line @typescript-eslint/no-redeclare -- 50 eslint-disable-next-line @typescript-eslint/no-redeclare --
39 Intentionally naming the type the same as the store definition. 51 Intentionally naming the type the same as the store definition.
40*/ 52*/
41interface GlobalSettings extends Instance<typeof GlobalSettings> {} 53interface GlobalSettingsBase extends Instance<typeof GlobalSettingsBase> {}
42 54
43export default GlobalSettings; 55export default GlobalSettingsBase;
44 56
45export interface GlobalSettingsSnapshotIn 57export interface GlobalSettingsSnapshotIn
46 extends SnapshotIn<typeof GlobalSettings> {} 58 extends SnapshotIn<typeof GlobalSettingsBase> {}
47 59
48export interface GlobalSettingsSnapshotOut 60export interface GlobalSettingsSnapshotOut
49 extends SnapshotOut<typeof GlobalSettings> {} 61 extends SnapshotOut<typeof GlobalSettingsBase> {}
diff --git a/packages/shared/src/stores/Service.ts b/packages/shared/src/stores/ServiceBase.ts
index a4e3c92..cde403b 100644
--- a/packages/shared/src/stores/Service.ts
+++ b/packages/shared/src/stores/ServiceBase.ts
@@ -18,14 +18,14 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import { Instance, types } from 'mobx-state-tree'; 21import { IAnyModelType, Instance, types } from 'mobx-state-tree';
22 22
23import ServiceSettings from './ServiceSettings'; 23import ServiceSettingsBase from './ServiceSettingsBase';
24 24
25const Service = /* @__PURE__ */ (() => 25export function defineServiceModel<TS extends IAnyModelType>(settings: TS) {
26 types.model('Service', { 26 return types.model('Service', {
27 id: types.identifier, 27 id: types.identifier,
28 settings: ServiceSettings, 28 settings,
29 currentUrl: types.maybe(types.string), 29 currentUrl: types.maybe(types.string),
30 canGoBack: false, 30 canGoBack: false,
31 canGoForward: false, 31 canGoForward: false,
@@ -36,12 +36,16 @@ const Service = /* @__PURE__ */ (() =>
36 ), 36 ),
37 directMessageCount: 0, 37 directMessageCount: 0,
38 indirectMessageCount: 0, 38 indirectMessageCount: 0,
39 }))(); 39 });
40}
41
42const ServiceBase = /* @__PURE__ */ (() =>
43 defineServiceModel(ServiceSettingsBase))();
40 44
41/* 45/*
42 eslint-disable-next-line @typescript-eslint/no-redeclare -- 46 eslint-disable-next-line @typescript-eslint/no-redeclare --
43 Intentionally naming the type the same as the store definition. 47 Intentionally naming the type the same as the store definition.
44*/ 48*/
45interface Service extends Instance<typeof Service> {} 49interface ServiceBase extends Instance<typeof ServiceBase> {}
46 50
47export default Service; 51export default ServiceBase;
diff --git a/packages/shared/src/stores/ServiceSettings.ts b/packages/shared/src/stores/ServiceSettingsBase.ts
index a5811f5..45eb15d 100644
--- a/packages/shared/src/stores/ServiceSettings.ts
+++ b/packages/shared/src/stores/ServiceSettingsBase.ts
@@ -18,28 +18,40 @@
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 {
22 Instance,
23 types,
24 SnapshotIn,
25 SnapshotOut,
26 IAnyModelType,
27} from 'mobx-state-tree';
22 28
23import Profile from './Profile'; 29import ProfileBase from './Profile';
24 30
25const ServiceSettings = /* @__PURE__ */ (() => 31export function defineServiceSettingsModel<TP extends IAnyModelType>(
26 types.model('ServiceSettings', { 32 profile: TP,
33) {
34 return types.model('ServiceSettings', {
27 name: types.string, 35 name: types.string,
28 profile: types.reference(Profile), 36 profile: types.reference(profile),
29 // TODO: Remove this once recipes are added. 37 // TODO: Remove this once recipes are added.
30 url: types.string, 38 url: types.string,
31 }))(); 39 });
40}
41
42const ServiceSettingsBase = /* @__PURE__ */ (() =>
43 defineServiceSettingsModel(ProfileBase))();
32 44
33/* 45/*
34 eslint-disable-next-line @typescript-eslint/no-redeclare -- 46 eslint-disable-next-line @typescript-eslint/no-redeclare --
35 Intentionally naming the type the same as the store definition. 47 Intentionally naming the type the same as the store definition.
36*/ 48*/
37interface ServiceSettings extends Instance<typeof ServiceSettings> {} 49interface ServiceSettingsBase extends Instance<typeof ServiceSettingsBase> {}
38 50
39export default ServiceSettings; 51export default ServiceSettingsBase;
40 52
41export interface ServiceSettingsSnapshotIn 53export interface ServiceSettingsSnapshotIn
42 extends SnapshotIn<typeof ServiceSettings> {} 54 extends SnapshotIn<typeof ServiceSettingsBase> {}
43 55
44export interface ServiceSettingsSnapshotOut 56export interface ServiceSettingsSnapshotOut
45 extends SnapshotOut<typeof ServiceSettings> {} 57 extends SnapshotOut<typeof ServiceSettingsBase> {}
diff --git a/packages/shared/src/stores/SharedStore.ts b/packages/shared/src/stores/SharedStoreBase.ts
index d81a3d3..8d6624b 100644
--- a/packages/shared/src/stores/SharedStore.ts
+++ b/packages/shared/src/stores/SharedStoreBase.ts
@@ -24,34 +24,44 @@ import {
24 types, 24 types,
25 SnapshotIn, 25 SnapshotIn,
26 SnapshotOut, 26 SnapshotOut,
27 IAnyModelType,
27} from 'mobx-state-tree'; 28} from 'mobx-state-tree';
28 29
29import GlobalSettings from './GlobalSettings'; 30import GlobalSettingsBase from './GlobalSettingsBase';
30import Profile from './Profile'; 31import ProfileBase from './Profile';
31import Service from './Service'; 32import ServiceBase from './ServiceBase';
32 33
33const SharedStore = /* @__PURE__ */ (() => 34export function defineSharedStoreModel<
34 types.model('SharedStore', { 35 TG extends IAnyModelType,
35 settings: types.optional(GlobalSettings, {}), 36 TP extends IAnyModelType,
36 profilesById: types.map(Profile), 37 TS extends IAnyModelType,
37 profiles: types.array(types.reference(Profile)), 38>(globalSettings: TG, profile: TP, service: TS) {
38 servicesById: types.map(Service), 39 return types.model('SharedStore', {
39 services: types.array(types.reference(Service)), 40 settings: types.optional(globalSettings, {}),
41 profilesById: types.map(profile),
42 profiles: types.array(types.reference(profile)),
43 servicesById: types.map(service),
44 services: types.array(types.reference(service)),
40 shouldUseDarkColors: false, 45 shouldUseDarkColors: false,
41 }))(); 46 });
47}
48
49const SharedStoreBase = /* @__PURE__ */ (() =>
50 defineSharedStoreModel(GlobalSettingsBase, ProfileBase, ServiceBase))();
42 51
43/* 52/*
44 eslint-disable-next-line @typescript-eslint/no-redeclare -- 53 eslint-disable-next-line @typescript-eslint/no-redeclare --
45 Intentionally naming the type the same as the store definition. 54 Intentionally naming the type the same as the store definition.
46*/ 55*/
47interface SharedStore extends Instance<typeof SharedStore> {} 56interface SharedStoreBase extends Instance<typeof SharedStoreBase> {}
48 57
49export default SharedStore; 58export default SharedStoreBase;
50 59
51export interface SharedStoreSnapshotIn extends SnapshotIn<typeof SharedStore> {} 60export interface SharedStoreSnapshotIn
61 extends SnapshotIn<typeof SharedStoreBase> {}
52 62
53export interface SharedStoreSnapshotOut 63export interface SharedStoreSnapshotOut
54 extends SnapshotOut<typeof SharedStore> {} 64 extends SnapshotOut<typeof SharedStoreBase> {}
55 65
56export interface SharedStoreListener { 66export interface SharedStoreListener {
57 onSnapshot(snapshot: SharedStoreSnapshotIn): void; 67 onSnapshot(snapshot: SharedStoreSnapshotIn): void;