From 473b88f399478b47bfa53d44ef9981aeb6d9960b Mon Sep 17 00:00:00 2001 From: frankiebee Date: Wed, 24 May 2017 22:13:35 -0400 Subject: [PATCH 1/3] Reload the page when switching networks for sites that use web3 --- app/scripts/contentscript.js | 1 - app/scripts/inpage.js | 21 +++------------ app/scripts/lib/auto-reload.js | 47 ++++++++++++++++++---------------- 3 files changed, 28 insertions(+), 41 deletions(-) diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index f7237b32e..291b922e8 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -61,7 +61,6 @@ function setupStreams () { // ignore unused channels (handled by background) mx.ignoreStream('provider') mx.ignoreStream('publicConfig') - mx.ignoreStream('reload') } function shouldInjectWeb3 () { diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index 419f78cd6..ec764535e 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -31,26 +31,11 @@ web3.setProvider = function () { console.log('MetaMask - overrode web3.setProvider') } console.log('MetaMask - injected web3') -// export global web3, with usage-detection reload fn -var triggerReload = setupDappAutoReload(web3) - -// listen for reset requests from metamask -var reloadStream = inpageProvider.multiStream.createStream('reload') -reloadStream.once('data', triggerReload) - -// setup ping timeout autoreload -// LocalMessageDuplexStream does not self-close, so reload if pingStream fails -// var pingChannel = inpageProvider.multiStream.createStream('pingpong') -// var pingStream = new PingStream({ objectMode: true }) -// wait for first successful reponse - -// disable pingStream until https://github.com/MetaMask/metamask-plugin/issues/746 is resolved more gracefully -// metamaskStream.once('data', function(){ -// pingStream.pipe(pingChannel).pipe(pingStream) -// }) -// endOfStream(pingStream, triggerReload) +// export global web3, with usage-detection +setupDappAutoReload(web3, inpageProvider.publicConfigStore) // set web3 defaultAccount + inpageProvider.publicConfigStore.subscribe(function (state) { web3.eth.defaultAccount = state.selectedAddress }) diff --git a/app/scripts/lib/auto-reload.js b/app/scripts/lib/auto-reload.js index 1302df35f..30ddd2395 100644 --- a/app/scripts/lib/auto-reload.js +++ b/app/scripts/lib/auto-reload.js @@ -1,30 +1,33 @@ -const once = require('once') -const ensnare = require('ensnare') - module.exports = setupDappAutoReload -function setupDappAutoReload (web3) { +function setupDappAutoReload (web3, observable) { // export web3 as a global, checking for usage - var pageIsUsingWeb3 = false - var resetWasRequested = false - global.web3 = ensnare(web3, once(function () { - // if web3 usage happened after a reset request, trigger reset late - if (resetWasRequested) return triggerReset() - // mark web3 as used - pageIsUsingWeb3 = true - // reset web3 reference - global.web3 = web3 - })) + global.web3 = new Proxy(web3, { + get: (_web3, name) => { + // get the time of use + if (name !== '_used') _web3._used = Date.now() + return _web3[name] + }, + set: (_web3, name, value) => { + _web3[name] = value + }, + }) + var networkVersion - return handleResetRequest + observable.subscribe(function (state) { + // get the initial network + const curentNetVersion = state.networkVersion + if (!networkVersion) networkVersion = curentNetVersion - function handleResetRequest () { - resetWasRequested = true - // ignore if web3 was not used - if (!pageIsUsingWeb3) return - // reload after short timeout - setTimeout(triggerReset, 500) - } + if (curentNetVersion !== networkVersion && web3._used) { + const timeSenseUse = Date.now() - web3._used + // if web3 was recently used then delay the reloading of the page + timeSenseUse > 500 ? triggerReset() : setTimeout(triggerReset, 500) + // prevent reentry into if statement if state updates again before + // reload + networkVersion = curentNetVersion + } + }) } // reload the page From a2064bd16e3f37c537ef6c818a5a10024ef24a2a Mon Sep 17 00:00:00 2001 From: frankiebee Date: Wed, 24 May 2017 23:18:09 -0400 Subject: [PATCH 2/3] add to CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index caa87b697..b755e3999 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Current Master +- Now when switching networks sites that use web3 will reload - Now when switching networks the extension does not restart - Cleanup decimal bugs in our gas inputs. - Fix bug where submit button was enabled for invalid gas inputs. From 717dceede84980050420fc3e3ff015caf2bcd553 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Wed, 24 May 2017 23:36:10 -0400 Subject: [PATCH 3/3] fix spelling --- app/scripts/lib/auto-reload.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/scripts/lib/auto-reload.js b/app/scripts/lib/auto-reload.js index 30ddd2395..534047330 100644 --- a/app/scripts/lib/auto-reload.js +++ b/app/scripts/lib/auto-reload.js @@ -20,9 +20,9 @@ function setupDappAutoReload (web3, observable) { if (!networkVersion) networkVersion = curentNetVersion if (curentNetVersion !== networkVersion && web3._used) { - const timeSenseUse = Date.now() - web3._used + const timeSinceUse = Date.now() - web3._used // if web3 was recently used then delay the reloading of the page - timeSenseUse > 500 ? triggerReset() : setTimeout(triggerReset, 500) + timeSinceUse > 500 ? triggerReset() : setTimeout(triggerReset, 500) // prevent reentry into if statement if state updates again before // reload networkVersion = curentNetVersion