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
|
|
|
/**
|
|
|
|
converts non-initial history entries into diffs
|
2020-11-10 18:30:41 +01:00
|
|
|
@param {Array} longHistory
|
|
|
|
@returns {Array}
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
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
|
|
|
/**
|
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),
|
2020-07-20 19:02:49 +02:00
|
|
|
path (the key and if a nested object then each key will be separated with a `/`)
|
2018-05-10 13:26:02 +02:00
|
|
|
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
|
2020-11-10 18:30:41 +01:00
|
|
|
@returns {Array}
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
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
|
|
|
}
|
2018-05-10 13:26:02 +02: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
|
|
|
/**
|
|
|
|
Recovers previous txMeta state obj
|
2020-01-13 19:36:36 +01:00
|
|
|
@returns {Object}
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
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}
|
|
|
|
* @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
|
|
|
}
|