aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/auto-reload.js
diff options
context:
space:
mode:
authorkumavis <kumavis@users.noreply.github.com>2017-06-13 04:27:04 +0800
committerGitHub <noreply@github.com>2017-06-13 04:27:04 +0800
commit27220b7bcd5ea1ebdae3bc1b494d28d9c828918c (patch)
treec9385fba3d742ca9cbfb9f3b4c6c9ef29c4f4a1b /app/scripts/lib/auto-reload.js
parent57d1a1f1860e50837104a10b7b9f86d398c795ec (diff)
parent8af41f1b0539e70cf4c6e1f4a9f4b10ad13656fc (diff)
downloadtangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.tar
tangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.tar.gz
tangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.tar.bz2
tangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.tar.lz
tangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.tar.xz
tangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.tar.zst
tangerine-wallet-browser-27220b7bcd5ea1ebdae3bc1b494d28d9c828918c.zip
Merge branch 'master' into i#1203MainNetSwitch
Diffstat (limited to 'app/scripts/lib/auto-reload.js')
-rw-r--r--app/scripts/lib/auto-reload.js47
1 files changed, 25 insertions, 22 deletions
diff --git a/app/scripts/lib/auto-reload.js b/app/scripts/lib/auto-reload.js
index 1302df35f..534047330 100644
--- a/app/scripts/lib/auto-reload.js
+++ b/app/scripts/lib/auto-reload.js
@@ -1,30 +1,33 @@
-const once = require('once')
-const ensnare = require('ensnare')
-
module.exports = setupDappAutoReload
-function setupDappAutoReload (web3) {
+function setupDappAutoReload (web3, observable) {
// 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
- }))
+ global.web3 = new Proxy(web3, {
+ get: (_web3, name) => {
+ // get the time of use
+ if (name !== '_used') _web3._used = Date.now()
+ return _web3[name]
+ },
+ set: (_web3, name, value) => {
+ _web3[name] = value
+ },
+ })
+ var networkVersion
- return handleResetRequest
+ observable.subscribe(function (state) {
+ // get the initial network
+ const curentNetVersion = state.networkVersion
+ if (!networkVersion) networkVersion = curentNetVersion
- function handleResetRequest () {
- resetWasRequested = true
- // ignore if web3 was not used
- if (!pageIsUsingWeb3) return
- // reload after short timeout
- setTimeout(triggerReset, 500)
- }
+ if (curentNetVersion !== networkVersion && web3._used) {
+ const timeSinceUse = Date.now() - web3._used
+ // if web3 was recently used then delay the reloading of the page
+ timeSinceUse > 500 ? triggerReset() : setTimeout(triggerReset, 500)
+ // prevent reentry into if statement if state updates again before
+ // reload
+ networkVersion = curentNetVersion
+ }
+ })
}
// reload the page