Browse Source

:art: Support non-native promises

cdaringe 9 years ago
parent
commit
32073fa079

+ 1 - 0
filenames.gypi

@@ -42,6 +42,7 @@
       'lib/common/api/crash-reporter.js',
       'lib/common/api/deprecate.js',
       'lib/common/api/deprecations.js',
+      'lib/common/api/is-promise.js',
       'lib/common/api/exports/electron.js',
       'lib/common/api/native-image.js',
       'lib/common/api/shell.js',

+ 2 - 2
lib/browser/rpc-server.js

@@ -2,7 +2,7 @@
 
 const electron = require('electron')
 const v8Util = process.atomBinding('v8_util')
-const {ipcMain} = electron
+const {ipcMain, isPromise} = electron
 
 const objectsRegistry = require('./objects-registry')
 
@@ -65,7 +65,7 @@ let valueToMeta = function (sender, value, optimizeSimpleObject = false) {
       meta.type = 'error'
     } else if (value instanceof Date) {
       meta.type = 'date'
-    } else if (value.constructor != null && value.constructor.name === 'Promise') {
+    } else if (isPromise(value)) {
       meta.type = 'promise'
     } else if (value.hasOwnProperty('callee') && value.length != null) {
       // Treat the arguments object as array.

+ 5 - 0
lib/common/api/exports/electron.js

@@ -43,6 +43,11 @@ exports.defineProperties = function (exports) {
       get: function () {
         return require('../deprecations')
       }
+    },
+    isPromise: {
+      get: function () {
+        return require('../is-promise')
+      }
     }
   })
 }

+ 14 - 0
lib/common/api/is-promise.js

@@ -0,0 +1,14 @@
+'use strict'
+
+module.exports = function isPromise (val) {
+  return (
+    val &&
+    val.then &&
+    val.then instanceof Function &&
+    val.constructor &&
+    val.constructor.reject &&
+    val.constructor.reject instanceof Function &&
+    val.constructor.resolve &&
+    val.constructor.resolve instanceof Function
+  )
+}

+ 2 - 2
lib/renderer/api/remote.js

@@ -1,7 +1,7 @@
 'use strict'
 
 const v8Util = process.atomBinding('v8_util')
-const {ipcRenderer, CallbacksRegistry} = require('electron')
+const {ipcRenderer, isPromise, CallbacksRegistry} = require('electron')
 
 const callbacksRegistry = new CallbacksRegistry()
 
@@ -44,7 +44,7 @@ var wrapArgs = function (args, visited) {
         value: value.getTime()
       }
     } else if ((value != null) && typeof value === 'object') {
-      if (value.constructor != null && value.constructor.name === 'Promise') {
+      if (isPromise(value)) {
         return {
           type: 'promise',
           then: valueToMeta(function (onFulfilled, onRejected) {