refactor: remove .bridge

This commit is contained in:
草鞋没号 2022-01-21 17:18:07 +08:00
parent 38f0a11824
commit e6b82ec838
4 changed files with 16 additions and 31 deletions

View File

@ -1,5 +1,4 @@
import fs from 'fs' import fs from 'fs'
import path from 'path'
import { contextBridge, ipcRenderer } from 'electron' import { contextBridge, ipcRenderer } from 'electron'
import { domReady } from './utils' import { domReady } from './utils'
import { useLoading } from './loading' import { useLoading } from './loading'
@ -13,16 +12,10 @@ const { appendLoading, removeLoading } = useLoading()
appendLoading() appendLoading()
})(); })();
// --------------------------------------------------- // --------- Expose some API to Renderer process. ---------
contextBridge.exposeInMainWorld('fs', fs)
contextBridge.exposeInMainWorld('bridge', { contextBridge.exposeInMainWorld('removeLoading', removeLoading)
__dirname, contextBridge.exposeInMainWorld('ipcRenderer', withPrototype(ipcRenderer))
__filename,
fs,
path,
ipcRenderer: withPrototype(ipcRenderer),
removeLoading,
})
// `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>) {

View File

@ -1,16 +1,11 @@
export {} export { }
declare global { declare global {
interface Window { interface Window {
/** Expose some Api through preload script */ // Expose some Api through preload script
bridge: { fs: typeof import('fs')
__dirname: string ipcRenderer: import('electron').IpcRenderer
__filename: string removeLoading: () => void
fs: typeof import('fs')
path: typeof import('path')
ipcRenderer: import('electron').IpcRenderer
removeLoading: () => void
}
} }
} }

View File

@ -10,16 +10,13 @@ ReactDOM.render(
<App /> <App />
</React.StrictMode>, </React.StrictMode>,
document.getElementById('root'), document.getElementById('root'),
() => { window.removeLoading,
window.bridge.removeLoading()
},
) )
// ----------------------------------------------------------- console.log('fs', window.fs)
console.log('ipcRenderer', window.ipcRenderer)
console.log('contextBridge ->', window.bridge)
// Use ipcRenderer.on // Use ipcRenderer.on
window.bridge.ipcRenderer.on('main-process-message', (_event, ...args) => { window.ipcRenderer.on('main-process-message', (_event, ...args) => {
console.log('[Receive Main-process message]:', ...args) console.log('[Receive Main-process message]:', ...args)
}) })

View File

@ -1,7 +1,7 @@
// Use 'electron-store' // Use 'electron-store'
const store = { const store = {
async get(key: string) { async get(key: string) {
const { invoke } = window.bridge.ipcRenderer const { invoke } = window.ipcRenderer
let value = await invoke('electron-store', 'get', key) let value = await invoke('electron-store', 'get', key)
try { try {
value = JSON.parse(value) value = JSON.parse(value)
@ -10,7 +10,7 @@ const store = {
} }
}, },
async set(key: string, value: any) { async set(key: string, value: any) {
const { invoke } = window.bridge.ipcRenderer const { invoke } = window.ipcRenderer
let val = value let val = value
try { try {
if (value && typeof value === 'object') { if (value && typeof value === 'object') {
@ -25,7 +25,7 @@ const store = {
(async () => { (async () => {
await store.set('Date.now', Date.now()) await store.set('Date.now', Date.now())
console.log('electron-store ->', 'Date.now:', await store.get('Date.now')) console.log('electron-store ->', 'Date.now:', await store.get('Date.now'))
console.log('electron-store ->', 'path:', await window.bridge.ipcRenderer.invoke('electron-store', 'path')) console.log('electron-store ->', 'path:', await window.ipcRenderer.invoke('electron-store', 'path'))
})(); })();
export { } export { }