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