diff options
background - persistence pipeline - fix persistence bug
Diffstat (limited to 'app')
-rw-r--r-- | app/scripts/background.js | 14 | ||||
-rw-r--r-- | app/scripts/lib/createStreamSink.js | 24 |
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) + } + +} |