1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 21:00:13 +01:00
metamask-extension/app/scripts/lib/auto-reload.js

35 lines
924 B
JavaScript
Raw Normal View History

2016-05-23 00:23:16 +02:00
const once = require('once')
const ensnare = require('./ensnare.js')
module.exports = setupDappAutoReload
2016-06-21 22:18:32 +02:00
function setupDappAutoReload (web3, controlStream) {
2016-05-23 00:23:16 +02:00
// export web3 as a global, checking for usage
var pageIsUsingWeb3 = false
var resetWasRequested = false
2016-06-21 22:18:32 +02:00
global.web3 = ensnare(web3, once(function () {
2016-05-23 00:23:16 +02:00
// 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
}))
// listen for reset requests from metamask
2016-06-21 22:18:32 +02:00
controlStream.once('data', function () {
2016-05-23 00:23:16 +02:00
resetWasRequested = true
// ignore if web3 was not used
if (!pageIsUsingWeb3) return
// reload after short timeout
triggerReset()
})
// reload the page
2016-06-21 22:18:32 +02:00
function triggerReset () {
setTimeout(function () {
2016-05-23 00:23:16 +02:00
global.location.reload()
}, 500)
}
2016-06-21 22:18:32 +02:00
}