2011-12-04 16:33:56 +01:00
|
|
|
/**
|
2019-04-16 00:34:29 +02:00
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
2011-12-04 16:33:56 +01:00
|
|
|
* This helps other people to understand this code better and helps them to improve it.
|
|
|
|
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
|
|
|
|
*/
|
|
|
|
|
2011-06-20 12:44:04 +02:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-06-20 12:44:04 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-06-20 12:44:04 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-06-20 12:44:04 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
// revision info is a skip list whos entries represent a particular revision
|
|
|
|
// of the document. These revisions are connected together by various
|
|
|
|
// changesets, or deltas, between any two revisions.
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function loadBroadcastRevisionsJS() {
|
|
|
|
function Revision(revNum) {
|
2011-06-20 12:44:04 +02:00
|
|
|
this.rev = revNum;
|
|
|
|
this.changesets = [];
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
Revision.prototype.addChangeset = function (destIndex, changeset, timeDelta) {
|
|
|
|
const changesetWrapper = {
|
2011-06-20 12:44:04 +02:00
|
|
|
deltaRev: destIndex - this.rev,
|
|
|
|
deltaTime: timeDelta,
|
2020-11-23 19:24:19 +01:00
|
|
|
getValue() {
|
2011-06-20 12:44:04 +02:00
|
|
|
return changeset;
|
2020-11-23 19:24:19 +01:00
|
|
|
},
|
2011-06-20 12:44:04 +02:00
|
|
|
};
|
|
|
|
this.changesets.push(changesetWrapper);
|
2020-11-23 19:24:19 +01:00
|
|
|
this.changesets.sort((a, b) => (b.deltaRev - a.deltaRev));
|
|
|
|
};
|
2011-06-20 12:44:04 +02:00
|
|
|
|
|
|
|
revisionInfo = {};
|
2020-11-23 19:24:19 +01:00
|
|
|
revisionInfo.addChangeset = function (fromIndex, toIndex, changeset, backChangeset, timeDelta) {
|
|
|
|
const startRevision = revisionInfo[fromIndex] || revisionInfo.createNew(fromIndex);
|
|
|
|
const endRevision = revisionInfo[toIndex] || revisionInfo.createNew(toIndex);
|
2011-06-20 12:44:04 +02:00
|
|
|
startRevision.addChangeset(toIndex, changeset, timeDelta);
|
|
|
|
endRevision.addChangeset(fromIndex, backChangeset, -1 * timeDelta);
|
2020-11-23 19:24:19 +01:00
|
|
|
};
|
2011-06-20 12:44:04 +02:00
|
|
|
|
2012-04-23 12:52:30 +02:00
|
|
|
revisionInfo.latest = clientVars.collab_client_vars.rev || -1;
|
2011-06-20 12:44:04 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
revisionInfo.createNew = function (index) {
|
2011-06-20 12:44:04 +02:00
|
|
|
revisionInfo[index] = new Revision(index);
|
2020-11-23 19:24:19 +01:00
|
|
|
if (index > revisionInfo.latest) {
|
2011-06-20 12:44:04 +02:00
|
|
|
revisionInfo.latest = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return revisionInfo[index];
|
2020-11-23 19:24:19 +01:00
|
|
|
};
|
2011-06-20 12:44:04 +02:00
|
|
|
|
|
|
|
// assuming that there is a path from fromIndex to toIndex, and that the links
|
|
|
|
// are laid out in a skip-list format
|
2020-11-23 19:24:19 +01:00
|
|
|
revisionInfo.getPath = function (fromIndex, toIndex) {
|
|
|
|
const changesets = [];
|
|
|
|
const spans = [];
|
|
|
|
const times = [];
|
|
|
|
let elem = revisionInfo[fromIndex] || revisionInfo.createNew(fromIndex);
|
|
|
|
if (elem.changesets.length != 0 && fromIndex != toIndex) {
|
|
|
|
const reverse = !(fromIndex < toIndex);
|
|
|
|
while (((elem.rev < toIndex) && !reverse) || ((elem.rev > toIndex) && reverse)) {
|
|
|
|
let couldNotContinue = false;
|
|
|
|
const oldRev = elem.rev;
|
2011-06-20 12:44:04 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
for (let i = reverse ? elem.changesets.length - 1 : 0;
|
|
|
|
reverse ? i >= 0 : i < elem.changesets.length;
|
|
|
|
i += reverse ? -1 : 1) {
|
|
|
|
if (((elem.changesets[i].deltaRev < 0) && !reverse) || ((elem.changesets[i].deltaRev > 0) && reverse)) {
|
2011-06-20 12:44:04 +02:00
|
|
|
couldNotContinue = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (((elem.rev + elem.changesets[i].deltaRev <= toIndex) && !reverse) || ((elem.rev + elem.changesets[i].deltaRev >= toIndex) && reverse)) {
|
|
|
|
const topush = elem.changesets[i];
|
2011-06-20 12:44:04 +02:00
|
|
|
changesets.push(topush.getValue());
|
|
|
|
spans.push(elem.changesets[i].deltaRev);
|
|
|
|
times.push(topush.deltaTime);
|
|
|
|
elem = revisionInfo[elem.rev + elem.changesets[i].deltaRev];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (couldNotContinue || oldRev == elem.rev) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let status = 'partial';
|
2011-06-20 12:44:04 +02:00
|
|
|
if (elem.rev == toIndex) status = 'complete';
|
|
|
|
|
|
|
|
return {
|
2020-11-23 19:24:19 +01:00
|
|
|
fromRev: fromIndex,
|
|
|
|
rev: elem.rev,
|
|
|
|
status,
|
|
|
|
changesets,
|
|
|
|
spans,
|
|
|
|
times,
|
2011-06-20 12:44:04 +02:00
|
|
|
};
|
2020-11-23 19:24:19 +01:00
|
|
|
};
|
2011-06-20 12:44:04 +02:00
|
|
|
}
|
2012-01-16 02:23:48 +01:00
|
|
|
|
|
|
|
exports.loadBroadcastRevisionsJS = loadBroadcastRevisionsJS;
|