chore: optimize code
This commit is contained in:
parent
b5a442d3b0
commit
1509250f92
|
@ -31,9 +31,9 @@ async function mainWin() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test active push message to Renderer-process.
|
// Test active push message to Renderer-process.
|
||||||
setInterval(() => {
|
win.webContents.on('did-finish-load', () => {
|
||||||
win?.webContents.send('main-process-message', (new Date).toLocaleString())
|
win?.webContents.send('main-process-message', (new Date).toLocaleString())
|
||||||
}, 999)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
app.whenReady().then(mainWin)
|
app.whenReady().then(mainWin)
|
||||||
|
|
|
@ -26,17 +26,18 @@ contextBridge.exposeInMainWorld('bridge', {
|
||||||
|
|
||||||
// `exposeInMainWorld` can not detect `prototype` attribute and methods, manually patch it.
|
// `exposeInMainWorld` can not detect `prototype` attribute and methods, manually patch it.
|
||||||
function withPrototype(obj: Record<string, any>) {
|
function withPrototype(obj: Record<string, any>) {
|
||||||
const protos = Object.entries(Object.getPrototypeOf(obj))
|
const protos = Object.getPrototypeOf(obj)
|
||||||
for (const [key, value] of protos) {
|
|
||||||
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
|
for (const [key, value] of Object.entries(protos)) {
|
||||||
if (typeof value === 'function') {
|
if (Object.prototype.hasOwnProperty.call(obj, key)) continue
|
||||||
// Some native API not work Renderer-process, like `NodeJS.EventEmitter['on']`. Wrap a function patch it.
|
|
||||||
obj[key] = function (...args: any) {
|
if (typeof value === 'function') {
|
||||||
return value.call(obj, ...args)
|
// Some native API not work in Renderer-process, like `NodeJS.EventEmitter['on']`. Wrap a function patch it.
|
||||||
}
|
obj[key] = function (...args: any) {
|
||||||
} else {
|
return value.call(obj, ...args)
|
||||||
Object.assign(obj, { [key]: value })
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
obj[key] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return obj
|
return obj
|
||||||
|
|
Loading…
Reference in New Issue