1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 02:58:09 +01:00
metamask-extension/app/scripts/lib/auto-reload.js

35 lines
919 B
JavaScript
Raw Normal View History

2016-05-23 00:23:16 +02:00
const once = require('once')
2016-08-12 22:25:38 +02:00
const ensnare = require('ensnare')
2016-05-23 00:23:16 +02:00
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
}