diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-02-27 00:57:44 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2022-03-06 18:56:46 +0100 |
commit | f05d54406c9bc4b69609a4935132ff17b8e28824 (patch) | |
tree | e7ffde8f8b3433e004932a6e068dedbb4f2196da /packages/shared | |
parent | design: Simpler message count indicators (diff) | |
download | sophie-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')
-rw-r--r-- | packages/shared/src/contextBridge/SophieRenderer.ts | 2 | ||||
-rw-r--r-- | packages/shared/src/index.ts | 26 | ||||
-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 | ||
21 | import { Action } from '../schemas'; | 21 | import { Action } from '../schemas'; |
22 | import { SharedStoreListener } from '../stores/SharedStore'; | 22 | import { SharedStoreListener } from '../stores/SharedStoreBase'; |
23 | 23 | ||
24 | export default interface SophieRenderer { | 24 | export 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'; | |||
27 | export type { | 27 | export type { |
28 | GlobalSettingsSnapshotIn, | 28 | GlobalSettingsSnapshotIn, |
29 | GlobalSettingsSnapshotOut, | 29 | GlobalSettingsSnapshotOut, |
30 | } from './stores/GlobalSettings'; | 30 | } from './stores/GlobalSettingsBase'; |
31 | export { default as GlobalSettings } from './stores/GlobalSettings'; | 31 | export { |
32 | default as GlobalSettingsBase, | ||
33 | defineGlobalSettingsModel, | ||
34 | } from './stores/GlobalSettingsBase'; | ||
32 | 35 | ||
33 | export { default as Profile } from './stores/Profile'; | 36 | export { default as Profile } from './stores/Profile'; |
34 | 37 | ||
@@ -38,17 +41,26 @@ export type { | |||
38 | } from './stores/ProfileSettings'; | 41 | } from './stores/ProfileSettings'; |
39 | export { default as ProfileSettings } from './stores/ProfileSettings'; | 42 | export { default as ProfileSettings } from './stores/ProfileSettings'; |
40 | 43 | ||
41 | export { default as Service } from './stores/Service'; | 44 | export { |
45 | default as ServiceBase, | ||
46 | defineServiceModel, | ||
47 | } from './stores/ServiceBase'; | ||
42 | 48 | ||
43 | export type { | 49 | export type { |
44 | ServiceSettingsSnapshotIn, | 50 | ServiceSettingsSnapshotIn, |
45 | ServiceSettingsSnapshotOut, | 51 | ServiceSettingsSnapshotOut, |
46 | } from './stores/ServiceSettings'; | 52 | } from './stores/ServiceSettingsBase'; |
47 | export { default as ServiceSettings } from './stores/ServiceSettings'; | 53 | export { |
54 | default as ServiceSettingsBase, | ||
55 | defineServiceSettingsModel, | ||
56 | } from './stores/ServiceSettingsBase'; | ||
48 | 57 | ||
49 | export type { | 58 | export type { |
50 | SharedStoreListener, | 59 | SharedStoreListener, |
51 | SharedStoreSnapshotIn, | 60 | SharedStoreSnapshotIn, |
52 | SharedStoreSnapshotOut, | 61 | SharedStoreSnapshotOut, |
53 | } from './stores/SharedStore'; | 62 | } from './stores/SharedStoreBase'; |
54 | export { default as SharedStore } from './stores/SharedStore'; | 63 | export { |
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 | ||
21 | import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree'; | 21 | import { |
22 | Instance, | ||
23 | types, | ||
24 | SnapshotIn, | ||
25 | SnapshotOut, | ||
26 | IAnyModelType, | ||
27 | } from 'mobx-state-tree'; | ||
22 | 28 | ||
23 | import { ThemeSource } from '../schemas'; | 29 | import { ThemeSource } from '../schemas'; |
24 | 30 | ||
25 | import Service from './Service'; | 31 | import ServiceBase from './ServiceBase'; |
26 | 32 | ||
27 | const GlobalSettings = /* @__PURE__ */ (() => | 33 | export 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 | |||
46 | const 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 | */ |
41 | interface GlobalSettings extends Instance<typeof GlobalSettings> {} | 53 | interface GlobalSettingsBase extends Instance<typeof GlobalSettingsBase> {} |
42 | 54 | ||
43 | export default GlobalSettings; | 55 | export default GlobalSettingsBase; |
44 | 56 | ||
45 | export interface GlobalSettingsSnapshotIn | 57 | export interface GlobalSettingsSnapshotIn |
46 | extends SnapshotIn<typeof GlobalSettings> {} | 58 | extends SnapshotIn<typeof GlobalSettingsBase> {} |
47 | 59 | ||
48 | export interface GlobalSettingsSnapshotOut | 60 | export 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 | ||
21 | import { Instance, types } from 'mobx-state-tree'; | 21 | import { IAnyModelType, Instance, types } from 'mobx-state-tree'; |
22 | 22 | ||
23 | import ServiceSettings from './ServiceSettings'; | 23 | import ServiceSettingsBase from './ServiceSettingsBase'; |
24 | 24 | ||
25 | const Service = /* @__PURE__ */ (() => | 25 | export 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 | |||
42 | const 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 | */ |
45 | interface Service extends Instance<typeof Service> {} | 49 | interface ServiceBase extends Instance<typeof ServiceBase> {} |
46 | 50 | ||
47 | export default Service; | 51 | export 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 | ||
21 | import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree'; | 21 | import { |
22 | Instance, | ||
23 | types, | ||
24 | SnapshotIn, | ||
25 | SnapshotOut, | ||
26 | IAnyModelType, | ||
27 | } from 'mobx-state-tree'; | ||
22 | 28 | ||
23 | import Profile from './Profile'; | 29 | import ProfileBase from './Profile'; |
24 | 30 | ||
25 | const ServiceSettings = /* @__PURE__ */ (() => | 31 | export 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 | |||
42 | const 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 | */ |
37 | interface ServiceSettings extends Instance<typeof ServiceSettings> {} | 49 | interface ServiceSettingsBase extends Instance<typeof ServiceSettingsBase> {} |
38 | 50 | ||
39 | export default ServiceSettings; | 51 | export default ServiceSettingsBase; |
40 | 52 | ||
41 | export interface ServiceSettingsSnapshotIn | 53 | export interface ServiceSettingsSnapshotIn |
42 | extends SnapshotIn<typeof ServiceSettings> {} | 54 | extends SnapshotIn<typeof ServiceSettingsBase> {} |
43 | 55 | ||
44 | export interface ServiceSettingsSnapshotOut | 56 | export 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 | ||
29 | import GlobalSettings from './GlobalSettings'; | 30 | import GlobalSettingsBase from './GlobalSettingsBase'; |
30 | import Profile from './Profile'; | 31 | import ProfileBase from './Profile'; |
31 | import Service from './Service'; | 32 | import ServiceBase from './ServiceBase'; |
32 | 33 | ||
33 | const SharedStore = /* @__PURE__ */ (() => | 34 | export 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 | |||
49 | const 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 | */ |
47 | interface SharedStore extends Instance<typeof SharedStore> {} | 56 | interface SharedStoreBase extends Instance<typeof SharedStoreBase> {} |
48 | 57 | ||
49 | export default SharedStore; | 58 | export default SharedStoreBase; |
50 | 59 | ||
51 | export interface SharedStoreSnapshotIn extends SnapshotIn<typeof SharedStore> {} | 60 | export interface SharedStoreSnapshotIn |
61 | extends SnapshotIn<typeof SharedStoreBase> {} | ||
52 | 62 | ||
53 | export interface SharedStoreSnapshotOut | 63 | export interface SharedStoreSnapshotOut |
54 | extends SnapshotOut<typeof SharedStore> {} | 64 | extends SnapshotOut<typeof SharedStoreBase> {} |
55 | 65 | ||
56 | export interface SharedStoreListener { | 66 | export interface SharedStoreListener { |
57 | onSnapshot(snapshot: SharedStoreSnapshotIn): void; | 67 | onSnapshot(snapshot: SharedStoreSnapshotIn): void; |