aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/auto-reload.js
blob: 3c90905db35ed64960398bd9235351cc1d0f4a7a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const once = require('once')
const ensnare = require('ensnare')

module.exports = setupDappAutoReload

function setupDappAutoReload (web3) {
  // 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
  }))

  return handleResetRequest

  function handleResetRequest() {
    resetWasRequested = true
    // ignore if web3 was not used
    if (!pageIsUsingWeb3) return
    // reload after short timeout
    setTimeout(triggerReset, 500)
  }

}

// reload the page
function triggerReset () {
  global.location.reload()
}