Skip to content

Commit

Permalink
fix(app): 优化 app remote 模块,修改成异步通信,修复同步通信导致的页面卡死问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Jun 4, 2022
1 parent 65ca4e1 commit 9a81721
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/web/src/components/Path.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ const { shell } = require('electron');
const realPath = ref(store.get(name.value) ? path.resolve(store.get(name.value)) : '');
function change (name: string) {
async function change (name: string) {
if (setting.value) {
const res = remote.dialog.call('showOpenDialogSync', {
const res = await remote.dialog.call('showOpenDialogSync', {
properties: ['openDirectory'],
defaultPath: realPath.value
});
Expand Down
13 changes: 9 additions & 4 deletions packages/web/src/components/Title.vue
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ function relaunch () {
remote.app.call('quit');
}

function openLog () {
shell.openPath(path.join(remote.app.call('getPath', 'logs'), formatDate()));
async function openLog () {
shell.openPath(path.join(await remote.app.call('getPath', 'logs'), formatDate()));
}

function allNotify () {
Expand All @@ -164,7 +164,7 @@ function about () {
closable: true,
maskClosable: true,
content: h('ul', [
h('li', '软件版本 : ' + remote.app.call('getVersion')),
h('li', '软件版本 : ' + await remote.app.call('getVersion')),
h('li', [
h('span', '版本详情 : '),
h(
Expand All @@ -179,8 +179,13 @@ function about () {
])
});
}
const max = ref<boolean>(false);

const max = ref<boolean>(remote.win.call('isMaximized'));
onMounted(() => {
nextTick(async() => {
max.value = await remote.win.call('isMaximized');
});
});

watch(max, () => {
if (max.value) {
Expand Down
5 changes: 2 additions & 3 deletions packages/web/src/components/terminal/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ export class Process {
/**
* 使用 child_process 运行 ocs 命令
*/
init (xterm: ITerminal) {
const target = path.join(remote.app.call('getAppPath'), './script.js');

async init(xterm: ITerminal) {
const target = path.join(await remote.app.call('getAppPath'), './script.js');
const shell = childProcess.fork(target, { stdio: ['ipc'] });
shell.stdout?.on('data', (data: any) => xterm.write(data));
shell.stderr?.on('data', (data: any) => remote.logger.call('error', String(data)));
Expand Down
36 changes: 27 additions & 9 deletions packages/web/src/utils/remote.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
import { BrowserWindow, App, Dialog, WebContents } from 'electron';
import { notify } from './notify';
import { RemoteMethods } from '@ocsjs/app';
const { randomUUID } = require('crypto');
const { ipcRenderer } = require('electron');

/**
* 注册渲染进程远程通信
* @param eventName
* @returns
*/
function registerRemote<T> (eventName: string) {
function sendSync (channel: string, ...args: any[]): any {
console.log(channel, args);
function registerRemote<T>(eventName: string) {
function sendSync(channel: string, ...args: any[]): any {
const res = ipcRenderer.sendSync(channel, ...args);

if (res?.error) {
notify('remote 模块错误', res.error, 'remote', { copy: true, type: 'error' });
}
return res;
}

function send(channel: string, args: any[]): any {
return new Promise((resolve) => {
ipcRenderer.once(args[0], (e: any, ...respondArgs) => {
if (respondArgs[0].error) {
notify('remote 模块错误', respondArgs[0].error, 'remote', { copy: true, type: 'error' });
} else {
resolve(respondArgs[0].data);
}
console.log(args[1], args[2], respondArgs[0].data);
});
ipcRenderer.send(channel, args);
});
}

return {
// 获取远程变量
get (property: keyof T) {
get(property: keyof T) {
return sendSync(eventName + '-get', [property]);
},
// 设置远程变量
set (property: keyof T, value: any) {
set(property: keyof T, value: any) {
return sendSync(eventName + '-set', [property, value]);
},
// 调用远程方法
call (property: keyof T, ...args: any[]) {
return sendSync(eventName + '-call', [property, ...args]);
call(property: keyof T, ...args: any[]) {
// 通信名
const channel = [eventName, 'call'].join('-');
// 回调名
const respondChannel = randomUUID().replace(/-/g, '');
return send(channel, [respondChannel, property, ...args]);
}
};
}
Expand All @@ -44,7 +62,7 @@ export const remote = {
// 注册 dialog 通信
dialog: registerRemote<Dialog>('dialog'),
// 暴露方法
methods: registerRemote<any>('methods'),
methods: registerRemote<RemoteMethods>('methods'),
// 日志
// eslint-disable-next-line no-undef
logger: registerRemote<Console>('logger')
Expand Down

0 comments on commit 9a81721

Please sign in to comment.