diff options
author | kumavis <aaron@kumavis.me> | 2017-08-15 09:46:04 +0800 |
---|---|---|
committer | kumavis <aaron@kumavis.me> | 2017-08-15 09:46:04 +0800 |
commit | fdffb6fedc6a29a2f952f7db669bdc2907afb854 (patch) | |
tree | 35a996001d2e32950c1e7bd0ee8fb37d0b345bd8 /app/scripts/lib | |
parent | 68c6b2d666719476bff78cbc107a56be3b86dbe1 (diff) | |
download | tangerine-wallet-browser-fdffb6fedc6a29a2f952f7db669bdc2907afb854.tar.gz 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')
-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..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 |