aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/tx-state-history-helper.js
diff options
context:
space:
mode:
authorKevin Serrano <kevin.serrano@consensys.net>2017-09-06 00:03:44 +0800
committerKevin Serrano <kevin.serrano@consensys.net>2017-09-06 00:03:44 +0800
commite647337a8a34d8fb4b5a8081888ec32ce5fd27a0 (patch)
tree2060fcf05407d4cec876332a70d25418237a724d /app/scripts/lib/tx-state-history-helper.js
parent45fc1d6ec356232e51fe4a9cc1f01929e35e8014 (diff)
parent2885a9547187ce1ad0fb3bed1935387d901942a8 (diff)
downloadtangerine-wallet-browser-e647337a8a34d8fb4b5a8081888ec32ce5fd27a0.tar.gz
tangerine-wallet-browser-e647337a8a34d8fb4b5a8081888ec32ce5fd27a0.tar.zst
tangerine-wallet-browser-e647337a8a34d8fb4b5a8081888ec32ce5fd27a0.zip
Resolve merge conflict from master
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