Fix always passing the first result to renderer when the API is called multiple
time at once.
This commit is contained in:
parent
fb4efec55d
commit
9c861b9ad3
2 changed files with 11 additions and 7 deletions
|
@ -4,13 +4,13 @@ ipc = require 'ipc'
|
||||||
desktopCapturer = process.atomBinding('desktop_capturer').desktopCapturer
|
desktopCapturer = process.atomBinding('desktop_capturer').desktopCapturer
|
||||||
|
|
||||||
isOptionsEqual = (opt1, opt2) ->
|
isOptionsEqual = (opt1, opt2) ->
|
||||||
return JSON.stringify opt1 is JSON.stringify opt2
|
return JSON.stringify(opt1) is JSON.stringify(opt2)
|
||||||
|
|
||||||
# A queue for holding all requests from renderer process.
|
# A queue for holding all requests from renderer process.
|
||||||
requestsQueue = []
|
requestsQueue = []
|
||||||
|
|
||||||
ipc.on 'ATOM_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', (event, options) ->
|
ipc.on 'ATOM_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', (event, options, id) ->
|
||||||
request = { options: options, webContents: event.sender }
|
request = { id: id, options: options, webContents: event.sender }
|
||||||
requestsQueue.push request
|
requestsQueue.push request
|
||||||
desktopCapturer.startHandling options if requestsQueue.length is 1
|
desktopCapturer.startHandling options if requestsQueue.length is 1
|
||||||
# If the WebContents is destroyed before receiving result, just remove the
|
# If the WebContents is destroyed before receiving result, just remove the
|
||||||
|
@ -23,14 +23,14 @@ desktopCapturer.emit = (event_name, event, error_message, sources) ->
|
||||||
handledRequest = requestsQueue.shift 0
|
handledRequest = requestsQueue.shift 0
|
||||||
error = if error_message then Error error_message else null
|
error = if error_message then Error error_message else null
|
||||||
result = ({ id: source.id, name: source.name, thumbnail: source.thumbnail.toDataUrl() } for source in sources)
|
result = ({ id: source.id, name: source.name, thumbnail: source.thumbnail.toDataUrl() } for source in sources)
|
||||||
handledRequest.webContents?.send 'ATOM_RENDERER_DESKTOP_CAPTURER_RESULT', error_message, result
|
handledRequest.webContents?.send "ATOM_RENDERER_DESKTOP_CAPTURER_RESULT_#{handledRequest.id}", error_message, result
|
||||||
|
|
||||||
# Check the queue to see whether there is other same request. If has, handle
|
# Check the queue to see whether there is other same request. If has, handle
|
||||||
# it for reducing redunplicated `desktopCaptuer.startHandling` calls.
|
# it for reducing redunplicated `desktopCaptuer.startHandling` calls.
|
||||||
unhandledRequestsQueue = []
|
unhandledRequestsQueue = []
|
||||||
for request in requestsQueue
|
for request in requestsQueue
|
||||||
if isOptionsEqual handledRequest.options, request.options
|
if isOptionsEqual handledRequest.options, request.options
|
||||||
request.webContents?.send 'ATOM_RENDERER_DESKTOP_CAPTURER_RESULT', error_message, result
|
request.webContents?.send "ATOM_RENDERER_DESKTOP_CAPTURER_RESULT_#{request.id}", error_message, result
|
||||||
else
|
else
|
||||||
unhandledRequestsQueue.push request
|
unhandledRequestsQueue.push request
|
||||||
requestsQueue = unhandledRequestsQueue
|
requestsQueue = unhandledRequestsQueue
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
ipc = require 'ipc'
|
ipc = require 'ipc'
|
||||||
NativeImage = require 'native-image'
|
NativeImage = require 'native-image'
|
||||||
|
|
||||||
|
nextId = 0
|
||||||
|
getNextId = -> ++nextId
|
||||||
|
|
||||||
getSources = (options, callback) ->
|
getSources = (options, callback) ->
|
||||||
ipc.send 'ATOM_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', options
|
id = getNextId()
|
||||||
ipc.once 'ATOM_RENDERER_DESKTOP_CAPTURER_RESULT', (error_message, sources) ->
|
ipc.send 'ATOM_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', options, id
|
||||||
|
ipc.once "ATOM_RENDERER_DESKTOP_CAPTURER_RESULT_#{id}", (error_message, sources) ->
|
||||||
error = if error_message then Error error_message else null
|
error = if error_message then Error error_message else null
|
||||||
callback error, ({id: source.id, name: source.name, thumbnail: NativeImage.createFromDataUrl source.thumbnail} for source in sources)
|
callback error, ({id: source.id, name: source.name, thumbnail: NativeImage.createFromDataUrl source.thumbnail} for source in sources)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue