2020-01-09 04:34:58 +01:00
|
|
|
import jsonDiffer from 'fast-json-patch'
|
2020-01-29 18:14:33 +01:00
|
|
|
import { cloneDeep } from 'lodash'
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
|
|
|
converts non-initial history entries into diffs
|
2020-01-13 19:36:36 +01:00
|
|
|
@param {array} longHistory
|
2018-04-19 20:29:26 +02:00
|
|
|
@returns {array}
|
|
|
|
*/
|
2020-04-20 17:30:51 +02:00
|
|
|
export function migrateFromSnapshotsToDiffs (longHistory) {
|
2017-08-15 03:46:04 +02:00
|
|
|
return (
|
|
|
|
longHistory
|
|
|
|
// convert non-initial history entries into diffs
|
2019-07-31 22:17:11 +02:00
|
|
|
.map((entry, index) => {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (index === 0) {
|
|
|
|
return entry
|
|
|
|
}
|
2019-07-31 22:17:11 +02:00
|
|
|
return generateHistoryEntry(longHistory[index - 1], entry)
|
|
|
|
})
|
2017-08-15 03:46:04 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2018-05-10 13:26:02 +02:00
|
|
|
Generates an array of history objects sense the previous state.
|
2018-05-10 13:43:31 +02:00
|
|
|
The object has the keys
|
2018-05-10 13:26:02 +02:00
|
|
|
op (the operation performed),
|
|
|
|
path (the key and if a nested object then each key will be seperated with a `/`)
|
|
|
|
value
|
|
|
|
with the first entry having the note and a timestamp when the change took place
|
2020-01-13 19:36:36 +01:00
|
|
|
@param {Object} previousState - the previous state of the object
|
|
|
|
@param {Object} newState - the update object
|
2019-12-17 00:24:52 +01:00
|
|
|
@param {string} [note] - a optional note for the state change
|
2018-05-10 13:26:02 +02:00
|
|
|
@returns {array}
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
2020-04-20 17:30:51 +02:00
|
|
|
export function generateHistoryEntry (previousState, newState, note) {
|
2017-10-02 22:44:11 +02:00
|
|
|
const entry = jsonDiffer.compare(previousState, newState)
|
|
|
|
// Add a note to the first op, since it breaks if we append it to the entry
|
2018-05-10 13:26:02 +02:00
|
|
|
if (entry[0]) {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (note) {
|
|
|
|
entry[0].note = note
|
|
|
|
}
|
2018-05-10 13:26:02 +02:00
|
|
|
|
2018-05-16 22:05:07 +02:00
|
|
|
entry[0].timestamp = Date.now()
|
2018-05-10 13:26:02 +02:00
|
|
|
}
|
2017-10-02 22:44:11 +02:00
|
|
|
return entry
|
2017-08-15 03:46:04 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
|
|
|
Recovers previous txMeta state obj
|
2020-01-13 19:36:36 +01:00
|
|
|
@returns {Object}
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
2020-04-20 17:30:51 +02:00
|
|
|
export function replayHistory (_shortHistory) {
|
2020-01-29 18:14:33 +01:00
|
|
|
const shortHistory = cloneDeep(_shortHistory)
|
2017-08-15 03:46:04 +02:00
|
|
|
return shortHistory.reduce((val, entry) => jsonDiffer.applyPatch(val, entry).newDocument)
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-04-20 16:56:01 +02:00
|
|
|
* Snapshot {@code txMeta}
|
|
|
|
* @param {Object} txMeta - the tx metadata object
|
|
|
|
* @returns {Object} a deep clone without history
|
|
|
|
*/
|
2020-04-20 17:30:51 +02:00
|
|
|
export function snapshotFromTxMeta (txMeta) {
|
2020-04-20 16:56:01 +02:00
|
|
|
const shallow = { ...txMeta }
|
|
|
|
delete shallow.history
|
|
|
|
return cloneDeep(shallow)
|
2017-10-21 21:06:39 +02:00
|
|
|
}
|