Pad: Call `padCreate`, `padUpdate` hooks asynchronously

pull/5512/head
Richard Hansen 2021-11-27 18:15:52 -05:00
parent 2ca740c1db
commit ff494563d9
2 changed files with 20 additions and 25 deletions

View File

@ -75,13 +75,13 @@
* The `destinationID` context property is deprecated; use `dstPad.id` * The `destinationID` context property is deprecated; use `dstPad.id`
instead. instead.
* `padCreate`: The `author` context property is deprecated; use the new * `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. * `padLoad`: Now runs when a temporary Pad object is created during import.
Also, it now runs asynchronously. Also, it now runs asynchronously.
* `padRemove`: The `padID` context property is deprecated; use `pad.id` * `padRemove`: The `padID` context property is deprecated; use `pad.id`
instead. instead.
* `padUpdate`: The `author` context property is deprecated; use the new * `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; * Returning `true` from a `handleMessageSecurity` hook function is deprecated;
return `'permitOnce'` instead. return `'permitOnce'` instead.
* Changes to the `src/static/js/Changeset.js` library: * Changes to the `src/static/js/Changeset.js` library:

View File

@ -84,7 +84,8 @@ class Pad {
// ex. getNumForAuthor // ex. getNumForAuthor
if (authorId !== '') this.pool.putAttrib(['author', authorId]); 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}`, { this.db.set(`pad:${this.id}:revs:${newRev}`, {
changeset: aChangeset, changeset: aChangeset,
meta: { meta: {
@ -98,10 +99,7 @@ class Pad {
}), }),
this.saveToDatabase(), this.saveToDatabase(),
authorId && authorManager.addPad(authorId, this.id), authorId && authorManager.addPad(authorId, this.id),
]); hooks.aCallAll(hook, {
let hook = 'padCreate';
const context = {
pad: this, pad: this,
authorId, authorId,
get author() { get author() {
@ -112,15 +110,12 @@ class Pad {
warnDeprecated(`${hook} hook author context is deprecated; use authorId instead`); warnDeprecated(`${hook} hook author context is deprecated; use authorId instead`);
this.authorId = authorId; this.authorId = authorId;
}, },
}; ...this.head === 0 ? {} : {
if (this.head !== 0) { revs: newRev,
hook = 'padUpdate'; changeset: aChangeset,
context.revs = newRev; },
context.changeset = aChangeset; }),
} ]);
hooks.callAll(hook, context);
await p;
return newRev; return newRev;
} }