]> khamidou — GitHub mirrors - electron.git/commitdiff
Calling asynchronous functions in renderer now doesn't block browser.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 24 Sep 2013 10:01:12 +0000 (18:01 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 24 Sep 2013 10:01:12 +0000 (18:01 +0800)
browser/api/lib/dialog.coffee
browser/atom/rpc-server.coffee

index 7cf538b2f410c0914611bbb834f74fd98357b1f5..5b333f4a13b574c775f7fadc71502e5b04748eb6 100644 (file)
@@ -1,4 +1,5 @@
 binding = process.atomBinding 'dialog'
+v8Util = process.atomBinding 'v8_util'
 BrowserWindow = require 'browser-window'
 
 fileDialogProperties =
@@ -72,3 +73,6 @@ module.exports =
                            String(options.detail),
                            window,
                            callback
+
+# Mark standard asynchronous functions.
+v8Util.setHiddenValue f, 'asynchronous', true for k, f of module.exports
index 0e5c707c403d25c80a5b0c318daf15c394020584..04d5fe2c9484bdf5f98f151ab0ea71fba5292c58 100644 (file)
@@ -61,6 +61,17 @@ unwrapArgs = (processId, routingId, args) ->
 
   args.map metaToValue
 
+# Call a function and send reply asynchronously if it's a an asynchronous
+# style function and the caller didn't pass a callback.
+callFunction = (event, processId, routingId, func, caller, args) ->
+  if v8Util.getHiddenValue(func, 'asynchronous') and typeof args[args.length - 1] isnt 'function'
+    args.push (ret) ->
+      event.returnValue = valueToMeta processId, routingId, ret
+    func.apply caller, args
+  else
+    ret = func.apply caller, args
+    event.returnValue = valueToMeta processId, routingId, ret
+
 ipc.on 'ATOM_BROWSER_REQUIRE', (event, processId, routingId, module) ->
   try
     event.returnValue = valueToMeta processId, routingId, require(module)
@@ -100,8 +111,7 @@ ipc.on 'ATOM_BROWSER_FUNCTION_CALL', (event, processId, routingId, id, args) ->
   try
     args = unwrapArgs processId, routingId, args
     func = objectsRegistry.get id
-    ret = func.apply global, args
-    event.returnValue = valueToMeta processId, routingId, ret
+    callFunction event, processId, routingId, func, global, args
   catch e
     event.returnValue = errorToMeta e
 
@@ -119,8 +129,7 @@ ipc.on 'ATOM_BROWSER_MEMBER_CALL', (event, processId, routingId, id, method, arg
   try
     args = unwrapArgs processId, routingId, args
     obj = objectsRegistry.get id
-    ret = obj[method].apply(obj, args)
-    event.returnValue = valueToMeta processId, routingId, ret
+    callFunction event, processId, routingId, obj[method], obj, args
   catch e
     event.returnValue = errorToMeta e