在主进程启动时,提前预加载菜单数据和缩放配置数据,从而减少主界面的加载耗时。
This commit is contained in:
parent
12377a42ed
commit
542a167066
@ -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();
|
||||||
|
}
|
||||||
|
@ -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 {
|
||||||
|
@ -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 图标时重新创建窗口
|
||||||
|
@ -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和url。其中webContentId是请求打开URL的webview的id。
|
* @param callback 回调函数,参数为webContentId和url。其中webContentId是请求打开URL的webview的id。
|
||||||
|
12
src/types/electron.d.ts
vendored
12
src/types/electron.d.ts
vendored
@ -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和url。其中webContentId是请求打开URL的webview的id。
|
* @param callback 回调函数,参数为webContentId和url。其中webContentId是请求打开URL的webview的id。
|
||||||
|
Loading…
x
Reference in New Issue
Block a user