From 1509250f926139ecb17399cfa71fc830488f469e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Fri, 26 Nov 2021 08:49:02 +0800 Subject: [PATCH] chore: optimize code --- src/main/index.ts | 4 ++-- src/preload/index.ts | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 74d6252..821c1d1 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -31,9 +31,9 @@ async function mainWin() { } // Test active push message to Renderer-process. - setInterval(() => { + win.webContents.on('did-finish-load', () => { win?.webContents.send('main-process-message', (new Date).toLocaleString()) - }, 999) + }) } app.whenReady().then(mainWin) diff --git a/src/preload/index.ts b/src/preload/index.ts index ec4636b..ca1a052 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -26,17 +26,18 @@ contextBridge.exposeInMainWorld('bridge', { // `exposeInMainWorld` can not detect `prototype` attribute and methods, manually patch it. function withPrototype(obj: Record) { - const protos = Object.entries(Object.getPrototypeOf(obj)) - for (const [key, value] of protos) { - if (!Object.prototype.hasOwnProperty.call(obj, key)) { - if (typeof value === 'function') { - // Some native API not work Renderer-process, like `NodeJS.EventEmitter['on']`. Wrap a function patch it. - obj[key] = function (...args: any) { - return value.call(obj, ...args) - } - } else { - Object.assign(obj, { [key]: value }) + const protos = Object.getPrototypeOf(obj) + + for (const [key, value] of Object.entries(protos)) { + if (Object.prototype.hasOwnProperty.call(obj, key)) continue + + if (typeof value === 'function') { + // Some native API not work in Renderer-process, like `NodeJS.EventEmitter['on']`. Wrap a function patch it. + obj[key] = function (...args: any) { + return value.call(obj, ...args) } + } else { + obj[key] = value } } return obj