diff options
author | Dan Finlay <dan@danfinlay.com> | 2016-11-29 04:43:44 +0800 |
---|---|---|
committer | Dan Finlay <dan@danfinlay.com> | 2016-11-29 08:13:03 +0800 |
commit | 80e76b45ee67900b5a60da1ddcd8b310f1e92413 (patch) | |
tree | 4eff8a2a7718e25afe8958198e3880e22530fdbb /app/scripts/lib | |
parent | b8991195829474691f17db3a7a6c9c081f9a95c9 (diff) | |
download | tangerine-wallet-browser-80e76b45ee67900b5a60da1ddcd8b310f1e92413.tar tangerine-wallet-browser-80e76b45ee67900b5a60da1ddcd8b310f1e92413.tar.gz tangerine-wallet-browser-80e76b45ee67900b5a60da1ddcd8b310f1e92413.tar.bz2 tangerine-wallet-browser-80e76b45ee67900b5a60da1ddcd8b310f1e92413.tar.lz tangerine-wallet-browser-80e76b45ee67900b5a60da1ddcd8b310f1e92413.tar.xz tangerine-wallet-browser-80e76b45ee67900b5a60da1ddcd8b310f1e92413.tar.zst tangerine-wallet-browser-80e76b45ee67900b5a60da1ddcd8b310f1e92413.zip |
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.
Diffstat (limited to 'app/scripts/lib')
-rw-r--r-- | app/scripts/lib/nodeify.js | 59 |
1 files changed, 59 insertions, 0 deletions
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 |