padDiff.js: convert to Promises/async

pull/3559/head
Ray Bellis 2019-01-31 14:38:56 +00:00
parent 4622309dc2
commit ccb49dcdc1
1 changed files with 93 additions and 175 deletions

View File

@ -1,7 +1,5 @@
var Changeset = require("../../static/js/Changeset"); var Changeset = require("../../static/js/Changeset");
var async = require("async");
var exportHtml = require('./ExportHtml'); var exportHtml = require('./ExportHtml');
const thenify = require("thenify").withCallback;
function PadDiff (pad, fromRev, toRev) { function PadDiff (pad, fromRev, toRev) {
// check parameters // check parameters
@ -79,79 +77,52 @@ PadDiff.prototype._isClearAuthorship = function(changeset) {
return true; return true;
}; };
PadDiff.prototype._createClearAuthorship = thenify(function(rev, callback) { PadDiff.prototype._createClearAuthorship = async function(rev) {
var self = this;
this._pad.getInternalRevisionAText(rev, function(err, atext) { let atext = await this._pad.getInternalRevisionAText(rev);
if (err) {
return callback(err);
}
// build clearAuthorship changeset // build clearAuthorship changeset
var builder = Changeset.builder(atext.text.length); var builder = Changeset.builder(atext.text.length);
builder.keepText(atext.text, [['author','']], self._pad.pool); builder.keepText(atext.text, [['author','']], this._pad.pool);
var changeset = builder.toString(); var changeset = builder.toString();
callback(null, changeset); return changeset;
}); }
});
PadDiff.prototype._createClearStartAtext = thenify(function(rev, callback) { PadDiff.prototype._createClearStartAtext = async function(rev) {
var self = this;
// get the atext of this revision // get the atext of this revision
this._pad.getInternalRevisionAText(rev, function(err, atext) { let atext = this._pad.getInternalRevisionAText(rev);
if (err) {
return callback(err);
}
// create the clearAuthorship changeset // create the clearAuthorship changeset
self._createClearAuthorship(rev, function(err, changeset) { let changeset = await this._createClearAuthorship(rev);
if (err) {
return callback(err);
}
try {
// apply the clearAuthorship changeset // apply the clearAuthorship changeset
var newAText = Changeset.applyToAText(changeset, atext, self._pad.pool); let newAText = Changeset.applyToAText(changeset, atext, this._pad.pool);
} catch(err) {
return callback(err)
}
callback(null, newAText); return newAText;
}); }
});
});
PadDiff.prototype._getChangesetsInBulk = thenify(function(startRev, count, callback) { PadDiff.prototype._getChangesetsInBulk = async function(startRev, count) {
var self = this;
// find out which revisions we need // find out which revisions we need
var revisions = []; let revisions = [];
for (var i = startRev; i < (startRev + count) && i <= this._pad.head; i++) { for (let i = startRev; i < (startRev + count) && i <= this._pad.head; i++) {
revisions.push(i); revisions.push(i);
} }
var changesets = [], authors = []; // get all needed revisions (in parallel)
let changesets = [], authors = [];
// get all needed revisions await Promise.all(revisions.map(rev => {
async.forEach(revisions, function(rev, callback) { return this._pad.getRevision(rev).then(revision => {
self._pad.getRevision(rev, function(err, revision) { let arrayNum = rev - startRev;
if (err) {
return callback(err);
}
var arrayNum = rev-startRev;
changesets[arrayNum] = revision.changeset; changesets[arrayNum] = revision.changeset;
authors[arrayNum] = revision.meta.author; authors[arrayNum] = revision.meta.author;
});
}));
callback(); return { changesets, authors };
}); }
},
function(err) {
callback(err, changesets, authors);
});
});
PadDiff.prototype._addAuthors = function(authors) { PadDiff.prototype._addAuthors = function(authors) {
var self = this; var self = this;
@ -164,39 +135,33 @@ PadDiff.prototype._addAuthors = function(authors) {
}); });
}; };
PadDiff.prototype._createDiffAtext = thenify(function(callback) { PadDiff.prototype._createDiffAtext = async function() {
var self = this;
var bulkSize = 100; let bulkSize = 100;
// get the cleaned startAText // get the cleaned startAText
self._createClearStartAtext(self._fromRev, function(err, atext) { let atext = await this._createClearStartAtext(this._fromRev);
if (err) { return callback(err); }
var superChangeset = null; let superChangeset = null;
let rev = this._fromRev + 1;
var rev = self._fromRev + 1; for (let rev = this._fromRev + 1; rev <= this._toRev; rev += bulkSize) {
// async while loop
async.whilst(
// loop condition
function () { return rev <= self._toRev; },
// loop body
function (callback) {
// get the bulk // get the bulk
self._getChangesetsInBulk(rev,bulkSize,function(err, changesets, authors) { let { changesets, authors } = await this._getChangesetsInBulk(rev, bulkSize);
var addedAuthors = [];
// run trough all changesets let addedAuthors = [];
for (var i = 0; i < changesets.length && (rev + i) <= self._toRev; i++) {
var changeset = changesets[i]; // run through all changesets
for (let i = 0; i < changesets.length && (rev + i) <= this._toRev; ++i) {
let changeset = changesets[i];
// skip clearAuthorship Changesets // skip clearAuthorship Changesets
if (self._isClearAuthorship(changeset)) { if (this._isClearAuthorship(changeset)) {
continue; continue;
} }
changeset = self._extendChangesetWithAuthor(changeset, authors[i], self._pad.pool); changeset = this._extendChangesetWithAuthor(changeset, authors[i], this._pad.pool);
// add this author to the authorarray // add this author to the authorarray
addedAuthors.push(authors[i]); addedAuthors.push(authors[i]);
@ -205,103 +170,56 @@ PadDiff.prototype._createDiffAtext = thenify(function(callback) {
if (superChangeset === null) { if (superChangeset === null) {
superChangeset = changeset; superChangeset = changeset;
} else { } else {
superChangeset = Changeset.composeWithDeletions(superChangeset, changeset, self._pad.pool); superChangeset = Changeset.composeWithDeletions(superChangeset, changeset, this._pad.pool);
} }
} }
// add the authors to the PadDiff authorArray // add the authors to the PadDiff authorArray
self._addAuthors(addedAuthors); this._addAuthors(addedAuthors);
}
// lets continue with the next bulk
rev += bulkSize;
callback();
});
},
// after the loop has ended
function (err) {
// if there are only clearAuthorship changesets, we don't get a superChangeset, so we can skip this step // if there are only clearAuthorship changesets, we don't get a superChangeset, so we can skip this step
if (superChangeset) { if (superChangeset) {
var deletionChangeset = self._createDeletionChangeset(superChangeset,atext,self._pad.pool); let deletionChangeset = this._createDeletionChangeset(superChangeset, atext, this._pad.pool);
try {
// apply the superChangeset, which includes all addings // apply the superChangeset, which includes all addings
atext = Changeset.applyToAText(superChangeset, atext, self._pad.pool); atext = Changeset.applyToAText(superChangeset, atext, this._pad.pool);
// apply the deletionChangeset, which adds a deletions // apply the deletionChangeset, which adds a deletions
atext = Changeset.applyToAText(deletionChangeset, atext, self._pad.pool); atext = Changeset.applyToAText(deletionChangeset, atext, this._pad.pool);
} catch(err) {
return callback(err)
}
} }
callback(err, atext); return atext;
} }
);
}); PadDiff.prototype.getHtml = async function() {
});
PadDiff.prototype.getHtml = thenify(function(callback) {
// cache the html // cache the html
if (this._html != null) { if (this._html != null) {
return callback(null, this._html); return this._html;
} }
var self = this;
var atext, html, authorColors;
async.series([
// get the diff atext // get the diff atext
function(callback) { let atext = await this._createDiffAtext();
self._createDiffAtext(function(err, _atext) {
if (err) {
return callback(err);
}
atext = _atext;
callback();
});
},
// get the authorColor table // get the authorColor table
function(callback) { let authorColors = await this._pad.getAllAuthorColors();
self._pad.getAllAuthorColors(function(err, _authorColors) {
if (err) {
return callback(err);
}
authorColors = _authorColors;
callback();
});
},
// convert the atext to html // convert the atext to html
function(callback) { this._html = exportHtml.getHTMLFromAtext(this._pad, atext, authorColors);
html = exportHtml.getHTMLFromAtext(self._pad, atext, authorColors);
self._html = html;
callback();
}
],
function(err) {
callback(err, html);
});
});
PadDiff.prototype.getAuthors = thenify(function(callback) { return this._html;
var self = this; }
PadDiff.prototype.getAuthors = async function() {
// check if html was already produced, if not produce it, this generates the author array at the same time // check if html was already produced, if not produce it, this generates the author array at the same time
if (self._html == null) { if (this._html == null) {
self.getHtml(function(err) { await this.getHtml();
if (err) {
return callback(err);
} }
callback(null, self._authors); return self._authors;
}); }
} else {
callback(null, self._authors);
}
});
PadDiff.prototype._extendChangesetWithAuthor = function(changeset, author, apool) { PadDiff.prototype._extendChangesetWithAuthor = function(changeset, author, apool) {
// unpack // unpack