在主进程启动时,提前预加载菜单数据和缩放配置数据,从而减少主界面的加载耗时。

This commit is contained in:
Allen 2025-05-17 21:34:19 +08:00
parent 12377a42ed
commit 542a167066
5 changed files with 75 additions and 3 deletions

View File

@ -2,6 +2,7 @@ import { ipcMain, screen, session } from "electron";
import http from 'http'; import http from 'http';
import https from 'https'; import https from 'https';
import { URL } from 'url'; import { URL } from 'url';
import { ApiResponse, MenuItem, TagResolutionConfig, EIACDesktopApi } from './EIAC_Desktop_Api';
export function initialize(): void { export function initialize(): void {
// Get primary display需要注意size 是逻辑像素尺寸logical screen size已被 DPI 缩放影响,需要除以 DPI 缩放比例才是物理像素尺寸physical screen size // Get primary display需要注意size 是逻辑像素尺寸logical screen size已被 DPI 缩放影响,需要除以 DPI 缩放比例才是物理像素尺寸physical screen size
@ -97,5 +98,50 @@ export function initialize(): void {
return false; return false;
} }
}); });
// Get menu cache
ipcMain.handle('get-menu-cache', async () => menuData ?? await menuDataReadyPromise);
// Get config cache
ipcMain.handle('get-config-cache', async () => configData ?? await configDataReadyPromise);
} }
let menuData: ApiResponse<MenuItem[]> = null;
let menuDataReadyPromise: Promise<ApiResponse<MenuItem[]>>;
let configData: ApiResponse<TagResolutionConfig[]> = null;
let configDataReadyPromise: Promise<ApiResponse<TagResolutionConfig[]>>;
function getMenuAsync(): void {
if (!menuDataReadyPromise) {
menuDataReadyPromise = new Promise((resolve, reject) => {
EIACDesktopApi.Menu.GetMenuAsync()
.then(data => {
menuData = data;
resolve(data);
})
.catch(err => {
reject(err);
});
});
}
}
function getConfigAsync(): void {
if (!configDataReadyPromise) {
configDataReadyPromise = new Promise((resolve, reject) => {
EIACDesktopApi.Menu.GetConfigAsync()
.then(data => {
configData = data;
resolve(data);
})
.catch(err => {
reject(err);
});
});
}
}
export function preloadData(): void {
getMenuAsync();
getConfigAsync();
}

View File

@ -1,6 +1,6 @@
import { PageTitleUpdatedEvent } from 'electron'; import { PageTitleUpdatedEvent } from 'electron';
import { TabGroup, Tab } from 'electron-tabs'; import { TabGroup, Tab } from 'electron-tabs';
import { MenuItem, ApiResponse, EIACDesktopApi } from './EIAC_Desktop_Api'; import { MenuItem, ApiResponse } from './EIAC_Desktop_Api';
const tabGroup: TabGroup = document.querySelector('tab-group') as TabGroup; const tabGroup: TabGroup = document.querySelector('tab-group') as TabGroup;
@ -27,7 +27,7 @@ function checkLoginStatus(): void {
*/ */
async function getMenuListAsync(): Promise<MenuItem[]> { async function getMenuListAsync(): Promise<MenuItem[]> {
try { try {
const response: ApiResponse<MenuItem[]> = await EIACDesktopApi.Menu.GetMenuAsync(); const response: ApiResponse<MenuItem[]> = await window.electronAPI.getMenuCacheAsync();
if (response.status === 0) { if (response.status === 0) {
return response.data; return response.data;
} else { } else {

View File

@ -1,7 +1,7 @@
import { app, BrowserWindow, globalShortcut, Menu, Tray } from 'electron'; import { app, BrowserWindow, globalShortcut, Menu, Tray } from 'electron';
import path from 'node:path'; import path from 'node:path';
import started from 'electron-squirrel-startup'; import started from 'electron-squirrel-startup';
import { initialize } from './IpcMainHandler'; import { initialize, preloadData } from './IpcMainHandler';
const isDevelopment: boolean = process.env.NODE_ENV === 'development'; const isDevelopment: boolean = process.env.NODE_ENV === 'development';
@ -151,6 +151,7 @@ app.on('window-all-closed', () => {
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.whenReady().then(() => { app.whenReady().then(() => {
preloadData();
createWindow(); createWindow();
// 在 macOS 平台上,点击 dock 图标时重新创建窗口 // 在 macOS 平台上,点击 dock 图标时重新创建窗口

View File

@ -2,8 +2,21 @@
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts // https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
import { contextBridge, ipcRenderer } from 'electron'; import { contextBridge, ipcRenderer } from 'electron';
import { ApiResponse, MenuItem, TagResolutionConfig } from './EIAC_Desktop_Api';
contextBridge.exposeInMainWorld('electronAPI', { contextBridge.exposeInMainWorld('electronAPI', {
/**
*
* @returns
*/
getMenuCacheAsync: () => ipcRenderer.invoke('get-menu-cache') as Promise<ApiResponse<MenuItem[]>>,
/**
*
* @returns
*/
getConfigCacheAsync: () => ipcRenderer.invoke('get-config-cache') as Promise<ApiResponse<TagResolutionConfig[]>>,
/** /**
* URL * URL
* @param callback webContentId和urlwebContentId是请求打开URL的webview的id * @param callback webContentId和urlwebContentId是请求打开URL的webview的id

View File

@ -1,4 +1,16 @@
import { ApiResponse, MenuItem, TagResolutionConfig } from '../EIAC_Desktop_Api';
export interface ElectronAPI { export interface ElectronAPI {
/**
*
* @returns
*/
getMenuCacheAsync: () => Promise<ApiResponse<MenuItem[]>>;
/**
*
* @returns
*/
getConfigCacheAsync: () => Promise<ApiResponse<TagResolutionConfig[]>>;
/** /**
* URL * URL
* @param callback webContentId和urlwebContentId是请求打开URL的webview的id * @param callback webContentId和urlwebContentId是请求打开URL的webview的id