refactor: re-implement desktop-capturer in TypeScript (#18580)
This commit is contained in:
parent
4ef8de69ef
commit
370e9522b4
9 changed files with 127 additions and 144 deletions
|
@ -1,91 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
const { createDesktopCapturer } = process.electronBinding('desktop_capturer')
|
||||
|
||||
const deepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b)
|
||||
|
||||
let currentlyRunning = []
|
||||
|
||||
exports.getSources = (event, captureWindow, captureScreen, thumbnailSize, fetchWindowIcons) => {
|
||||
const options = {
|
||||
captureWindow,
|
||||
captureScreen,
|
||||
thumbnailSize,
|
||||
fetchWindowIcons
|
||||
}
|
||||
|
||||
for (const running of currentlyRunning) {
|
||||
if (deepEqual(running.options, options)) {
|
||||
// If a request is currently running for the same options
|
||||
// return that promise
|
||||
return running.getSources
|
||||
}
|
||||
}
|
||||
|
||||
const getSources = new Promise((resolve, reject) => {
|
||||
const stopRunning = () => {
|
||||
// Remove from currentlyRunning once we resolve or reject
|
||||
currentlyRunning = currentlyRunning.filter(running => running.options !== options)
|
||||
}
|
||||
const request = {
|
||||
options,
|
||||
resolve: (value) => {
|
||||
stopRunning()
|
||||
resolve(value)
|
||||
},
|
||||
reject: (err) => {
|
||||
stopRunning()
|
||||
reject(err)
|
||||
},
|
||||
capturer: createDesktopCapturer()
|
||||
}
|
||||
request.capturer.emit = createCapturerEmitHandler(request.capturer, request)
|
||||
request.capturer.startHandling(captureWindow, captureScreen, thumbnailSize, fetchWindowIcons)
|
||||
|
||||
// If the WebContents is destroyed before receiving result, just remove the
|
||||
// reference to resolve, emit and the capturer itself so that it never dispatches
|
||||
// back to the renderer
|
||||
event.sender.once('destroyed', () => {
|
||||
request.resolve = null
|
||||
delete request.capturer.emit
|
||||
delete request.capturer
|
||||
stopRunning()
|
||||
})
|
||||
})
|
||||
|
||||
currentlyRunning.push({
|
||||
options,
|
||||
getSources
|
||||
})
|
||||
|
||||
return getSources
|
||||
}
|
||||
|
||||
const createCapturerEmitHandler = (capturer, request) => {
|
||||
return function handlEmitOnCapturer (event, name, sources, fetchWindowIcons) {
|
||||
// Ensure that this capturer instance can only ever receive a single event
|
||||
// if we get more than one it is a bug but will also cause strange behavior
|
||||
// if we still try to handle it
|
||||
delete capturer.emit
|
||||
|
||||
if (name === 'error') {
|
||||
const error = sources
|
||||
request.reject(error)
|
||||
return
|
||||
}
|
||||
|
||||
const result = sources.map(source => {
|
||||
return {
|
||||
id: source.id,
|
||||
name: source.name,
|
||||
thumbnail: source.thumbnail.toDataURL(),
|
||||
display_id: source.display_id,
|
||||
appIcon: (fetchWindowIcons && source.appIcon) ? source.appIcon.toDataURL() : null
|
||||
}
|
||||
})
|
||||
|
||||
if (request.resolve) {
|
||||
request.resolve(result)
|
||||
}
|
||||
}
|
||||
}
|
66
lib/browser/desktop-capturer.ts
Normal file
66
lib/browser/desktop-capturer.ts
Normal file
|
@ -0,0 +1,66 @@
|
|||
import { EventEmitter } from 'events'
|
||||
|
||||
const { createDesktopCapturer } = process.electronBinding('desktop_capturer')
|
||||
|
||||
const deepEqual = (a: ElectronInternal.GetSourcesOptions, b: ElectronInternal.GetSourcesOptions) => JSON.stringify(a) === JSON.stringify(b)
|
||||
|
||||
let currentlyRunning: {
|
||||
options: ElectronInternal.GetSourcesOptions;
|
||||
getSources: Promise<ElectronInternal.GetSourcesResult[]>;
|
||||
}[] = []
|
||||
|
||||
export const getSources = (event: Electron.IpcMainEvent, options: ElectronInternal.GetSourcesOptions) => {
|
||||
for (const running of currentlyRunning) {
|
||||
if (deepEqual(running.options, options)) {
|
||||
// If a request is currently running for the same options
|
||||
// return that promise
|
||||
return running.getSources
|
||||
}
|
||||
}
|
||||
|
||||
const getSources = new Promise<ElectronInternal.GetSourcesResult[]>((resolve, reject) => {
|
||||
const stopRunning = () => {
|
||||
// Remove from currentlyRunning once we resolve or reject
|
||||
currentlyRunning = currentlyRunning.filter(running => running.options !== options)
|
||||
}
|
||||
|
||||
const emitter = new EventEmitter()
|
||||
|
||||
emitter.once('error', (event, error: string) => {
|
||||
stopRunning()
|
||||
reject(error)
|
||||
})
|
||||
|
||||
emitter.once('finished', (event, sources: Electron.DesktopCapturerSource[], fetchWindowIcons: boolean) => {
|
||||
stopRunning()
|
||||
resolve(sources.map(source => ({
|
||||
id: source.id,
|
||||
name: source.name,
|
||||
thumbnail: source.thumbnail.toDataURL(),
|
||||
display_id: source.display_id,
|
||||
appIcon: (fetchWindowIcons && source.appIcon) ? source.appIcon.toDataURL() : null
|
||||
})))
|
||||
})
|
||||
|
||||
let capturer: ElectronInternal.DesktopCapturer | null = createDesktopCapturer()
|
||||
|
||||
capturer.emit = emitter.emit.bind(emitter)
|
||||
capturer.startHandling(options.captureWindow, options.captureScreen, options.thumbnailSize, options.fetchWindowIcons)
|
||||
|
||||
// If the WebContents is destroyed before receiving result, just remove the
|
||||
// reference to emit and the capturer itself so that it never dispatches
|
||||
// back to the renderer
|
||||
event.sender.once('destroyed', () => {
|
||||
capturer!.emit = null
|
||||
capturer = null
|
||||
stopRunning()
|
||||
})
|
||||
})
|
||||
|
||||
currentlyRunning.push({
|
||||
options,
|
||||
getSources
|
||||
})
|
||||
|
||||
return getSources
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue