mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-18 06:07:48 +00:00
refactor: version 2.0 (React)
This commit is contained in:
parent
7dad7d810a
commit
950f72d4e8
356 changed files with 7901 additions and 29547 deletions
36
packages/preload/index.ts
Normal file
36
packages/preload/index.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { contextBridge, ipcRenderer } from 'electron'
|
||||
import fs from 'fs'
|
||||
import { useLoading } from './loading'
|
||||
import { domReady } from './utils'
|
||||
|
||||
const { appendLoading, removeLoading } = useLoading()
|
||||
|
||||
;(async () => {
|
||||
await domReady()
|
||||
|
||||
appendLoading()
|
||||
})()
|
||||
|
||||
// --------- Expose some API to the Renderer process. ---------
|
||||
contextBridge.exposeInMainWorld('fs', fs)
|
||||
contextBridge.exposeInMainWorld('removeLoading', removeLoading)
|
||||
contextBridge.exposeInMainWorld('ipcRenderer', withPrototype(ipcRenderer))
|
||||
|
||||
// `exposeInMainWorld` can't detect attributes and methods of `prototype`, manually patching it.
|
||||
function withPrototype(obj: Record<string, any>) {
|
||||
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 APIs, like `NodeJS.EventEmitter['on']`, don't work in the Renderer process. Wrapping them into a function.
|
||||
obj[key] = function (...args: any) {
|
||||
return value.call(obj, ...args)
|
||||
}
|
||||
} else {
|
||||
obj[key] = value
|
||||
}
|
||||
}
|
||||
return obj
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue