refactor: merge ipcMainUtils.handle / handleSync (#17062)
This commit is contained in:
parent
848d16525f
commit
5a99ea4c46
5 changed files with 13 additions and 17 deletions
|
@ -103,7 +103,7 @@ ipcMainUtils.handle('ELECTRON_INSPECTOR_SELECT_FILE', function (event) {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMainUtils.handleSync('ELECTRON_INSPECTOR_CONFIRM', function (event, message, title) {
|
ipcMainUtils.handle('ELECTRON_INSPECTOR_CONFIRM', function (event, message, title) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
assertChromeDevTools(event.sender, 'window.confirm()')
|
assertChromeDevTools(event.sender, 'window.confirm()')
|
||||||
|
|
||||||
|
|
|
@ -170,7 +170,7 @@ ipcMainInternal.on('CHROME_RUNTIME_CONNECT', function (event, extensionId, conne
|
||||||
page.webContents._sendInternalToAll(`CHROME_RUNTIME_ONCONNECT_${extensionId}`, event.sender.id, portId, connectInfo)
|
page.webContents._sendInternalToAll(`CHROME_RUNTIME_ONCONNECT_${extensionId}`, event.sender.id, portId, connectInfo)
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMainUtils.handleSync('CHROME_EXTENSION_MANIFEST', function (event, extensionId) {
|
ipcMainUtils.handle('CHROME_EXTENSION_MANIFEST', function (event, extensionId) {
|
||||||
const manifest = manifestMap[extensionId]
|
const manifest = manifestMap[extensionId]
|
||||||
if (!manifest) {
|
if (!manifest) {
|
||||||
throw new Error(`Invalid extensionId: ${extensionId}`)
|
throw new Error(`Invalid extensionId: ${extensionId}`)
|
||||||
|
@ -232,7 +232,7 @@ const getMessagesPath = (extensionId) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ipcMainUtils.handleSync('CHROME_GET_MESSAGES', function (event, extensionId) {
|
ipcMainUtils.handle('CHROME_GET_MESSAGES', function (event, extensionId) {
|
||||||
const messagesPath = getMessagesPath(extensionId)
|
const messagesPath = getMessagesPath(extensionId)
|
||||||
return fs.readFileSync(messagesPath)
|
return fs.readFileSync(messagesPath)
|
||||||
})
|
})
|
||||||
|
|
|
@ -15,15 +15,11 @@ const callHandler = async function (handler: IPCHandler, event: ElectronInternal
|
||||||
export const handle = function <T extends IPCHandler> (channel: string, handler: T) {
|
export const handle = function <T extends IPCHandler> (channel: string, handler: T) {
|
||||||
ipcMainInternal.on(channel, (event, requestId, ...args) => {
|
ipcMainInternal.on(channel, (event, requestId, ...args) => {
|
||||||
callHandler(handler, event, args, responseArgs => {
|
callHandler(handler, event, args, responseArgs => {
|
||||||
|
if (requestId) {
|
||||||
event._replyInternal(`${channel}_RESPONSE_${requestId}`, ...responseArgs)
|
event._replyInternal(`${channel}_RESPONSE_${requestId}`, ...responseArgs)
|
||||||
})
|
} else {
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export const handleSync = function <T extends IPCHandler> (channel: string, handler: T) {
|
|
||||||
ipcMainInternal.on(channel, (event, ...args) => {
|
|
||||||
callHandler(handler, event, args, responseArgs => {
|
|
||||||
event.returnValue = responseArgs
|
event.returnValue = responseArgs
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -479,19 +479,19 @@ ipcMainInternal.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) {
|
||||||
event.returnValue = null
|
event.returnValue = null
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_INIT', function (event, options) {
|
ipcMainUtils.handle('ELECTRON_CRASH_REPORTER_INIT', function (event, options) {
|
||||||
return crashReporterInit(options)
|
return crashReporterInit(options)
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMainUtils.handleSync('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES', function (event) {
|
ipcMainUtils.handle('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES', function (event) {
|
||||||
return event.sender.getLastWebPreferences()
|
return event.sender.getLastWebPreferences()
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD_READ_FIND_TEXT', function (event) {
|
ipcMainUtils.handle('ELECTRON_BROWSER_CLIPBOARD_READ_FIND_TEXT', function (event) {
|
||||||
return electron.clipboard.readFindText()
|
return electron.clipboard.readFindText()
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD_WRITE_FIND_TEXT', function (event, text) {
|
ipcMainUtils.handle('ELECTRON_BROWSER_CLIPBOARD_WRITE_FIND_TEXT', function (event, text) {
|
||||||
return electron.clipboard.writeFindText(text)
|
return electron.clipboard.writeFindText(text)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ export function invoke<T> (command: string, ...args: any[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function invokeSync<T> (command: string, ...args: any[]): T {
|
export function invokeSync<T> (command: string, ...args: any[]): T {
|
||||||
const [ error, result ] = ipcRendererInternal.sendSync(command, ...args)
|
const [ error, result ] = ipcRendererInternal.sendSync(command, null, ...args)
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
throw errorUtils.deserialize(error)
|
throw errorUtils.deserialize(error)
|
||||||
|
|
Loading…
Reference in a new issue