🎨 Support non-native promises

This commit is contained in:
cdaringe 2016-05-24 23:38:35 -06:00
parent 4cd0de0e87
commit 32073fa079
No known key found for this signature in database
GPG key ID: 8C3201E7E66E08D0
5 changed files with 24 additions and 4 deletions

View file

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

View file

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

View file

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

View file

@ -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
)
}

View file

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