aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib
diff options
context:
space:
mode:
Diffstat (limited to 'app/scripts/lib')
-rw-r--r--app/scripts/lib/auto-reload.js61
-rw-r--r--app/scripts/lib/createStreamSink.js24
-rw-r--r--app/scripts/lib/get-first-preferred-lang-code.js20
-rw-r--r--app/scripts/lib/notification-manager.js6
4 files changed, 106 insertions, 5 deletions
diff --git a/app/scripts/lib/auto-reload.js b/app/scripts/lib/auto-reload.js
new file mode 100644
index 000000000..cce31c3d2
--- /dev/null
+++ b/app/scripts/lib/auto-reload.js
@@ -0,0 +1,61 @@
+module.exports = setupDappAutoReload
+
+function setupDappAutoReload (web3, observable) {
+ // export web3 as a global, checking for usage
+ let hasBeenWarned = false
+ let reloadInProgress = false
+ let lastTimeUsed
+ let lastSeenNetwork
+
+ global.web3 = new Proxy(web3, {
+ get: (_web3, key) => {
+ // show warning once on web3 access
+ if (!hasBeenWarned && key !== 'currentProvider') {
+ console.warn('MetaMask: web3 will be deprecated in the near future in favor of the ethereumProvider \nhttps://github.com/MetaMask/faq/blob/master/detecting_metamask.md#web3-deprecation')
+ hasBeenWarned = true
+ }
+ // get the time of use
+ lastTimeUsed = Date.now()
+ // return value normally
+ return _web3[key]
+ },
+ set: (_web3, key, value) => {
+ // set value normally
+ _web3[key] = value
+ },
+ })
+
+ observable.subscribe(function (state) {
+ // if reload in progress, no need to check reload logic
+ if (reloadInProgress) return
+
+ const currentNetwork = state.networkVersion
+
+ // set the initial network
+ if (!lastSeenNetwork) {
+ lastSeenNetwork = currentNetwork
+ return
+ }
+
+ // skip reload logic if web3 not used
+ if (!lastTimeUsed) return
+
+ // if network did not change, exit
+ if (currentNetwork === lastSeenNetwork) return
+
+ // initiate page reload
+ reloadInProgress = true
+ const timeSinceUse = Date.now() - lastTimeUsed
+ // if web3 was recently used then delay the reloading of the page
+ if (timeSinceUse > 500) {
+ triggerReset()
+ } else {
+ setTimeout(triggerReset, 500)
+ }
+ })
+}
+
+// reload the page
+function triggerReset () {
+ global.location.reload()
+}
diff --git a/app/scripts/lib/createStreamSink.js b/app/scripts/lib/createStreamSink.js
new file mode 100644
index 000000000..cf9416fea
--- /dev/null
+++ b/app/scripts/lib/createStreamSink.js
@@ -0,0 +1,24 @@
+const WritableStream = require('readable-stream').Writable
+const promiseToCallback = require('promise-to-callback')
+
+module.exports = createStreamSink
+
+
+function createStreamSink(asyncWriteFn, _opts) {
+ return new AsyncWritableStream(asyncWriteFn, _opts)
+}
+
+class AsyncWritableStream extends WritableStream {
+
+ constructor (asyncWriteFn, _opts) {
+ const opts = Object.assign({ objectMode: true }, _opts)
+ super(opts)
+ this._asyncWriteFn = asyncWriteFn
+ }
+
+ // write from incomming stream to state
+ _write (chunk, encoding, callback) {
+ promiseToCallback(this._asyncWriteFn(chunk, encoding))(callback)
+ }
+
+}
diff --git a/app/scripts/lib/get-first-preferred-lang-code.js b/app/scripts/lib/get-first-preferred-lang-code.js
index 1e6a83ba6..41a886d74 100644
--- a/app/scripts/lib/get-first-preferred-lang-code.js
+++ b/app/scripts/lib/get-first-preferred-lang-code.js
@@ -2,8 +2,7 @@ const extension = require('extensionizer')
const promisify = require('pify')
const allLocales = require('../../_locales/index.json')
-const isSupported = extension.i18n && extension.i18n.getAcceptLanguages
-const getPreferredLocales = isSupported ? promisify(
+const getPreferredLocales = extension.i18n ? promisify(
extension.i18n.getAcceptLanguages,
{ errorFirst: false }
) : async () => []
@@ -18,7 +17,21 @@ const existingLocaleCodes = allLocales.map(locale => locale.code.toLowerCase().r
*
*/
async function getFirstPreferredLangCode () {
- const userPreferredLocaleCodes = await getPreferredLocales()
+ let userPreferredLocaleCodes
+
+ try {
+ userPreferredLocaleCodes = await getPreferredLocales()
+ } catch (e) {
+ // Brave currently throws when calling getAcceptLanguages, so this handles that.
+ userPreferredLocaleCodes = []
+ }
+
+ // safeguard for Brave Browser until they implement chrome.i18n.getAcceptLanguages
+ // https://github.com/MetaMask/metamask-extension/issues/4270
+ if (!userPreferredLocaleCodes){
+ userPreferredLocaleCodes = []
+ }
+
const firstPreferredLangCode = userPreferredLocaleCodes
.map(code => code.toLowerCase())
.find(code => existingLocaleCodes.includes(code))
@@ -26,3 +39,4 @@ async function getFirstPreferredLangCode () {
}
module.exports = getFirstPreferredLangCode
+
diff --git a/app/scripts/lib/notification-manager.js b/app/scripts/lib/notification-manager.js
index 5dfb42078..6b88a7a99 100644
--- a/app/scripts/lib/notification-manager.js
+++ b/app/scripts/lib/notification-manager.js
@@ -32,6 +32,8 @@ class NotificationManager {
type: 'popup',
width,
height,
+ }).then((currentPopup) => {
+ this._popupId = currentPopup.id
})
}
})
@@ -84,7 +86,7 @@ class NotificationManager {
}
/**
- * Given an array of windows, returns the first that has a 'popup' type, or null if no such window exists.
+ * Given an array of windows, returns the 'popup' that has been opened by MetaMask, or null if no such window exists.
*
* @private
* @param {array} windows An array of objects containing data about the open MetaMask extension windows.
@@ -93,7 +95,7 @@ class NotificationManager {
_getPopupIn (windows) {
return windows ? windows.find((win) => {
// Returns notification popup
- return (win && win.type === 'popup')
+ return (win && win.type === 'popup' && win.id === this._popupId)
}) : null
}