diff --git a/app/scripts/popup-core.js b/app/scripts/popup-core.js index b1e521a7a..1e5d70e8b 100644 --- a/app/scripts/popup-core.js +++ b/app/scripts/popup-core.js @@ -1,7 +1,8 @@ const EventEmitter = require('events').EventEmitter +const async = require('async') const Dnode = require('dnode') const Web3 = require('web3') -const MetaMaskUi = require('../../ui') +const launchMetamaskUi = require('../../ui') const StreamProvider = require('web3-stream-provider') const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex @@ -9,9 +10,13 @@ const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex module.exports = initializePopup -function initializePopup (connectionStream) { +function initializePopup ({ container, connectionStream }, cb) { // setup app - connectToAccountManager(connectionStream, setupApp) + async.waterfall([ + (cb) => connectToAccountManager(connectionStream, cb), + (accountManager, cb) => launchMetamaskUi({ container, accountManager }, cb), + ], cb) + } function connectToAccountManager (connectionStream, cb) { @@ -47,19 +52,3 @@ function setupControllerConnection (connectionStream, cb) { cb(null, accountManager) }) } - -function setupApp (err, accountManager) { - var container = document.getElementById('app-content') - if (err) { - container.innerHTML = '
The MetaMask app failed to load: please open and close MetaMask again to restart.
' - container.style.height = '80px' - log.error(err.stack) - throw err - } - - - MetaMaskUi({ - container: container, - accountManager: accountManager, - }) -} diff --git a/app/scripts/popup.js b/app/scripts/popup.js index 6606c9584..f6dcb829b 100644 --- a/app/scripts/popup.js +++ b/app/scripts/popup.js @@ -5,23 +5,37 @@ const PortStream = require('./lib/port-stream.js') const isPopupOrNotification = require('./lib/is-popup-or-notification') const extension = require('extensionizer') const NotificationManager = require('./lib/notification-manager') - const notificationManager = new NotificationManager() -var css = MetaMaskUiCss() +// inject css +const css = MetaMaskUiCss() injectCss(css) -var name = isPopupOrNotification() -closePopupIfOpen(name) -window.METAMASK_UI_TYPE = name +// identify window type (popup, notification) +const windowType = isPopupOrNotification() +global.METAMASK_UI_TYPE = windowType +closePopupIfOpen(windowType) -var pluginPort = extension.runtime.connect({ name }) -var portStream = new PortStream(pluginPort) +// setup stream to background +const extensionPort = extension.runtime.connect({ windowType }) +const connectionStream = new PortStream(extensionPort) -startPopup(portStream) +// start ui +const container = document.getElementById('app-content') +startPopup({ container, connectionStream }, (err, store) => { + if (err) return displayCriticalError(err) +}) -function closePopupIfOpen (name) { - if (name !== 'notification') { + +function closePopupIfOpen (windowType) { + if (windowType !== 'notification') { notificationManager.closePopup() } } + +function displayCriticalError(err) { + container.innerHTML = '
The MetaMask app failed to load: please open and close MetaMask again to restart.
' + container.style.height = '80px' + log.error(err.stack) + throw err +} diff --git a/ui/app/reducers/app.js b/ui/app/reducers/app.js index e0c001546..46b27fe25 100644 --- a/ui/app/reducers/app.js +++ b/ui/app/reducers/app.js @@ -34,6 +34,7 @@ function reduceApp (state, action) { seedWords, } + // default state var appState = extend({ menuOpen: false, currentView: seedWords ? seedConfView : defaultView, diff --git a/ui/index.js b/ui/index.js index 1a65f813c..e3648c374 100644 --- a/ui/index.js +++ b/ui/index.js @@ -4,27 +4,28 @@ const Root = require('./app/root') const actions = require('./app/actions') const configureStore = require('./app/store') const txHelper = require('./lib/tx-helper') -module.exports = launchApp +global.log = require('loglevel') -let debugMode = window.METAMASK_DEBUG -const log = require('loglevel') -window.log = log -log.setLevel(debugMode ? 'debug' : 'warn') +module.exports = launchMetamaskUi -function launchApp (opts) { + +log.setLevel(global.METAMASK_DEBUG ? 'debug' : 'warn') + +function launchMetamaskUi (opts, cb) { var accountManager = opts.accountManager actions._setBackgroundConnection(accountManager) // check if we are unlocked first accountManager.getState(function (err, metamaskState) { - if (err) throw err - startApp(metamaskState, accountManager, opts) + if (err) return cb(err) + const store = startApp(metamaskState, accountManager, opts) + cb(null, store) }) } function startApp (metamaskState, accountManager, opts) { // parse opts - var store = configureStore({ + const store = configureStore({ // metamaskState represents the cross-tab state metamask: metamaskState, @@ -37,7 +38,7 @@ function startApp (metamaskState, accountManager, opts) { }) // if unconfirmed txs, start on txConf page - var unapprovedTxsAll = txHelper(metamaskState.unapprovedTxs, metamaskState.unapprovedMsgs, metamaskState.unapprovedPersonalMsgs, metamaskState.network) + const unapprovedTxsAll = txHelper(metamaskState.unapprovedTxs, metamaskState.unapprovedMsgs, metamaskState.unapprovedPersonalMsgs, metamaskState.network) if (unapprovedTxsAll.length > 0) { store.dispatch(actions.showConfTxPage()) } @@ -53,4 +54,6 @@ function startApp (metamaskState, accountManager, opts) { store: store, } ), opts.container) + + return store }