refactor: use ipcRendererUtils.invoke / ipcMainUtils.handle for desktopCapturer.getSources() (#16619)
This commit is contained in:
parent
ac88b3ead5
commit
5791a2a9ec
2 changed files with 31 additions and 50 deletions
|
@ -1,6 +1,6 @@
|
|||
'use strict'
|
||||
|
||||
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
|
||||
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
|
||||
|
||||
const { desktopCapturer } = process.atomBinding('desktop_capturer')
|
||||
const eventBinding = process.atomBinding('event')
|
||||
|
@ -10,37 +10,34 @@ const deepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b)
|
|||
// A queue for holding all requests from renderer process.
|
||||
let requestsQueue = []
|
||||
|
||||
const electronSources = 'ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES'
|
||||
const capturerResult = (id) => `ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_${id}`
|
||||
|
||||
ipcMainInternal.on(electronSources, (event, captureWindow, captureScreen, thumbnailSize, fetchWindowIcons, id) => {
|
||||
ipcMainUtils.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', (event, captureWindow, captureScreen, thumbnailSize, fetchWindowIcons) => {
|
||||
const customEvent = eventBinding.createWithSender(event.sender)
|
||||
event.sender.emit('desktop-capturer-get-sources', customEvent)
|
||||
|
||||
if (customEvent.defaultPrevented) {
|
||||
event._replyInternal(capturerResult(id), [])
|
||||
return
|
||||
return []
|
||||
}
|
||||
|
||||
const request = {
|
||||
id,
|
||||
options: {
|
||||
captureWindow,
|
||||
captureScreen,
|
||||
thumbnailSize,
|
||||
fetchWindowIcons
|
||||
},
|
||||
event
|
||||
}
|
||||
requestsQueue.push(request)
|
||||
if (requestsQueue.length === 1) {
|
||||
desktopCapturer.startHandling(captureWindow, captureScreen, thumbnailSize, fetchWindowIcons)
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = {
|
||||
options: {
|
||||
captureWindow,
|
||||
captureScreen,
|
||||
thumbnailSize,
|
||||
fetchWindowIcons
|
||||
},
|
||||
resolve
|
||||
}
|
||||
requestsQueue.push(request)
|
||||
if (requestsQueue.length === 1) {
|
||||
desktopCapturer.startHandling(captureWindow, captureScreen, thumbnailSize, fetchWindowIcons)
|
||||
}
|
||||
|
||||
// If the WebContents is destroyed before receiving result, just remove the
|
||||
// reference from requestsQueue to make the module not send the result to it.
|
||||
event.sender.once('destroyed', () => {
|
||||
request.event = null
|
||||
// If the WebContents is destroyed before receiving result, just remove the
|
||||
// reference from requestsQueue to make the module not send the result to it.
|
||||
event.sender.once('destroyed', () => {
|
||||
request.resolve = null
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -59,16 +56,15 @@ desktopCapturer.emit = (event, name, sources, fetchWindowIcons) => {
|
|||
}
|
||||
})
|
||||
|
||||
if (handledRequest.event) {
|
||||
handledRequest.event._replyInternal(capturerResult(handledRequest.id), result)
|
||||
if (handledRequest.resolve) {
|
||||
handledRequest.resolve(result)
|
||||
}
|
||||
|
||||
// Check the queue to see whether there is another identical request & handle
|
||||
requestsQueue.forEach(request => {
|
||||
const event = request.event
|
||||
if (deepEqual(handledRequest.options, request.options)) {
|
||||
if (event) {
|
||||
event._replyInternal(capturerResult(request.id), result)
|
||||
if (request.resolve) {
|
||||
request.resolve(result)
|
||||
}
|
||||
} else {
|
||||
unhandledRequestsQueue.push(request)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue