db: await more database operations

Co-authored-by: Richard Hansen <rhansen@rhansen.org>
This commit is contained in:
webzwo0i 2021-07-04 16:30:30 +02:00 committed by Richard Hansen
parent e64462323b
commit 0040f5984e
3 changed files with 19 additions and 21 deletions

View file

@ -139,11 +139,11 @@ exports.getRevisionChangeset = async (padID, rev) => {
} }
// get the changeset for this revision // get the changeset for this revision
return pad.getRevisionChangeset(rev); return await pad.getRevisionChangeset(rev);
} }
// the client wants the latest changeset, lets return it to him // the client wants the latest changeset, lets return it to him
return pad.getRevisionChangeset(head); return await pad.getRevisionChangeset(head);
}; };
/** /**

View file

@ -376,12 +376,12 @@ Pad.prototype.copy = async function (destinationID, force) {
// if force is true and already exists a Pad with the same id, remove that Pad // if force is true and already exists a Pad with the same id, remove that Pad
await this.removePadIfForceIsTrueAndAlreadyExist(destinationID, force); await this.removePadIfForceIsTrueAndAlreadyExist(destinationID, force);
// copy the 'pad' entry // copy all records in parallel
const pad = await this._db.get(`pad:${this.id}`); const promises = [
db.set(`pad:${destinationID}`, pad); // Copy the 'pad' entry. This is wrapped in an IIFE so that this._db.get() can run in parallel
// with the other record copies done below.
// copy all relations in parallel (async () => await db.set(`pad:${destinationID}`, await this._db.get(`pad:${this.id}`)))(),
const promises = []; ];
// copy all chat messages // copy all chat messages
const chatHead = this.chatHead; const chatHead = this.chatHead;
@ -399,7 +399,7 @@ Pad.prototype.copy = async function (destinationID, force) {
promises.push(p); promises.push(p);
} }
this.copyAuthorInfoToDestinationPad(destinationID); promises.push(this.copyAuthorInfoToDestinationPad(destinationID));
// wait for the above to complete // wait for the above to complete
await Promise.all(promises); await Promise.all(promises);
@ -409,11 +409,8 @@ Pad.prototype.copy = async function (destinationID, force) {
await db.setSub(`group:${destGroupID}`, ['pads', destinationID], 1); await db.setSub(`group:${destGroupID}`, ['pads', destinationID], 1);
} }
// delay still necessary?
await new Promise((resolve) => setTimeout(resolve, 10));
// Initialize the new pad (will update the listAllPads cache) // Initialize the new pad (will update the listAllPads cache)
await padManager.getPad(destinationID, null); // this runs too early. await padManager.getPad(destinationID, null);
// let the plugins know the pad was copied // let the plugins know the pad was copied
await hooks.aCallAll('padCopy', {originalPad: this, destinationID}); await hooks.aCallAll('padCopy', {originalPad: this, destinationID});
@ -459,11 +456,10 @@ Pad.prototype.removePadIfForceIsTrueAndAlreadyExist = async function (destinatio
} }
}; };
Pad.prototype.copyAuthorInfoToDestinationPad = function (destinationID) { Pad.prototype.copyAuthorInfoToDestinationPad = async function (destinationID) {
// add the new sourcePad to all authors who contributed to the old one // add the new sourcePad to all authors who contributed to the old one
this.getAllAuthors().forEach((authorID) => { await Promise.all(this.getAllAuthors().map(
authorManager.addPad(authorID, destinationID); (authorID) => authorManager.addPad(authorID, destinationID)));
});
}; };
Pad.prototype.copyPadWithoutHistory = async function (destinationID, force) { Pad.prototype.copyPadWithoutHistory = async function (destinationID, force) {
@ -481,7 +477,7 @@ Pad.prototype.copyPadWithoutHistory = async function (destinationID, force) {
const sourcePad = await padManager.getPad(sourceID); const sourcePad = await padManager.getPad(sourceID);
// add the new sourcePad to all authors who contributed to the old one // add the new sourcePad to all authors who contributed to the old one
this.copyAuthorInfoToDestinationPad(destinationID); await this.copyAuthorInfoToDestinationPad(destinationID);
// Group pad? Add it to the group's list // Group pad? Add it to the group's list
if (destGroupID) { if (destGroupID) {

View file

@ -41,8 +41,10 @@ exports.getReadOnlyId = async (padId) => {
// there is no readOnly Entry in the database, let's create one // there is no readOnly Entry in the database, let's create one
if (readOnlyId == null) { if (readOnlyId == null) {
readOnlyId = `r.${randomString(16)}`; readOnlyId = `r.${randomString(16)}`;
db.set(`pad2readonly:${padId}`, readOnlyId); await Promise.all([
db.set(`readonly2pad:${readOnlyId}`, padId); db.set(`pad2readonly:${padId}`, readOnlyId),
db.set(`readonly2pad:${readOnlyId}`, padId),
]);
} }
return readOnlyId; return readOnlyId;
@ -52,7 +54,7 @@ exports.getReadOnlyId = async (padId) => {
* returns the padId for a read only id * returns the padId for a read only id
* @param {String} readOnlyId read only id * @param {String} readOnlyId read only id
*/ */
exports.getPadId = (readOnlyId) => db.get(`readonly2pad:${readOnlyId}`); exports.getPadId = async (readOnlyId) => await db.get(`readonly2pad:${readOnlyId}`);
/** /**
* returns the padId and readonlyPadId in an object for any id * returns the padId and readonlyPadId in an object for any id