mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Merge pull request #1496 from MetaMask/reloadDapp
Reload the page when switching networks for sites that use web3
This commit is contained in:
commit
3724754dcc
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
## Current Master
|
## Current Master
|
||||||
|
|
||||||
|
- Now when switching networks sites that use web3 will reload
|
||||||
- Now when switching networks the extension does not restart
|
- Now when switching networks the extension does not restart
|
||||||
- Cleanup decimal bugs in our gas inputs.
|
- Cleanup decimal bugs in our gas inputs.
|
||||||
- Fix bug where submit button was enabled for invalid gas inputs.
|
- Fix bug where submit button was enabled for invalid gas inputs.
|
||||||
|
@ -61,7 +61,6 @@ function setupStreams () {
|
|||||||
// ignore unused channels (handled by background)
|
// ignore unused channels (handled by background)
|
||||||
mx.ignoreStream('provider')
|
mx.ignoreStream('provider')
|
||||||
mx.ignoreStream('publicConfig')
|
mx.ignoreStream('publicConfig')
|
||||||
mx.ignoreStream('reload')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function shouldInjectWeb3 () {
|
function shouldInjectWeb3 () {
|
||||||
|
@ -31,26 +31,11 @@ web3.setProvider = function () {
|
|||||||
console.log('MetaMask - overrode web3.setProvider')
|
console.log('MetaMask - overrode web3.setProvider')
|
||||||
}
|
}
|
||||||
console.log('MetaMask - injected web3')
|
console.log('MetaMask - injected web3')
|
||||||
// export global web3, with usage-detection reload fn
|
// export global web3, with usage-detection
|
||||||
var triggerReload = setupDappAutoReload(web3)
|
setupDappAutoReload(web3, inpageProvider.publicConfigStore)
|
||||||
|
|
||||||
// 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)
|
|
||||||
|
|
||||||
// set web3 defaultAccount
|
// set web3 defaultAccount
|
||||||
|
|
||||||
inpageProvider.publicConfigStore.subscribe(function (state) {
|
inpageProvider.publicConfigStore.subscribe(function (state) {
|
||||||
web3.eth.defaultAccount = state.selectedAddress
|
web3.eth.defaultAccount = state.selectedAddress
|
||||||
})
|
})
|
||||||
|
@ -1,30 +1,33 @@
|
|||||||
const once = require('once')
|
|
||||||
const ensnare = require('ensnare')
|
|
||||||
|
|
||||||
module.exports = setupDappAutoReload
|
module.exports = setupDappAutoReload
|
||||||
|
|
||||||
function setupDappAutoReload (web3) {
|
function setupDappAutoReload (web3, observable) {
|
||||||
// export web3 as a global, checking for usage
|
// export web3 as a global, checking for usage
|
||||||
var pageIsUsingWeb3 = false
|
global.web3 = new Proxy(web3, {
|
||||||
var resetWasRequested = false
|
get: (_web3, name) => {
|
||||||
global.web3 = ensnare(web3, once(function () {
|
// get the time of use
|
||||||
// if web3 usage happened after a reset request, trigger reset late
|
if (name !== '_used') _web3._used = Date.now()
|
||||||
if (resetWasRequested) return triggerReset()
|
return _web3[name]
|
||||||
// mark web3 as used
|
},
|
||||||
pageIsUsingWeb3 = true
|
set: (_web3, name, value) => {
|
||||||
// reset web3 reference
|
_web3[name] = value
|
||||||
global.web3 = web3
|
},
|
||||||
}))
|
})
|
||||||
|
var networkVersion
|
||||||
|
|
||||||
return handleResetRequest
|
observable.subscribe(function (state) {
|
||||||
|
// get the initial network
|
||||||
|
const curentNetVersion = state.networkVersion
|
||||||
|
if (!networkVersion) networkVersion = curentNetVersion
|
||||||
|
|
||||||
function handleResetRequest () {
|
if (curentNetVersion !== networkVersion && web3._used) {
|
||||||
resetWasRequested = true
|
const timeSinceUse = Date.now() - web3._used
|
||||||
// ignore if web3 was not used
|
// if web3 was recently used then delay the reloading of the page
|
||||||
if (!pageIsUsingWeb3) return
|
timeSinceUse > 500 ? triggerReset() : setTimeout(triggerReset, 500)
|
||||||
// reload after short timeout
|
// prevent reentry into if statement if state updates again before
|
||||||
setTimeout(triggerReset, 500)
|
// reload
|
||||||
}
|
networkVersion = curentNetVersion
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// reload the page
|
// reload the page
|
||||||
|
Loading…
Reference in New Issue
Block a user