Skip to content

Commit

Permalink
fix: ensures hooks run before access
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikrut committed Aug 30, 2021
1 parent c187da0 commit 96629f1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/fields/hookPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ type Arguments = {
fullData: Record<string, unknown>
}

const hookPromise = ({
const hookPromise = async ({
data,
field,
hook,
req,
operation,
fullOriginalDoc,
fullData,
}: Arguments) => async (): Promise<void> => {
}: Arguments): Promise<void> => {
const resultingData = data;

if (field.hooks && field.hooks[hook]) {
Expand Down
14 changes: 6 additions & 8 deletions src/fields/performFieldOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,10 @@ export default async function performFieldOperations(this: Payload, entityConfig
transformActions.forEach((action) => action());
}

hookPromises.forEach((promise) => promise());

await Promise.all(hookPromises);
const hookResults = hookPromises.map((promise) => promise());
await Promise.all(hookResults);

validationPromises.forEach((promise) => promise());

await Promise.all(validationPromises);

if (errors.length > 0) {
Expand All @@ -125,11 +123,11 @@ export default async function performFieldOperations(this: Payload, entityConfig

unflattenLocaleActions.forEach((action) => action());

await Promise.all(accessPromises);

const relationshipPopulationPromises = relationshipPopulations.map((population) => population());
const accessResults = accessPromises.map((promise) => promise());
await Promise.all(accessResults);

await Promise.all(relationshipPopulationPromises);
const relationshipPopulationResults = relationshipPopulations.map((population) => population());
await Promise.all(relationshipPopulationResults);

return fullData;
}
6 changes: 3 additions & 3 deletions src/fields/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Arguments = {
flattenLocales: boolean
locale: string
fallbackLocale: string
accessPromises: Promise<void>[]
accessPromises: (() => Promise<void>)[]
operation: Operation
overrideAccess: boolean
req: PayloadRequest
Expand Down Expand Up @@ -173,7 +173,7 @@ const traverseFields = (args: Arguments): void => {
});
}

accessPromises.push(accessPromise({
accessPromises.push(() => accessPromise({
data,
fullData,
originalDoc,
Expand All @@ -189,7 +189,7 @@ const traverseFields = (args: Arguments): void => {
payload,
}));

hookPromises.push(hookPromise({
hookPromises.push(() => hookPromise({
data,
field,
hook,
Expand Down

0 comments on commit 96629f1

Please sign in to comment.