aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/tx-state-history-helper.js
diff options
context:
space:
mode:
authorDan Finlay <flyswatter@users.noreply.github.com>2017-08-19 04:14:17 +0800
committerGitHub <noreply@github.com>2017-08-19 04:14:17 +0800
commite0c35179c23ffb3975906da76366a80fd0ae05d8 (patch)
treef352f3b671be7ac13496a8c223f4c4d8ec05a706 /app/scripts/lib/tx-state-history-helper.js
parent729233a2887c5394db324980711bf815e7abe11c (diff)
parent4e85ee5ccbf4b4b711c91f2076669b3574abd03e (diff)
downloadtangerine-wallet-browser-e0c35179c23ffb3975906da76366a80fd0ae05d8.tar.gz
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/tx-state-history-helper.js')
-rw-r--r--app/scripts/lib/tx-state-history-helper.js37
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