From ff494563d940c94ce9b71e0676f6114389c828be Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 27 Nov 2021 18:15:52 -0500 Subject: [PATCH] Pad: Call `padCreate`, `padUpdate` hooks asynchronously --- CHANGELOG.md | 4 ++-- src/node/db/Pad.js | 41 ++++++++++++++++++----------------------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6db5e47b..0bdd41a50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,13 +75,13 @@ * The `destinationID` context property is deprecated; use `dstPad.id` instead. * `padCreate`: The `author` context property is deprecated; use the new - `authorId` context property instead. + `authorId` context property instead. Also, the hook now runs asynchronously. * `padLoad`: Now runs when a temporary Pad object is created during import. Also, it now runs asynchronously. * `padRemove`: The `padID` context property is deprecated; use `pad.id` instead. * `padUpdate`: The `author` context property is deprecated; use the new - `authorId` context property instead. + `authorId` context property instead. Also, the hook now runs asynchronously. * Returning `true` from a `handleMessageSecurity` hook function is deprecated; return `'permitOnce'` instead. * Changes to the `src/static/js/Changeset.js` library: diff --git a/src/node/db/Pad.js b/src/node/db/Pad.js index 6e3fbf2c3..6feaf34af 100644 --- a/src/node/db/Pad.js +++ b/src/node/db/Pad.js @@ -84,7 +84,8 @@ class Pad { // ex. getNumForAuthor if (authorId !== '') this.pool.putAttrib(['author', authorId]); - const p = Promise.all([ + const hook = this.head === 0 ? 'padCreate' : 'padUpdate'; + await Promise.all([ this.db.set(`pad:${this.id}:revs:${newRev}`, { changeset: aChangeset, meta: { @@ -98,29 +99,23 @@ class Pad { }), this.saveToDatabase(), authorId && authorManager.addPad(authorId, this.id), + hooks.aCallAll(hook, { + pad: this, + authorId, + get author() { + warnDeprecated(`${hook} hook author context is deprecated; use authorId instead`); + return this.authorId; + }, + set author(authorId) { + warnDeprecated(`${hook} hook author context is deprecated; use authorId instead`); + this.authorId = authorId; + }, + ...this.head === 0 ? {} : { + revs: newRev, + changeset: aChangeset, + }, + }), ]); - - let hook = 'padCreate'; - const context = { - pad: this, - authorId, - get author() { - warnDeprecated(`${hook} hook author context is deprecated; use authorId instead`); - return this.authorId; - }, - set author(authorId) { - warnDeprecated(`${hook} hook author context is deprecated; use authorId instead`); - this.authorId = authorId; - }, - }; - if (this.head !== 0) { - hook = 'padUpdate'; - context.revs = newRev; - context.changeset = aChangeset; - } - hooks.callAll(hook, context); - - await p; return newRev; }