From f35466a247e503659228f0b8e098275a875026a4 Mon Sep 17 00:00:00 2001 From: bitpshr Date: Tue, 23 Oct 2018 13:19:01 -0400 Subject: [PATCH] Fix race condition with publicConfigStore --- app/scripts/inpage.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index 9d50a695f..cadc61727 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -40,14 +40,33 @@ inpageProvider.enable = function () { if (typeof detail.error !== 'undefined') { reject(detail.error) } else { - inpageProvider.sendAsync({ method: 'eth_accounts', params: [] }, (error, response) => { - if (error) { - reject(error) + const publicConfig = new Promise((resolve) => { + const { selectedAddress } = inpageProvider.publicConfigStore.getState() + if (selectedAddress) { + resolve() } else { - isEnabled = true - resolve(response.result) + inpageProvider.publicConfigStore.on('update', ({ selectedAddress }) => { + selectedAddress && resolve() + }) } }) + + const ethAccounts = new Promise((resolveAccounts, rejectAccounts) => { + inpageProvider.sendAsync({ method: 'eth_accounts', params: [] }, (error, response) => { + if (error) { + rejectAccounts(error) + } else { + resolveAccounts(response.result) + } + }) + }) + + Promise.all([ethAccounts, publicConfig]) + .then(([selectedAddress]) => { + isEnabled = true + resolve(selectedAddress) + }) + .catch(reject) } }) window.postMessage({ type: 'ETHEREUM_ENABLE_PROVIDER' }, '*')