aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/tx-state-history-helper.js
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-08-15 09:46:04 +0800
committerkumavis <aaron@kumavis.me>2017-08-15 09:46:04 +0800
commitfdffb6fedc6a29a2f952f7db669bdc2907afb854 (patch)
tree35a996001d2e32950c1e7bd0ee8fb37d0b345bd8 /app/scripts/lib/tx-state-history-helper.js
parent68c6b2d666719476bff78cbc107a56be3b86dbe1 (diff)
downloadtangerine-wallet-browser-fdffb6fedc6a29a2f952f7db669bdc2907afb854.tar
tangerine-wallet-browser-fdffb6fedc6a29a2f952f7db669bdc2907afb854.tar.gz
tangerine-wallet-browser-fdffb6fedc6a29a2f952f7db669bdc2907afb854.tar.bz2
tangerine-wallet-browser-fdffb6fedc6a29a2f952f7db669bdc2907afb854.tar.lz
tangerine-wallet-browser-fdffb6fedc6a29a2f952f7db669bdc2907afb854.tar.xz
tangerine-wallet-browser-fdffb6fedc6a29a2f952f7db669bdc2907afb854.tar.zst
tangerine-wallet-browser-fdffb6fedc6a29a2f952f7db669bdc2907afb854.zip
introduce tx-state-history-helper and diff-based history
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..87b9a1d63
--- /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