revises hook arrays to run synchronously where necessary

This commit is contained in:
James
2020-07-17 23:00:18 -04:00
parent fc3aa7315a
commit 98eec002c0
10 changed files with 59 additions and 37 deletions

View File

@@ -28,15 +28,17 @@ const register = async (args) => {
}
// /////////////////////////////////////
// 2. Execute before register hook
// 2. Execute before create hook
// /////////////////////////////////////
collectionConfig.hooks.beforeRegister.forEach(async (hook) => {
await collectionConfig.hooks.beforeCreate.reduce(async (priorHook, hook) => {
await priorHook;
data = (await hook({
data,
req,
})) || data;
});
}, Promise.resolve());
// /////////////////////////////////////
// 3. Execute field-level hooks, access, and validation
@@ -82,15 +84,17 @@ const register = async (args) => {
});
// /////////////////////////////////////
// 8. Execute after register hook
// 8. Execute after create hook
// /////////////////////////////////////
collectionConfig.hooks.afterRegister.forEach(async (hook) => {
collectionConfig.hooks.afterCreate.reduce(async (priorHook, hook) => {
await priorHook;
result = await hook({
doc: result,
req: args.req,
}) || result;
});
}, Promise.resolve());
// /////////////////////////////////////
// 9. Return user

View File

@@ -56,13 +56,15 @@ const update = async (args) => {
// 2. Execute before update hook
// /////////////////////////////////////
collectionConfig.hooks.beforeUpdate.forEach(async (hook) => {
await collectionConfig.hooks.beforeUpdate.reduce(async (priorHook, hook) => {
await priorHook;
data = (await hook({
data,
req,
originalDoc,
})) || data;
});
}, Promise.resolve());
// /////////////////////////////////////
// 3. Merge updates into existing data
@@ -119,12 +121,14 @@ const update = async (args) => {
// 8. Execute after update hook
// /////////////////////////////////////
collectionConfig.hooks.afterUpdate.forEach(async (hook) => {
collectionConfig.hooks.afterUpdate.reduce(async (priorHook, hook) => {
await priorHook;
user = await hook({
doc: user,
req,
}) || user;
});
}, Promise.resolve());
// /////////////////////////////////////
// 9. Return user