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,15 +1,7 @@
|
|||
'use strict'
|
||||
|
||||
const { nativeImage, deprecate } = require('electron')
|
||||
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal')
|
||||
|
||||
const includes = [].includes
|
||||
let currentId = 0
|
||||
|
||||
const incrementId = () => {
|
||||
currentId += 1
|
||||
return currentId
|
||||
}
|
||||
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
|
||||
|
||||
// |options.types| can't be empty and must be an array
|
||||
function isValid (options) {
|
||||
|
@ -31,8 +23,8 @@ const getSources = (options) => {
|
|||
return new Promise((resolve, reject) => {
|
||||
if (!isValid(options)) throw new Error('Invalid options')
|
||||
|
||||
const captureWindow = includes.call(options.types, 'window')
|
||||
const captureScreen = includes.call(options.types, 'screen')
|
||||
const captureWindow = options.types.includes('window')
|
||||
const captureScreen = options.types.includes('screen')
|
||||
|
||||
if (options.thumbnailSize == null) {
|
||||
options.thumbnailSize = {
|
||||
|
@ -44,15 +36,8 @@ const getSources = (options) => {
|
|||
options.fetchWindowIcons = false
|
||||
}
|
||||
|
||||
const id = incrementId()
|
||||
ipcRendererInternal.send('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', captureWindow, captureScreen, options.thumbnailSize, options.fetchWindowIcons, id)
|
||||
return ipcRendererInternal.once(`ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_${id}`, (event, sources) => {
|
||||
try {
|
||||
resolve(mapSources(sources))
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
ipcRendererUtils.invoke('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', captureWindow, captureScreen, options.thumbnailSize, options.fetchWindowIcons)
|
||||
.then(sources => resolve(mapSources(sources)), reject)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue