diff options
author | frankiebee <frankie.diamond@gmail.com> | 2017-08-22 02:35:22 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2017-08-22 02:35:22 +0800 |
commit | fbba3a1ac8575b910e8a2a684748d617eec19414 (patch) | |
tree | c2940c33345d128a2888fc4441b8342e6b778d65 /app/scripts/lib | |
parent | 7ea83b6bae34dcf652d85474fe1d82893d592d55 (diff) | |
parent | 9203b4edf9df8b616877c57970fb01a7fb87924b (diff) | |
download | tangerine-wallet-browser-fbba3a1ac8575b910e8a2a684748d617eec19414.tar tangerine-wallet-browser-fbba3a1ac8575b910e8a2a684748d617eec19414.tar.gz tangerine-wallet-browser-fbba3a1ac8575b910e8a2a684748d617eec19414.tar.bz2 tangerine-wallet-browser-fbba3a1ac8575b910e8a2a684748d617eec19414.tar.lz tangerine-wallet-browser-fbba3a1ac8575b910e8a2a684748d617eec19414.tar.xz tangerine-wallet-browser-fbba3a1ac8575b910e8a2a684748d617eec19414.tar.zst tangerine-wallet-browser-fbba3a1ac8575b910e8a2a684748d617eec19414.zip |
Merge branch 'master' into transactionControllerRefractorPt3
Diffstat (limited to 'app/scripts/lib')
-rw-r--r-- | app/scripts/lib/auto-reload.js | 5 | ||||
-rw-r--r-- | app/scripts/lib/tx-state-history-helper.js | 37 | ||||
-rw-r--r-- | app/scripts/lib/tx-state-manager.js | 26 |
3 files changed, 56 insertions, 12 deletions
diff --git a/app/scripts/lib/auto-reload.js b/app/scripts/lib/auto-reload.js index 534047330..6abce73ea 100644 --- a/app/scripts/lib/auto-reload.js +++ b/app/scripts/lib/auto-reload.js @@ -5,7 +5,10 @@ function setupDappAutoReload (web3, observable) { global.web3 = new Proxy(web3, { get: (_web3, name) => { // get the time of use - if (name !== '_used') _web3._used = Date.now() + if (name !== '_used') { + console.warn('MetaMask: web3 will be deprecated in the near future in favor of the ethereumProvider \nhttps://github.com/ethereum/mist/releases/tag/v0.9.0') + _web3._used = Date.now() + } return _web3[name] }, set: (_web3, name, value) => { diff --git a/app/scripts/lib/tx-state-history-helper.js b/app/scripts/lib/tx-state-history-helper.js new file mode 100644 index 000000000..304069d57 --- /dev/null +++ b/app/scripts/lib/tx-state-history-helper.js @@ -0,0 +1,37 @@ +const jsonDiffer = require('fast-json-patch') +const clone = require('clone') + +module.exports = { + generateHistoryEntry, + replayHistory, + snapshotFromTxMeta, + migrateFromSnapshotsToDiffs, +} + + +function migrateFromSnapshotsToDiffs(longHistory) { + return ( + longHistory + // convert non-initial history entries into diffs + .map((entry, index) => { + if (index === 0) return entry + return generateHistoryEntry(longHistory[index - 1], entry) + }) + ) +} + +function generateHistoryEntry(previousState, newState) { + return jsonDiffer.compare(previousState, newState) +} + +function replayHistory(shortHistory) { + return shortHistory.reduce((val, entry) => jsonDiffer.applyPatch(val, entry).newDocument) +} + +function snapshotFromTxMeta(txMeta) { + // create txMeta snapshot for history + const snapshot = clone(txMeta) + // dont include previous history in this snapshot + delete snapshot.history + return snapshot +}
\ No newline at end of file diff --git a/app/scripts/lib/tx-state-manager.js b/app/scripts/lib/tx-state-manager.js index 378ea38ab..d3314c286 100644 --- a/app/scripts/lib/tx-state-manager.js +++ b/app/scripts/lib/tx-state-manager.js @@ -1,6 +1,6 @@ -const clone = require('clone') const extend = require('xtend') const ObservableStore = require('obs-store') +const txStateHistoryHelper = require('./tx-state-history-helper') module.exports = class TransactionStateManger extends ObservableStore { constructor ({initState, txHistoryLimit, getNetwork}) { @@ -41,6 +41,11 @@ module.exports = class TransactionStateManger extends ObservableStore { this.once(`${txMeta.id}:rejected`, function (txId) { this.removeAllListeners(`${txMeta.id}:signed`) }) + // initialize history + txMeta.history = [] + // capture initial snapshot of txMeta for history + const snapshot = txStateHistoryHelper.snapshotFromTxMeta(txMeta) + txMeta.history.push(snapshot) const transactions = this.getFullTxList() const txCount = this.getTxCount() @@ -57,6 +62,7 @@ module.exports = class TransactionStateManger extends ObservableStore { } transactions.push(txMeta) this._saveTxList(transactions) + return txMeta } // gets tx by Id and returns it getTx (txId) { @@ -66,19 +72,17 @@ module.exports = class TransactionStateManger extends ObservableStore { updateTx (txMeta) { // create txMeta snapshot for history - const txMetaForHistory = clone(txMeta) - // dont include previous history in this snapshot - delete txMetaForHistory.history - // add snapshot to tx history - if (!txMeta.history) txMeta.history = [] - txMeta.history.push(txMetaForHistory) - + const currentState = txStateHistoryHelper.snapshotFromTxMeta(txMeta) + // recover previous tx state obj + const previousState = txStateHistoryHelper.replayHistory(txMeta.history) + // generate history entry and add to history + const entry = txStateHistoryHelper.generateHistoryEntry(previousState, currentState) + txMeta.history.push(entry) + + // commit txMeta to state const txId = txMeta.id const txList = this.getFullTxList() const index = txList.findIndex(txData => txData.id === txId) - if (!txMeta.history) txMeta.history = [] - txMeta.history.push(txMetaForHistory) - txList[index] = txMeta this._saveTxList(txList) } |