aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2018-06-14 07:45:18 +0800
committerkumavis <aaron@kumavis.me>2018-06-14 07:45:18 +0800
commit7b414f3ed08b8eb35ce7a8e076e4ffd75fea3d30 (patch)
tree3d037349648c3b785aa08ffe45535e0e8aa5349c
parent451d05a400302d443e13d4d75a7094bf20e86d97 (diff)
downloadtangerine-wallet-browser-7b414f3ed08b8eb35ce7a8e076e4ffd75fea3d30.tar
tangerine-wallet-browser-7b414f3ed08b8eb35ce7a8e076e4ffd75fea3d30.tar.gz
tangerine-wallet-browser-7b414f3ed08b8eb35ce7a8e076e4ffd75fea3d30.tar.bz2
tangerine-wallet-browser-7b414f3ed08b8eb35ce7a8e076e4ffd75fea3d30.tar.lz
tangerine-wallet-browser-7b414f3ed08b8eb35ce7a8e076e4ffd75fea3d30.tar.xz
tangerine-wallet-browser-7b414f3ed08b8eb35ce7a8e076e4ffd75fea3d30.tar.zst
tangerine-wallet-browser-7b414f3ed08b8eb35ce7a8e076e4ffd75fea3d30.zip
background - persistence pipeline - fix persistence bug
-rw-r--r--app/scripts/background.js14
-rw-r--r--app/scripts/lib/createStreamSink.js24
2 files changed, 32 insertions, 6 deletions
diff --git a/app/scripts/background.js b/app/scripts/background.js
index 56e190f97..2451cddb6 100644
--- a/app/scripts/background.js
+++ b/app/scripts/background.js
@@ -16,6 +16,7 @@ const ExtensionPlatform = require('./platforms/extension')
const Migrator = require('./lib/migrator/')
const migrations = require('./migrations/')
const PortStream = require('./lib/port-stream.js')
+const createStreamSink = require('./lib/createStreamSink')
const NotificationManager = require('./lib/notification-manager.js')
const MetamaskController = require('./metamask-controller')
const firstTimeState = require('./first-time-state')
@@ -273,7 +274,7 @@ function setupController (initState, initLangCode) {
asStream(controller.store),
debounce(1000),
storeTransform(versionifyData),
- storeTransform(persistData),
+ createStreamSink(persistData),
(error) => {
log.error('MetaMask - Persistence pipeline failed', error)
}
@@ -289,7 +290,7 @@ function setupController (initState, initLangCode) {
return versionedData
}
- function persistData (state) {
+ async function persistData (state) {
if (!state) {
throw new Error('MetaMask - updated state is missing', state)
}
@@ -297,12 +298,13 @@ function setupController (initState, initLangCode) {
throw new Error('MetaMask - updated state does not have data', state)
}
if (localStore.isSupported) {
- localStore.set(state)
- .catch((err) => {
+ try {
+ await localStore.set(state)
+ } catch (err) {
+ // log error so we dont break the pipeline
log.error('error setting state in local store:', err)
- })
+ }
}
- return state
}
//
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)
+ }
+
+}