From 3404bedfc990db7dedb359db39f50888ea2bf3f8 Mon Sep 17 00:00:00 2001 From: tiddlygit-test Date: Mon, 25 Jan 2021 01:05:27 +0800 Subject: [PATCH] refactor: export constants to renderer process through ContextService --- src/constants/channels.ts | 3 ++ src/main.ts | 42 ++++++++++++++----- src/preload/common/services.ts | 2 + src/services/constants/index.ts | 24 +++++++++++ src/services/constants/interface.ts | 34 +++++++++++++++ src/services/constants/listeners.ts | 8 ---- src/services/constants/paths.ts | 2 +- src/services/libs/bindServiceAndProxy.ts | 34 +++++++++------ src/services/serviceIdentifier.ts | 1 + src/services/windows/handleAttachToMenuBar.ts | 3 +- 10 files changed, 119 insertions(+), 34 deletions(-) create mode 100644 src/services/constants/index.ts create mode 100644 src/services/constants/interface.ts diff --git a/src/constants/channels.ts b/src/constants/channels.ts index a7000aba..980627c5 100644 --- a/src/constants/channels.ts +++ b/src/constants/channels.ts @@ -9,6 +9,9 @@ export enum MainChannel { export enum AuthenticationChannel { name = 'AuthenticationChannel', } +export enum ContextChannel { + name = 'ContextChannel', +} export enum GitChannel { name = 'GitChannel', } diff --git a/src/main.ts b/src/main.ts index 3e44bf84..46a6078e 100755 --- a/src/main.ts +++ b/src/main.ts @@ -7,6 +7,7 @@ import settings from 'electron-settings'; import { autoUpdater } from 'electron-updater'; import unhandled from 'electron-unhandled'; import { openNewGitHubIssue, debugInfo } from 'electron-util'; +import i18n from 'i18next'; import { clearMainBindings } from '@services/libs/i18n/i18next-electron-fs-backend'; import { buildLanguageMenu } from '@services/libs/i18n/buildLanguageMenu'; @@ -19,20 +20,30 @@ import MAILTO_URLS from '@services/constants/mailto-urls'; import { initI18NAfterServiceReady } from '@services/libs/i18n'; import serviceIdentifier from '@services/serviceIdentifier'; -import { IAuthenticationService, Authentication } from '@services/auth'; -import { IGitService, Git } from '@services/git'; -import { IMenuService, MenuService } from '@services/menu'; +import { Authentication } from '@services/auth'; +import { Git } from '@services/git'; +import { MenuService } from '@services/menu'; import { Notification } from '@services/notifications'; -import { IPreferenceService, Preference } from '@services/preferences'; +import { Preference } from '@services/preferences'; import { SystemPreference } from '@services/systemPreferences'; import { Updater } from '@services/updater'; -import { IViewService, View } from '@services/view'; -import { IWikiService, Wiki } from '@services/wiki'; +import { View } from '@services/view'; +import { Wiki } from '@services/wiki'; import { WikiGitWorkspace } from '@services/wikiGitWorkspace'; -import { IWindowService, Window } from '@services/windows'; +import { Window } from '@services/windows'; import { WindowNames } from '@services/windows/WindowProperties'; -import { IWorkspaceService, Workspace } from '@services/workspaces'; -import { IWorkspaceViewService, WorkspaceView } from '@services/workspacesView'; +import { Workspace } from '@services/workspaces'; +import { WorkspaceView } from '@services/workspacesView'; + +import { IAuthenticationService } from './services/auth/interface'; +import { IGitService } from './services/git/interface'; +import { IMenuService } from './services/menu/interface'; +import { IPreferenceService } from './services/preferences/interface'; +import { IViewService } from './services/view/interface'; +import { IWikiService } from './services/wiki/interface'; +import { IWindowService } from './services/windows/interface'; +import { IWorkspaceService } from './services/workspaces/interface'; +import { IWorkspaceViewService } from './services/workspacesView/interface'; const gotTheLock = app.requestSingleInstanceLock(); @@ -127,7 +138,15 @@ if (!gotTheLock) { }); } await windowService.open(WindowNames.main); - const { hibernateUnusedWorkspacesAtLaunch, proxyBypassRules, proxyPacScript, proxyRules, proxyType, themeSource } = preferenceService.getPreferences(); + const { + hibernateUnusedWorkspacesAtLaunch, + proxyBypassRules, + proxyPacScript, + proxyRules, + proxyType, + themeSource, + language, + } = preferenceService.getPreferences(); // configure proxy for default session if (proxyType === 'rules') { await session.defaultSession.setProxy({ @@ -146,6 +165,8 @@ if (!gotTheLock) { windowService.sendToAllWindows(ThemeChannel.nativeThemeUpdated); viewService.reloadViewsDarkReader(); }); + // set language async + void i18n.changeLanguage(language); const workspaces = workspaceService.getWorkspaces(); for (const workspaceID in workspaces) { const workspace = workspaces[workspaceID]; @@ -217,7 +238,6 @@ if (!gotTheLock) { menuService.buildMenu(); }; - app.on('ready', () => { autoUpdater.allowPrerelease = preferenceService.get('allowPrerelease'); autoUpdater.logger = logger; diff --git a/src/preload/common/services.ts b/src/preload/common/services.ts index 7e589871..6bb1569a 100644 --- a/src/preload/common/services.ts +++ b/src/preload/common/services.ts @@ -8,6 +8,7 @@ import { Asyncify, ConditionalKeys } from 'type-fest'; import { createProxy } from '@/helpers/electron-ipc-proxy/client'; import { IAuthenticationService, AuthenticationServiceIPCDescriptor } from '@services/auth/interface'; +import { IContextService, ContextServiceIPCDescriptor } from '@services/constants/interface'; import { IGitService, GitServiceIPCDescriptor } from '@services/git/interface'; import { IMenuService, MenuServiceIPCDescriptor } from '@services/menu/interface'; import { INotificationService, NotificationServiceIPCDescriptor } from '@services/notifications/interface'; @@ -30,6 +31,7 @@ type AsyncifyProxy>(AuthenticationServiceIPCDescriptor); +export const context = createProxy>(ContextServiceIPCDescriptor); export const git = createProxy>(GitServiceIPCDescriptor); export const menu = createProxy>(MenuServiceIPCDescriptor); export const notification = createProxy>(NotificationServiceIPCDescriptor); diff --git a/src/services/constants/index.ts b/src/services/constants/index.ts new file mode 100644 index 00000000..a6598e17 --- /dev/null +++ b/src/services/constants/index.ts @@ -0,0 +1,24 @@ +import { injectable } from 'inversify'; +import isDevelopment from 'electron-is-dev'; + +import { IContextService, IContext, IPaths, IConstants } from './interface'; +import * as paths from '@services/constants/paths'; + +@injectable() +export class ContextService implements IContextService { + pathConstants: IPaths = paths; + constants: IConstants = { + isDevelopment, + }; + + public get(key: K): IContext[K] { + if (key in this.pathConstants) { + return this.pathConstants[key]; + } + if (key in this.constants) { + return this.constants[key]; + } + + throw new Error(`${String(key)} not existed in ContextService`); + } +} diff --git a/src/services/constants/interface.ts b/src/services/constants/interface.ts new file mode 100644 index 00000000..8f1dd532 --- /dev/null +++ b/src/services/constants/interface.ts @@ -0,0 +1,34 @@ +import { ProxyPropertyType } from '@/helpers/electron-ipc-proxy/common'; +import { ContextChannel } from '@/constants/channels'; + +export interface IPaths { + REACT_PATH: string; + TIDDLYWIKI_TEMPLATE_FOLDER_PATH: string; + TIDDLERS_PATH: string; + ICON_PATH: string; + CHROME_ERROR_PATH: string; + DESKTOP_PATH: string; + LOG_FOLDER: string; + LOCALIZATION_FOLDER: string; +} +/** + * Available values about running environment + */ +export interface IConstants { + isDevelopment: boolean; +} + +export type IContext = IPaths | IConstants; + +/** + * Manage constant value like `isDevelopment` and many else, so you can know about about running environment in main and renderer process easily. + */ +export interface IContextService { + get(key: K): IContext[K]; +} +export const ContextServiceIPCDescriptor = { + channel: ContextChannel.name, + properties: { + get: ProxyPropertyType.Function, + }, +}; diff --git a/src/services/constants/listeners.ts b/src/services/constants/listeners.ts index 47ab9915..ef8532d8 100644 --- a/src/services/constants/listeners.ts +++ b/src/services/constants/listeners.ts @@ -1,14 +1,6 @@ import path from 'path'; import { ipcMain } from 'electron'; -ipcMain.handle( - 'get-constant', - async (_event, key: string): Promise => { - const pathConstants: Record = await import('@services/constants/paths'); - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return pathConstants[key]; - }, -); ipcMain.handle('get-basename', (event, pathString: string) => { return path.basename(pathString); }); diff --git a/src/services/constants/paths.ts b/src/services/constants/paths.ts index c492e3c6..57c406cc 100644 --- a/src/services/constants/paths.ts +++ b/src/services/constants/paths.ts @@ -22,4 +22,4 @@ const LOG_FOLDER = isDev : path.resolve(os.homedir(), '.tg-note', 'logs'); const LOCALIZATION_FOLDER = isDev ? path.resolve(sourcePath, '..', 'localization') : path.resolve(process.resourcesPath, '..', 'localization'); -export { REACT_PATH, TIDDLYWIKI_TEMPLATE_FOLDER_PATH, TIDDLERS_PATH, ICON_PATH, CHROME_ERROR_PATH, DESKTOP_PATH, LOG_FOLDER, LOCALIZATION_FOLDER, isDev }; +export { REACT_PATH, TIDDLYWIKI_TEMPLATE_FOLDER_PATH, TIDDLERS_PATH, ICON_PATH, CHROME_ERROR_PATH, DESKTOP_PATH, LOG_FOLDER, LOCALIZATION_FOLDER }; diff --git a/src/services/libs/bindServiceAndProxy.ts b/src/services/libs/bindServiceAndProxy.ts index cfb538ae..919142f1 100644 --- a/src/services/libs/bindServiceAndProxy.ts +++ b/src/services/libs/bindServiceAndProxy.ts @@ -1,9 +1,13 @@ +/** + * Don't forget to edit ./src/preload/common/services.ts to export service to renderer process + */ import { registerProxy } from '@/helpers/electron-ipc-proxy/server'; import serviceIdentifier from '@services/serviceIdentifier'; import { container } from '@services/container'; import { Authentication } from '@services/auth'; +import { ContextService } from '@services/constants'; import { Git } from '@services/git'; import { MenuService } from '@services/menu'; import { Notification } from '@services/notifications'; @@ -18,6 +22,7 @@ import { Workspace } from '@services/workspaces'; import { WorkspaceView } from '@services/workspacesView'; import { IAuthenticationService, AuthenticationServiceIPCDescriptor } from '@services/auth/interface'; +import { IContextService, ContextServiceIPCDescriptor } from '@services/constants/interface'; import { IGitService, GitServiceIPCDescriptor } from '@services/git/interface'; import { IMenuService, MenuServiceIPCDescriptor } from '@services/menu/interface'; import { INotificationService, NotificationServiceIPCDescriptor } from '@services/notifications/interface'; @@ -31,22 +36,24 @@ import { IWindowService, WindowServiceIPCDescriptor } from '@services/windows/in import { IWorkspaceService, WorkspaceServiceIPCDescriptor } from '@services/workspaces/interface'; import { IWorkspaceViewService, WorkspaceViewServiceIPCDescriptor } from '@services/workspacesView/interface'; -container.bind(serviceIdentifier.Authentication).to(Authentication).inSingletonScope(); -container.bind(serviceIdentifier.Git).to(Git).inSingletonScope(); -container.bind(serviceIdentifier.MenuService).to(MenuService).inSingletonScope(); -container.bind(serviceIdentifier.Notification).to(Notification).inSingletonScope(); -container.bind(serviceIdentifier.Preference).to(Preference).inSingletonScope(); -container.bind(serviceIdentifier.SystemPreference).to(SystemPreference).inSingletonScope(); -container.bind(serviceIdentifier.Updater).to(Updater).inSingletonScope(); -container.bind(serviceIdentifier.View).to(View).inSingletonScope(); -container.bind(serviceIdentifier.Wiki).to(Wiki).inSingletonScope(); -container.bind(serviceIdentifier.WikiGitWorkspace).to(WikiGitWorkspace).inSingletonScope(); -container.bind(serviceIdentifier.Window).to(Window).inSingletonScope(); -container.bind(serviceIdentifier.Workspace).to(Workspace).inSingletonScope(); -container.bind(serviceIdentifier.WorkspaceView).to(WorkspaceView).inSingletonScope(); +container.bind(serviceIdentifier.Authentication).to(Authentication).inSingletonScope(); +container.bind(serviceIdentifier.Context).to(ContextService).inSingletonScope(); +container.bind(serviceIdentifier.Git).to(Git).inSingletonScope(); +container.bind(serviceIdentifier.MenuService).to(MenuService).inSingletonScope(); +container.bind(serviceIdentifier.Notification).to(Notification).inSingletonScope(); +container.bind(serviceIdentifier.Preference).to(Preference).inSingletonScope(); +container.bind(serviceIdentifier.SystemPreference).to(SystemPreference).inSingletonScope(); +container.bind(serviceIdentifier.Updater).to(Updater).inSingletonScope(); +container.bind(serviceIdentifier.View).to(View).inSingletonScope(); +container.bind(serviceIdentifier.Wiki).to(Wiki).inSingletonScope(); +container.bind(serviceIdentifier.WikiGitWorkspace).to(WikiGitWorkspace).inSingletonScope(); +container.bind(serviceIdentifier.Window).to(Window).inSingletonScope(); +container.bind(serviceIdentifier.Workspace).to(Workspace).inSingletonScope(); +container.bind(serviceIdentifier.WorkspaceView).to(WorkspaceView).inSingletonScope(); // TODO: delay service init, call init() manually const authService = container.get(serviceIdentifier.Authentication); +const contextService = container.get(serviceIdentifier.Context); const gitService = container.get(serviceIdentifier.Git); const menuService = container.get(serviceIdentifier.MenuService); const notificationService = container.get(serviceIdentifier.Notification); @@ -61,6 +68,7 @@ const workspaceService = container.get(serviceIdentifier.Work const workspaceViewService = container.get(serviceIdentifier.WorkspaceView); registerProxy(authService, AuthenticationServiceIPCDescriptor); +registerProxy(contextService, ContextServiceIPCDescriptor); registerProxy(gitService, GitServiceIPCDescriptor); registerProxy(menuService, MenuServiceIPCDescriptor); registerProxy(notificationService, NotificationServiceIPCDescriptor); diff --git a/src/services/serviceIdentifier.ts b/src/services/serviceIdentifier.ts index 92e45ad9..6138ad10 100644 --- a/src/services/serviceIdentifier.ts +++ b/src/services/serviceIdentifier.ts @@ -1,6 +1,7 @@ export default { Authentication: Symbol.for('Authentication'), Git: Symbol.for('Git'), + Context: Symbol.for('Context'), MenuService: Symbol.for('MenuService'), Notification: Symbol.for('Notification'), Preference: Symbol.for('Preference'), diff --git a/src/services/windows/handleAttachToMenuBar.ts b/src/services/windows/handleAttachToMenuBar.ts index 8682d883..9c4c60da 100644 --- a/src/services/windows/handleAttachToMenuBar.ts +++ b/src/services/windows/handleAttachToMenuBar.ts @@ -3,8 +3,9 @@ import { Menu, Tray, ipcMain, nativeImage } from 'electron'; import windowStateKeeper from 'electron-window-state'; import { menubar, Menubar } from 'menubar'; import path from 'path'; +import isDevelopment from 'electron-is-dev'; -import { REACT_PATH, isDev as isDevelopment, buildResourcePath } from '@services/constants/paths'; +import { REACT_PATH, buildResourcePath } from '@services/constants/paths'; export default async function handleAttachToMenuBar(): Promise { const menubarWindowState = windowStateKeeper({