From 80e76b45ee67900b5a60da1ddcd8b310f1e92413 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 28 Nov 2016 12:43:44 -0800 Subject: Denodeify most of KeyringController Mostly Fixes #893 A couple methods cache callbacks, and will require a larger refactor to fully denodeify. Specifically, our methods involving web3 requests to sign a tx, sign a message, and approve or cancel either of those. I think we should postpone those until the TxManager refactor, since it will likely handle this response caching itself. --- app/scripts/lib/nodeify.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 app/scripts/lib/nodeify.js (limited to 'app/scripts/lib/nodeify.js') diff --git a/app/scripts/lib/nodeify.js b/app/scripts/lib/nodeify.js new file mode 100644 index 000000000..f48df34ef --- /dev/null +++ b/app/scripts/lib/nodeify.js @@ -0,0 +1,59 @@ +/* NODEIFY + * Modified from original npm package "nodeify" + * https://github.com/then/nodeify + * + * Removed Promise dependency, to only support + * native Promises and reduce bundle size. + */ + +var isPromise = require('is-promise') + +var nextTick +if (typeof setImmediate === 'function') nextTick = setImmediate +else if (typeof process === 'object' && process && process.nextTick) nextTick = process.nextTick +else nextTick = function (cb) { setTimeout(cb, 0) } + +module.exports = nodeify +function nodeify(promise, cb) { + if (typeof cb !== 'function') return promise + return promise + .then(function (res) { + nextTick(function () { + cb(null, res) + }) + }, function (err) { + nextTick(function () { + cb(err) + }) + }) +} +function nodeifyThis(cb) { + return nodeify(this, cb) +} + +nodeify.extend = extend +nodeify.Promise = NodeifyPromise + +function extend(prom) { + if (prom && isPromise(prom)) { + prom.nodeify = nodeifyThis + var then = prom.then + prom.then = function () { + return extend(then.apply(this, arguments)) + } + return prom + } else if (typeof prom === 'function') { + prom.prototype.nodeify = nodeifyThis + } +} + +function NodeifyPromise(fn) { + if (!(this instanceof NodeifyPromise)) { + return new NodeifyPromise(fn) + } + Promise.call(this, fn) + extend(this) +} + +NodeifyPromise.prototype = Object.create(Promise.prototype) +NodeifyPromise.prototype.constructor = NodeifyPromise -- cgit v1.2.3 From 9e764b193517c935fa04d4722357cb48abcfb2a2 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 28 Nov 2016 17:27:20 -0800 Subject: Fix nodeify --- app/scripts/lib/nodeify.js | 68 +++++++++------------------------------------- 1 file changed, 13 insertions(+), 55 deletions(-) (limited to 'app/scripts/lib/nodeify.js') diff --git a/app/scripts/lib/nodeify.js b/app/scripts/lib/nodeify.js index f48df34ef..56b793852 100644 --- a/app/scripts/lib/nodeify.js +++ b/app/scripts/lib/nodeify.js @@ -1,59 +1,17 @@ -/* NODEIFY - * Modified from original npm package "nodeify" - * https://github.com/then/nodeify - * - * Removed Promise dependency, to only support - * native Promises and reduce bundle size. - */ - -var isPromise = require('is-promise') - -var nextTick -if (typeof setImmediate === 'function') nextTick = setImmediate -else if (typeof process === 'object' && process && process.nextTick) nextTick = process.nextTick -else nextTick = function (cb) { setTimeout(cb, 0) } - -module.exports = nodeify -function nodeify(promise, cb) { - if (typeof cb !== 'function') return promise - return promise - .then(function (res) { - nextTick(function () { - cb(null, res) - }) - }, function (err) { - nextTick(function () { - cb(err) - }) - }) -} -function nodeifyThis(cb) { - return nodeify(this, cb) -} - -nodeify.extend = extend -nodeify.Promise = NodeifyPromise - -function extend(prom) { - if (prom && isPromise(prom)) { - prom.nodeify = nodeifyThis - var then = prom.then - prom.then = function () { - return extend(then.apply(this, arguments)) +module.exports = function (promiseFn) { + return function () { + var args = [] + for (var i = 0; i < arguments.length - 1; i++) { + args.push(arguments[i]) } - return prom - } else if (typeof prom === 'function') { - prom.prototype.nodeify = nodeifyThis - } -} + var cb = arguments[arguments.length - 1] -function NodeifyPromise(fn) { - if (!(this instanceof NodeifyPromise)) { - return new NodeifyPromise(fn) + return promiseFn.apply(this, args) + .then(function (result) { + cb(null, result) + }) + .catch(function (reason) { + cb(reason) + }) } - Promise.call(this, fn) - extend(this) } - -NodeifyPromise.prototype = Object.create(Promise.prototype) -NodeifyPromise.prototype.constructor = NodeifyPromise -- cgit v1.2.3 From 95bcc21a066d0849b0b5399a8d7825b01d6390aa Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 29 Nov 2016 15:54:10 -0800 Subject: Add useful nodeify error message If a nodified method does not return a Promise, it will throw an error, like this: ``` Error in event handler for (unknown): Error: The function setSelectedAccount did not return a Promise, but was nodeified. ``` --- app/scripts/lib/nodeify.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'app/scripts/lib/nodeify.js') diff --git a/app/scripts/lib/nodeify.js b/app/scripts/lib/nodeify.js index 56b793852..51d89a8fb 100644 --- a/app/scripts/lib/nodeify.js +++ b/app/scripts/lib/nodeify.js @@ -6,12 +6,19 @@ module.exports = function (promiseFn) { } var cb = arguments[arguments.length - 1] - return promiseFn.apply(this, args) - .then(function (result) { + const nodeified = promiseFn.apply(this, args) + + if (!nodeified) { + const methodName = String(promiseFn).split('(')[0] + throw new Error(`The ${methodName} did not return a Promise, but was nodeified.`) + } + nodeified.then(function (result) { cb(null, result) }) .catch(function (reason) { cb(reason) }) + + return nodeified } } -- cgit v1.2.3