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/tx-state-history-helper.js | |
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/tx-state-history-helper.js')
-rw-r--r-- | app/scripts/lib/tx-state-history-helper.js | 37 |
1 files changed, 37 insertions, 0 deletions
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 |