Fix always passing the first result to renderer when the API is called multiple

time at once.
This commit is contained in:
Haojian Wu 2015-10-19 18:07:35 +08:00
parent fb4efec55d
commit 9c861b9ad3
2 changed files with 11 additions and 7 deletions

View file

@ -4,13 +4,13 @@ ipc = require 'ipc'
desktopCapturer = process.atomBinding('desktop_capturer').desktopCapturer
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.
requestsQueue = []
ipc.on 'ATOM_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', (event, options) ->
request = { options: options, webContents: event.sender }
ipc.on 'ATOM_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', (event, options, id) ->
request = { id: id, options: options, webContents: event.sender }
requestsQueue.push request
desktopCapturer.startHandling options if requestsQueue.length is 1
# 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
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)
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
# it for reducing redunplicated `desktopCaptuer.startHandling` calls.
unhandledRequestsQueue = []
for request in requestsQueue
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
unhandledRequestsQueue.push request
requestsQueue = unhandledRequestsQueue

View file

@ -1,9 +1,13 @@
ipc = require 'ipc'
NativeImage = require 'native-image'
nextId = 0
getNextId = -> ++nextId
getSources = (options, callback) ->
ipc.send 'ATOM_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', options
ipc.once 'ATOM_RENDERER_DESKTOP_CAPTURER_RESULT', (error_message, sources) ->
id = getNextId()
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
callback error, ({id: source.id, name: source.name, thumbnail: NativeImage.createFromDataUrl source.thumbnail} for source in sources)