diff options
author | Dan Finlay <flyswatter@users.noreply.github.com> | 2017-08-19 04:14:17 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-19 04:14:17 +0800 |
commit | e0c35179c23ffb3975906da76366a80fd0ae05d8 (patch) | |
tree | f352f3b671be7ac13496a8c223f4c4d8ec05a706 /app/scripts/lib | |
parent | 729233a2887c5394db324980711bf815e7abe11c (diff) | |
parent | 4e85ee5ccbf4b4b711c91f2076669b3574abd03e (diff) | |
download | tangerine-wallet-browser-e0c35179c23ffb3975906da76366a80fd0ae05d8.tar tangerine-wallet-browser-e0c35179c23ffb3975906da76366a80fd0ae05d8.tar.gz tangerine-wallet-browser-e0c35179c23ffb3975906da76366a80fd0ae05d8.tar.bz2 tangerine-wallet-browser-e0c35179c23ffb3975906da76366a80fd0ae05d8.tar.lz tangerine-wallet-browser-e0c35179c23ffb3975906da76366a80fd0ae05d8.tar.xz tangerine-wallet-browser-e0c35179c23ffb3975906da76366a80fd0ae05d8.tar.zst tangerine-wallet-browser-e0c35179c23ffb3975906da76366a80fd0ae05d8.zip |
Merge pull request #1914 from MetaMask/history-diff
Move Tx State History to diff-based format
Diffstat (limited to 'app/scripts/lib')
-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 |