Deduplicate code

pull/1/head
yflory 4 years ago
parent 1b1487c2e4
commit 89c19b136b

@ -224,13 +224,16 @@ define([
var now = function () { return +new Date(); }; var now = function () { return +new Date(); };
var sortCpIndex = function (hashes) {
return Object.keys(hashes).map(Number).sort(function (a, b) {
return a-b;
});
};
var getLastCp = function (old, i) { var getLastCp = function (old, i) {
var hashes = old ? oldHashes : content.hashes; var hashes = old ? oldHashes : content.hashes;
if (!hashes || !Object.keys(hashes).length) { return {}; } if (!hashes || !Object.keys(hashes).length) { return {}; }
i = i || 0; i = i || 0;
var idx = Object.keys(hashes).map(Number).sort(function (a, b) { var idx = sortCpIndex(hashes);
return a-b;
});
var lastIndex = idx[idx.length - 1 - i]; var lastIndex = idx[idx.length - 1 - i];
var last = JSON.parse(JSON.stringify(hashes[lastIndex])); var last = JSON.parse(JSON.stringify(hashes[lastIndex]));
return last; return last;
@ -308,9 +311,7 @@ define([
return void UI.alert(Messages.oo_saveError); return void UI.alert(Messages.oo_saveError);
} }
// Get the last cp idx // Get the last cp idx
var all = Object.keys(content.hashes || {}).map(Number).sort(function (a, b) { var all = sortCpIndex(content.hashes || {});
return a-b;
});
var current = all[all.length - 1] || 0; var current = all[all.length - 1] || 0;
// Get the expected cp idx // Get the expected cp idx
var _i = Math.floor(ev.index / CHECKPOINT_INTERVAL); var _i = Math.floor(ev.index / CHECKPOINT_INTERVAL);

Loading…
Cancel
Save