From 32073fa079c26400cb805a12e8f1b6feb54d0c32 Mon Sep 17 00:00:00 2001 From: cdaringe Date: Tue, 24 May 2016 23:38:35 -0600 Subject: [PATCH] :art: Support non-native promises --- filenames.gypi | 1 + lib/browser/rpc-server.js | 4 ++-- lib/common/api/exports/electron.js | 5 +++++ lib/common/api/is-promise.js | 14 ++++++++++++++ lib/renderer/api/remote.js | 4 ++-- 5 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 lib/common/api/is-promise.js diff --git a/filenames.gypi b/filenames.gypi index cc33d9a81a67..14d01270f089 100644 --- a/filenames.gypi +++ b/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', diff --git a/lib/browser/rpc-server.js b/lib/browser/rpc-server.js index 7ad6d2c22aef..fe264cceeeff 100644 --- a/lib/browser/rpc-server.js +++ b/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. diff --git a/lib/common/api/exports/electron.js b/lib/common/api/exports/electron.js index 1e9c2d71f3b7..efa44d452cc6 100644 --- a/lib/common/api/exports/electron.js +++ b/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') + } } }) } diff --git a/lib/common/api/is-promise.js b/lib/common/api/is-promise.js new file mode 100644 index 000000000000..d6115a145585 --- /dev/null +++ b/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 + ) +} diff --git a/lib/renderer/api/remote.js b/lib/renderer/api/remote.js index d3fdec25aeb7..b5c9fe3a0e49 100644 --- a/lib/renderer/api/remote.js +++ b/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) {