mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2025-12-25 11:30:58 -08:00
refactor: export constants to renderer process through ContextService
This commit is contained in:
parent
8de13505ac
commit
3404bedfc9
10 changed files with 119 additions and 34 deletions
|
|
@ -9,6 +9,9 @@ export enum MainChannel {
|
|||
export enum AuthenticationChannel {
|
||||
name = 'AuthenticationChannel',
|
||||
}
|
||||
export enum ContextChannel {
|
||||
name = 'ContextChannel',
|
||||
}
|
||||
export enum GitChannel {
|
||||
name = 'GitChannel',
|
||||
}
|
||||
|
|
|
|||
42
src/main.ts
42
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;
|
||||
|
|
|
|||
|
|
@ -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<OriginalProxy, K extends ConditionalKeys<OriginalProxy, Funct
|
|||
};
|
||||
|
||||
export const auth = createProxy<AsyncifyProxy<IAuthenticationService>>(AuthenticationServiceIPCDescriptor);
|
||||
export const context = createProxy<AsyncifyProxy<IContextService>>(ContextServiceIPCDescriptor);
|
||||
export const git = createProxy<AsyncifyProxy<IGitService>>(GitServiceIPCDescriptor);
|
||||
export const menu = createProxy<AsyncifyProxy<IMenuService>>(MenuServiceIPCDescriptor);
|
||||
export const notification = createProxy<AsyncifyProxy<INotificationService>>(NotificationServiceIPCDescriptor);
|
||||
|
|
|
|||
24
src/services/constants/index.ts
Normal file
24
src/services/constants/index.ts
Normal file
|
|
@ -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<K extends keyof IContext>(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`);
|
||||
}
|
||||
}
|
||||
34
src/services/constants/interface.ts
Normal file
34
src/services/constants/interface.ts
Normal file
|
|
@ -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<K extends keyof IContext>(key: K): IContext[K];
|
||||
}
|
||||
export const ContextServiceIPCDescriptor = {
|
||||
channel: ContextChannel.name,
|
||||
properties: {
|
||||
get: ProxyPropertyType.Function,
|
||||
},
|
||||
};
|
||||
|
|
@ -1,14 +1,6 @@
|
|||
import path from 'path';
|
||||
import { ipcMain } from 'electron';
|
||||
|
||||
ipcMain.handle(
|
||||
'get-constant',
|
||||
async (_event, key: string): Promise<any> => {
|
||||
const pathConstants: Record<string, any> = 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);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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 };
|
||||
|
|
|
|||
|
|
@ -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<Authentication>(serviceIdentifier.Authentication).to(Authentication).inSingletonScope();
|
||||
container.bind<Git>(serviceIdentifier.Git).to(Git).inSingletonScope();
|
||||
container.bind<MenuService>(serviceIdentifier.MenuService).to(MenuService).inSingletonScope();
|
||||
container.bind<Notification>(serviceIdentifier.Notification).to(Notification).inSingletonScope();
|
||||
container.bind<Preference>(serviceIdentifier.Preference).to(Preference).inSingletonScope();
|
||||
container.bind<SystemPreference>(serviceIdentifier.SystemPreference).to(SystemPreference).inSingletonScope();
|
||||
container.bind<Updater>(serviceIdentifier.Updater).to(Updater).inSingletonScope();
|
||||
container.bind<View>(serviceIdentifier.View).to(View).inSingletonScope();
|
||||
container.bind<Wiki>(serviceIdentifier.Wiki).to(Wiki).inSingletonScope();
|
||||
container.bind<WikiGitWorkspace>(serviceIdentifier.WikiGitWorkspace).to(WikiGitWorkspace).inSingletonScope();
|
||||
container.bind<Window>(serviceIdentifier.Window).to(Window).inSingletonScope();
|
||||
container.bind<Workspace>(serviceIdentifier.Workspace).to(Workspace).inSingletonScope();
|
||||
container.bind<WorkspaceView>(serviceIdentifier.WorkspaceView).to(WorkspaceView).inSingletonScope();
|
||||
container.bind<IAuthenticationService>(serviceIdentifier.Authentication).to(Authentication).inSingletonScope();
|
||||
container.bind<IContextService>(serviceIdentifier.Context).to(ContextService).inSingletonScope();
|
||||
container.bind<IGitService>(serviceIdentifier.Git).to(Git).inSingletonScope();
|
||||
container.bind<IMenuService>(serviceIdentifier.MenuService).to(MenuService).inSingletonScope();
|
||||
container.bind<INotificationService>(serviceIdentifier.Notification).to(Notification).inSingletonScope();
|
||||
container.bind<IPreferenceService>(serviceIdentifier.Preference).to(Preference).inSingletonScope();
|
||||
container.bind<ISystemPreferenceService>(serviceIdentifier.SystemPreference).to(SystemPreference).inSingletonScope();
|
||||
container.bind<IUpdaterService>(serviceIdentifier.Updater).to(Updater).inSingletonScope();
|
||||
container.bind<IViewService>(serviceIdentifier.View).to(View).inSingletonScope();
|
||||
container.bind<IWikiService>(serviceIdentifier.Wiki).to(Wiki).inSingletonScope();
|
||||
container.bind<IWikiGitWorkspaceService>(serviceIdentifier.WikiGitWorkspace).to(WikiGitWorkspace).inSingletonScope();
|
||||
container.bind<IWindowService>(serviceIdentifier.Window).to(Window).inSingletonScope();
|
||||
container.bind<IWorkspaceService>(serviceIdentifier.Workspace).to(Workspace).inSingletonScope();
|
||||
container.bind<IWorkspaceViewService>(serviceIdentifier.WorkspaceView).to(WorkspaceView).inSingletonScope();
|
||||
|
||||
// TODO: delay service init, call init() manually
|
||||
const authService = container.get<IAuthenticationService>(serviceIdentifier.Authentication);
|
||||
const contextService = container.get<IContextService>(serviceIdentifier.Context);
|
||||
const gitService = container.get<IGitService>(serviceIdentifier.Git);
|
||||
const menuService = container.get<IMenuService>(serviceIdentifier.MenuService);
|
||||
const notificationService = container.get<INotificationService>(serviceIdentifier.Notification);
|
||||
|
|
@ -61,6 +68,7 @@ const workspaceService = container.get<IWorkspaceService>(serviceIdentifier.Work
|
|||
const workspaceViewService = container.get<IWorkspaceViewService>(serviceIdentifier.WorkspaceView);
|
||||
|
||||
registerProxy(authService, AuthenticationServiceIPCDescriptor);
|
||||
registerProxy(contextService, ContextServiceIPCDescriptor);
|
||||
registerProxy(gitService, GitServiceIPCDescriptor);
|
||||
registerProxy(menuService, MenuServiceIPCDescriptor);
|
||||
registerProxy(notificationService, NotificationServiceIPCDescriptor);
|
||||
|
|
|
|||
|
|
@ -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'),
|
||||
|
|
|
|||
|
|
@ -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<Menubar> {
|
||||
const menubarWindowState = windowStateKeeper({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue