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

34 lines
843 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
function setupDappAutoReload (web3) {
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
}))
return handleResetRequest
2016-11-11 19:26:12 +01:00
function handleResetRequest () {
2016-05-23 00:23:16 +02:00
resetWasRequested = true
// ignore if web3 was not used
if (!pageIsUsingWeb3) return
// reload after short timeout
setTimeout(triggerReset, 500)
2016-05-23 00:23:16 +02:00
}
2016-06-21 22:18:32 +02:00
}
// reload the page
function triggerReset () {
global.location.reload()
2016-11-11 19:26:12 +01:00
}