From 9c861b9ad37a9c6335dde2e59d3005742fe75150 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Mon, 19 Oct 2015 18:07:35 +0800 Subject: [PATCH] Fix always passing the first result to renderer when the API is called multiple time at once. --- atom/browser/lib/desktop-capturer.coffee | 10 +++++----- atom/renderer/api/lib/desktop-capturer.coffee | 8 ++++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/atom/browser/lib/desktop-capturer.coffee b/atom/browser/lib/desktop-capturer.coffee index 916eda7b68a..29d952595cb 100644 --- a/atom/browser/lib/desktop-capturer.coffee +++ b/atom/browser/lib/desktop-capturer.coffee @@ -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 diff --git a/atom/renderer/api/lib/desktop-capturer.coffee b/atom/renderer/api/lib/desktop-capturer.coffee index 5c8caf076a3..95e67ff9992 100644 --- a/atom/renderer/api/lib/desktop-capturer.coffee +++ b/atom/renderer/api/lib/desktop-capturer.coffee @@ -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)