refactor: merge ipcMainUtils.handle / handleSync (#17062)

This commit is contained in:
Milan Burda 2019-02-27 00:48:26 +01:00 committed by Samuel Attard
parent 848d16525f
commit 5a99ea4c46
5 changed files with 13 additions and 17 deletions

View file

@ -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) => {
assertChromeDevTools(event.sender, 'window.confirm()')

View file

@ -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)
})
ipcMainUtils.handleSync('CHROME_EXTENSION_MANIFEST', function (event, extensionId) {
ipcMainUtils.handle('CHROME_EXTENSION_MANIFEST', function (event, extensionId) {
const manifest = manifestMap[extensionId]
if (!manifest) {
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)
return fs.readFileSync(messagesPath)
})

View file

@ -15,15 +15,11 @@ const callHandler = async function (handler: IPCHandler, event: ElectronInternal
export const handle = function <T extends IPCHandler> (channel: string, handler: T) {
ipcMainInternal.on(channel, (event, requestId, ...args) => {
callHandler(handler, event, args, responseArgs => {
if (requestId) {
event._replyInternal(`${channel}_RESPONSE_${requestId}`, ...responseArgs)
})
})
}
export const handleSync = function <T extends IPCHandler> (channel: string, handler: T) {
ipcMainInternal.on(channel, (event, ...args) => {
callHandler(handler, event, args, responseArgs => {
} else {
event.returnValue = responseArgs
}
})
})
}

View file

@ -479,19 +479,19 @@ ipcMainInternal.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) {
event.returnValue = null
})
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_INIT', function (event, options) {
ipcMainUtils.handle('ELECTRON_CRASH_REPORTER_INIT', function (event, 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()
})
ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD_READ_FIND_TEXT', function (event) {
ipcMainUtils.handle('ELECTRON_BROWSER_CLIPBOARD_READ_FIND_TEXT', function (event) {
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)
})

View file

@ -20,7 +20,7 @@ export function invoke<T> (command: string, ...args: any[]) {
}
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) {
throw errorUtils.deserialize(error)