From f05d54406c9bc4b69609a4935132ff17b8e28824 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sun, 27 Feb 2022 00:57:44 +0100 Subject: refactor: Shared model type factories MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../shared/src/contextBridge/SophieRenderer.ts | 2 +- packages/shared/src/index.ts | 26 +++++--- packages/shared/src/stores/GlobalSettings.ts | 49 --------------- packages/shared/src/stores/GlobalSettingsBase.ts | 61 +++++++++++++++++++ packages/shared/src/stores/Service.ts | 47 --------------- packages/shared/src/stores/ServiceBase.ts | 51 ++++++++++++++++ packages/shared/src/stores/ServiceSettings.ts | 45 -------------- packages/shared/src/stores/ServiceSettingsBase.ts | 57 ++++++++++++++++++ packages/shared/src/stores/SharedStore.ts | 60 ------------------- packages/shared/src/stores/SharedStoreBase.ts | 70 ++++++++++++++++++++++ 10 files changed, 259 insertions(+), 209 deletions(-) delete mode 100644 packages/shared/src/stores/GlobalSettings.ts create mode 100644 packages/shared/src/stores/GlobalSettingsBase.ts delete mode 100644 packages/shared/src/stores/Service.ts create mode 100644 packages/shared/src/stores/ServiceBase.ts delete mode 100644 packages/shared/src/stores/ServiceSettings.ts create mode 100644 packages/shared/src/stores/ServiceSettingsBase.ts delete mode 100644 packages/shared/src/stores/SharedStore.ts create mode 100644 packages/shared/src/stores/SharedStoreBase.ts (limited to 'packages/shared') 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 @@ */ import { Action } from '../schemas'; -import { SharedStoreListener } from '../stores/SharedStore'; +import { SharedStoreListener } from '../stores/SharedStoreBase'; export default interface SophieRenderer { onSharedStoreChange(this: void, listener: SharedStoreListener): Promise; 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'; export type { GlobalSettingsSnapshotIn, GlobalSettingsSnapshotOut, -} from './stores/GlobalSettings'; -export { default as GlobalSettings } from './stores/GlobalSettings'; +} from './stores/GlobalSettingsBase'; +export { + default as GlobalSettingsBase, + defineGlobalSettingsModel, +} from './stores/GlobalSettingsBase'; export { default as Profile } from './stores/Profile'; @@ -38,17 +41,26 @@ export type { } from './stores/ProfileSettings'; export { default as ProfileSettings } from './stores/ProfileSettings'; -export { default as Service } from './stores/Service'; +export { + default as ServiceBase, + defineServiceModel, +} from './stores/ServiceBase'; export type { ServiceSettingsSnapshotIn, ServiceSettingsSnapshotOut, -} from './stores/ServiceSettings'; -export { default as ServiceSettings } from './stores/ServiceSettings'; +} from './stores/ServiceSettingsBase'; +export { + default as ServiceSettingsBase, + defineServiceSettingsModel, +} from './stores/ServiceSettingsBase'; export type { SharedStoreListener, SharedStoreSnapshotIn, SharedStoreSnapshotOut, -} from './stores/SharedStore'; -export { default as SharedStore } from './stores/SharedStore'; +} from './stores/SharedStoreBase'; +export { + default as SharedStoreBase, + defineSharedStoreModel, +} from './stores/SharedStoreBase'; diff --git a/packages/shared/src/stores/GlobalSettings.ts b/packages/shared/src/stores/GlobalSettings.ts deleted file mode 100644 index f316af9..0000000 --- a/packages/shared/src/stores/GlobalSettings.ts +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2022 Kristóf Marussy - * - * This file is part of Sophie. - * - * Sophie is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * SPDX-License-Identifier: AGPL-3.0-only - */ - -import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree'; - -import { ThemeSource } from '../schemas'; - -import Service from './Service'; - -const GlobalSettings = /* @__PURE__ */ (() => - types.model('GlobalSettings', { - themeSource: types.optional( - types.enumeration(ThemeSource.options), - 'system', - ), - showLocationBar: false, - selectedService: types.safeReference(Service), - }))(); - -/* - eslint-disable-next-line @typescript-eslint/no-redeclare -- - Intentionally naming the type the same as the store definition. -*/ -interface GlobalSettings extends Instance {} - -export default GlobalSettings; - -export interface GlobalSettingsSnapshotIn - extends SnapshotIn {} - -export interface GlobalSettingsSnapshotOut - extends SnapshotOut {} diff --git a/packages/shared/src/stores/GlobalSettingsBase.ts b/packages/shared/src/stores/GlobalSettingsBase.ts new file mode 100644 index 0000000..48092fd --- /dev/null +++ b/packages/shared/src/stores/GlobalSettingsBase.ts @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { + Instance, + types, + SnapshotIn, + SnapshotOut, + IAnyModelType, +} from 'mobx-state-tree'; + +import { ThemeSource } from '../schemas'; + +import ServiceBase from './ServiceBase'; + +export function defineGlobalSettingsModel( + service: TS, +) { + return types.model('GlobalSettings', { + themeSource: types.optional( + types.enumeration(ThemeSource.options), + 'system', + ), + showLocationBar: false, + selectedService: types.safeReference(service), + }); +} + +const GlobalSettingsBase = /* @__PURE__ */ (() => + defineGlobalSettingsModel(ServiceBase))(); + +/* + eslint-disable-next-line @typescript-eslint/no-redeclare -- + Intentionally naming the type the same as the store definition. +*/ +interface GlobalSettingsBase extends Instance {} + +export default GlobalSettingsBase; + +export interface GlobalSettingsSnapshotIn + extends SnapshotIn {} + +export interface GlobalSettingsSnapshotOut + extends SnapshotOut {} diff --git a/packages/shared/src/stores/Service.ts b/packages/shared/src/stores/Service.ts deleted file mode 100644 index a4e3c92..0000000 --- a/packages/shared/src/stores/Service.ts +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2021-2022 Kristóf Marussy - * - * This file is part of Sophie. - * - * Sophie is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * SPDX-License-Identifier: AGPL-3.0-only - */ - -import { Instance, types } from 'mobx-state-tree'; - -import ServiceSettings from './ServiceSettings'; - -const Service = /* @__PURE__ */ (() => - types.model('Service', { - id: types.identifier, - settings: ServiceSettings, - currentUrl: types.maybe(types.string), - canGoBack: false, - canGoForward: false, - title: types.maybe(types.string), - state: types.optional( - types.enumeration('ServiceState', ['loading', 'loaded', 'crashed']), - 'loading', - ), - directMessageCount: 0, - indirectMessageCount: 0, - }))(); - -/* - eslint-disable-next-line @typescript-eslint/no-redeclare -- - Intentionally naming the type the same as the store definition. -*/ -interface Service extends Instance {} - -export default Service; diff --git a/packages/shared/src/stores/ServiceBase.ts b/packages/shared/src/stores/ServiceBase.ts new file mode 100644 index 0000000..cde403b --- /dev/null +++ b/packages/shared/src/stores/ServiceBase.ts @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { IAnyModelType, Instance, types } from 'mobx-state-tree'; + +import ServiceSettingsBase from './ServiceSettingsBase'; + +export function defineServiceModel(settings: TS) { + return types.model('Service', { + id: types.identifier, + settings, + currentUrl: types.maybe(types.string), + canGoBack: false, + canGoForward: false, + title: types.maybe(types.string), + state: types.optional( + types.enumeration('ServiceState', ['loading', 'loaded', 'crashed']), + 'loading', + ), + directMessageCount: 0, + indirectMessageCount: 0, + }); +} + +const ServiceBase = /* @__PURE__ */ (() => + defineServiceModel(ServiceSettingsBase))(); + +/* + eslint-disable-next-line @typescript-eslint/no-redeclare -- + Intentionally naming the type the same as the store definition. +*/ +interface ServiceBase extends Instance {} + +export default ServiceBase; diff --git a/packages/shared/src/stores/ServiceSettings.ts b/packages/shared/src/stores/ServiceSettings.ts deleted file mode 100644 index a5811f5..0000000 --- a/packages/shared/src/stores/ServiceSettings.ts +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2022 Kristóf Marussy - * - * This file is part of Sophie. - * - * Sophie is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * SPDX-License-Identifier: AGPL-3.0-only - */ - -import { Instance, types, SnapshotIn, SnapshotOut } from 'mobx-state-tree'; - -import Profile from './Profile'; - -const ServiceSettings = /* @__PURE__ */ (() => - types.model('ServiceSettings', { - name: types.string, - profile: types.reference(Profile), - // TODO: Remove this once recipes are added. - url: types.string, - }))(); - -/* - eslint-disable-next-line @typescript-eslint/no-redeclare -- - Intentionally naming the type the same as the store definition. -*/ -interface ServiceSettings extends Instance {} - -export default ServiceSettings; - -export interface ServiceSettingsSnapshotIn - extends SnapshotIn {} - -export interface ServiceSettingsSnapshotOut - extends SnapshotOut {} diff --git a/packages/shared/src/stores/ServiceSettingsBase.ts b/packages/shared/src/stores/ServiceSettingsBase.ts new file mode 100644 index 0000000..45eb15d --- /dev/null +++ b/packages/shared/src/stores/ServiceSettingsBase.ts @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { + Instance, + types, + SnapshotIn, + SnapshotOut, + IAnyModelType, +} from 'mobx-state-tree'; + +import ProfileBase from './Profile'; + +export function defineServiceSettingsModel( + profile: TP, +) { + return types.model('ServiceSettings', { + name: types.string, + profile: types.reference(profile), + // TODO: Remove this once recipes are added. + url: types.string, + }); +} + +const ServiceSettingsBase = /* @__PURE__ */ (() => + defineServiceSettingsModel(ProfileBase))(); + +/* + eslint-disable-next-line @typescript-eslint/no-redeclare -- + Intentionally naming the type the same as the store definition. +*/ +interface ServiceSettingsBase extends Instance {} + +export default ServiceSettingsBase; + +export interface ServiceSettingsSnapshotIn + extends SnapshotIn {} + +export interface ServiceSettingsSnapshotOut + extends SnapshotOut {} diff --git a/packages/shared/src/stores/SharedStore.ts b/packages/shared/src/stores/SharedStore.ts deleted file mode 100644 index d81a3d3..0000000 --- a/packages/shared/src/stores/SharedStore.ts +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2021-2022 Kristóf Marussy - * - * This file is part of Sophie. - * - * Sophie is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * SPDX-License-Identifier: AGPL-3.0-only - */ - -import { - IJsonPatch, - Instance, - types, - SnapshotIn, - SnapshotOut, -} from 'mobx-state-tree'; - -import GlobalSettings from './GlobalSettings'; -import Profile from './Profile'; -import Service from './Service'; - -const SharedStore = /* @__PURE__ */ (() => - types.model('SharedStore', { - settings: types.optional(GlobalSettings, {}), - profilesById: types.map(Profile), - profiles: types.array(types.reference(Profile)), - servicesById: types.map(Service), - services: types.array(types.reference(Service)), - shouldUseDarkColors: false, - }))(); - -/* - eslint-disable-next-line @typescript-eslint/no-redeclare -- - Intentionally naming the type the same as the store definition. -*/ -interface SharedStore extends Instance {} - -export default SharedStore; - -export interface SharedStoreSnapshotIn extends SnapshotIn {} - -export interface SharedStoreSnapshotOut - extends SnapshotOut {} - -export interface SharedStoreListener { - onSnapshot(snapshot: SharedStoreSnapshotIn): void; - - onPatch(patches: IJsonPatch[]): void; -} diff --git a/packages/shared/src/stores/SharedStoreBase.ts b/packages/shared/src/stores/SharedStoreBase.ts new file mode 100644 index 0000000..8d6624b --- /dev/null +++ b/packages/shared/src/stores/SharedStoreBase.ts @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { + IJsonPatch, + Instance, + types, + SnapshotIn, + SnapshotOut, + IAnyModelType, +} from 'mobx-state-tree'; + +import GlobalSettingsBase from './GlobalSettingsBase'; +import ProfileBase from './Profile'; +import ServiceBase from './ServiceBase'; + +export function defineSharedStoreModel< + TG extends IAnyModelType, + TP extends IAnyModelType, + TS extends IAnyModelType, +>(globalSettings: TG, profile: TP, service: TS) { + return types.model('SharedStore', { + settings: types.optional(globalSettings, {}), + profilesById: types.map(profile), + profiles: types.array(types.reference(profile)), + servicesById: types.map(service), + services: types.array(types.reference(service)), + shouldUseDarkColors: false, + }); +} + +const SharedStoreBase = /* @__PURE__ */ (() => + defineSharedStoreModel(GlobalSettingsBase, ProfileBase, ServiceBase))(); + +/* + eslint-disable-next-line @typescript-eslint/no-redeclare -- + Intentionally naming the type the same as the store definition. +*/ +interface SharedStoreBase extends Instance {} + +export default SharedStoreBase; + +export interface SharedStoreSnapshotIn + extends SnapshotIn {} + +export interface SharedStoreSnapshotOut + extends SnapshotOut {} + +export interface SharedStoreListener { + onSnapshot(snapshot: SharedStoreSnapshotIn): void; + + onPatch(patches: IJsonPatch[]): void; +} -- cgit v1.2.3-70-g09d2