From 0040f5984e4052e7b8cd2b503b9f14b72be1fa29 Mon Sep 17 00:00:00 2001 From: webzwo0i Date: Sun, 4 Jul 2021 16:30:30 +0200 Subject: [PATCH] db: await more database operations Co-authored-by: Richard Hansen --- src/node/db/API.js | 4 ++-- src/node/db/Pad.js | 28 ++++++++++++---------------- src/node/db/ReadOnlyManager.js | 8 +++++--- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/node/db/API.js b/src/node/db/API.js index 040abf5a6..7fe0e5ca1 100644 --- a/src/node/db/API.js +++ b/src/node/db/API.js @@ -139,11 +139,11 @@ exports.getRevisionChangeset = async (padID, rev) => { } // 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 - return pad.getRevisionChangeset(head); + return await pad.getRevisionChangeset(head); }; /** diff --git a/src/node/db/Pad.js b/src/node/db/Pad.js index b7bbdb867..cca065dfc 100644 --- a/src/node/db/Pad.js +++ b/src/node/db/Pad.js @@ -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 await this.removePadIfForceIsTrueAndAlreadyExist(destinationID, force); - // copy the 'pad' entry - const pad = await this._db.get(`pad:${this.id}`); - db.set(`pad:${destinationID}`, pad); - - // copy all relations in parallel - const promises = []; + // copy all records in parallel + const promises = [ + // 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. + (async () => await db.set(`pad:${destinationID}`, await this._db.get(`pad:${this.id}`)))(), + ]; // copy all chat messages const chatHead = this.chatHead; @@ -399,7 +399,7 @@ Pad.prototype.copy = async function (destinationID, force) { promises.push(p); } - this.copyAuthorInfoToDestinationPad(destinationID); + promises.push(this.copyAuthorInfoToDestinationPad(destinationID)); // wait for the above to complete await Promise.all(promises); @@ -409,11 +409,8 @@ Pad.prototype.copy = async function (destinationID, force) { 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) - await padManager.getPad(destinationID, null); // this runs too early. + await padManager.getPad(destinationID, null); // let the plugins know the pad was copied 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 - this.getAllAuthors().forEach((authorID) => { - authorManager.addPad(authorID, destinationID); - }); + await Promise.all(this.getAllAuthors().map( + (authorID) => authorManager.addPad(authorID, destinationID))); }; Pad.prototype.copyPadWithoutHistory = async function (destinationID, force) { @@ -481,7 +477,7 @@ Pad.prototype.copyPadWithoutHistory = async function (destinationID, force) { const sourcePad = await padManager.getPad(sourceID); // 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 if (destGroupID) { diff --git a/src/node/db/ReadOnlyManager.js b/src/node/db/ReadOnlyManager.js index 0b8d29171..33ce2930a 100644 --- a/src/node/db/ReadOnlyManager.js +++ b/src/node/db/ReadOnlyManager.js @@ -41,8 +41,10 @@ exports.getReadOnlyId = async (padId) => { // there is no readOnly Entry in the database, let's create one if (readOnlyId == null) { readOnlyId = `r.${randomString(16)}`; - db.set(`pad2readonly:${padId}`, readOnlyId); - db.set(`readonly2pad:${readOnlyId}`, padId); + await Promise.all([ + db.set(`pad2readonly:${padId}`, readOnlyId), + db.set(`readonly2pad:${readOnlyId}`, padId), + ]); } return readOnlyId; @@ -52,7 +54,7 @@ exports.getReadOnlyId = async (padId) => { * returns the padId for a 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