Revert "Add support for running multiple callback hooks at the same time"

This reverts commit ffb2ed555b.
This commit is contained in:
SachaG 2019-01-22 09:52:16 +09:00
parent ffb2ed555b
commit 12df83e23f
2 changed files with 28 additions and 23 deletions

View file

@ -77,15 +77,6 @@ export const runCallbacks = function () {
if (typeof arguments[0] === 'object' && arguments.length === 1) {
const singleArgument = arguments[0];
hook = singleArgument.name;
// if an array of callback hook names is passed, call `runCallbacks` for each hook
if (Array.isArray(hook)) {
hook.forEach(name => {
runCallbacks({ ...singleArgument, name });
});
return;
}
formattedHook = formatHookName(hook);
item = singleArgument.iterator;
args = singleArgument.properties;

View file

@ -62,7 +62,8 @@ export const createMutator = async ({ collection, document, data, currentUser, v
validationErrors = validationErrors.concat(validateDocument(newDocument, collection, context));
// run validation callbacks
validationErrors = await runCallbacks({ name: [`${typeName.toLowerCase()}.create.validate`, '*.create.validate'], iterator: validationErrors, properties, });
validationErrors = await runCallbacks({ name: `${typeName.toLowerCase()}.create.validate`, iterator: validationErrors, properties, });
validationErrors = await runCallbacks({ name: '*.create.validate', iterator: validationErrors, properties, });
// OpenCRUD backwards compatibility
newDocument = await runCallbacks(`${collectionName.toLowerCase()}.new.validate`, newDocument, currentUser, validationErrors);
@ -111,15 +112,18 @@ export const createMutator = async ({ collection, document, data, currentUser, v
// }
// run sync callbacks
newDocument = await runCallbacks({ name: [`${typeName.toLowerCase()}.create.before`, '*.create.before'], iterator: newDocument, properties, });
newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.create.before`, iterator: newDocument, properties, });
newDocument = await runCallbacks({ name: '*.create.before', iterator: newDocument, properties, });
// OpenCRUD backwards compatibility
newDocument = await runCallbacks([`${collectionName.toLowerCase()}.new.before`, `${collectionName.toLowerCase()}.new.sync`], newDocument, currentUser);
newDocument = await runCallbacks(`${collectionName.toLowerCase()}.new.before`, newDocument, currentUser);
newDocument = await runCallbacks(`${collectionName.toLowerCase()}.new.sync`, newDocument, currentUser);
// add _id to document
newDocument._id = await Connectors.create(collection, newDocument);
// run any post-operation sync callbacks
newDocument = await runCallbacks({ name: [`${typeName.toLowerCase()}.create.after`, '*.create.after'], iterator: newDocument, properties, });
newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.create.after`, iterator: newDocument, properties, });
newDocument = await runCallbacks({ name: '*.create.after', iterator: newDocument, properties, });
// OpenCRUD backwards compatibility
newDocument = await runCallbacks(`${collectionName.toLowerCase()}.new.after`, newDocument, currentUser);
@ -128,7 +132,8 @@ export const createMutator = async ({ collection, document, data, currentUser, v
// run async callbacks
// note: query for document to get fresh document with collection-hooks effects applied
await runCallbacksAsync({ name: [`${typeName.toLowerCase()}.create.async`, '*.create.async'], properties, });
await runCallbacksAsync({ name: `${typeName.toLowerCase()}.create.async`, properties, });
await runCallbacksAsync({ name: '*.create.async', properties, });
// OpenCRUD backwards compatibility
await runCallbacksAsync(`${collectionName.toLowerCase()}.new.async`, newDocument, currentUser, collection);
@ -185,7 +190,8 @@ export const updateMutator = async ({ collection, documentId, selector, data, se
validationErrors = validationErrors.concat(validateData(data, document, collection, context));
validationErrors = await runCallbacks({ name: [`${typeName.toLowerCase()}.update.validate`, '*.update.validate'], iterator: validationErrors, properties, });
validationErrors = await runCallbacks({ name: `${typeName.toLowerCase()}.update.validate`, iterator: validationErrors, properties, });
validationErrors = await runCallbacks({ name: '*.update.validate', iterator: validationErrors, properties, });
// OpenCRUD backwards compatibility
data = modifierToData(await runCallbacks(`${collectionName.toLowerCase()}.edit.validate`, dataToModifier(data), document, currentUser, validationErrors));
@ -216,9 +222,11 @@ export const updateMutator = async ({ collection, documentId, selector, data, se
}
// run sync callbacks
data = await runCallbacks({ name:[ `${typeName.toLowerCase()}.update.before`, '*.update.before'], iterator: data, properties,});
data = await runCallbacks({ name: `${typeName.toLowerCase()}.update.before`, iterator: data, properties,});
data = await runCallbacks({ name: '*.update.before', iterator: data, properties,});
// OpenCRUD backwards compatibility
data = modifierToData(await runCallbacks([`${collectionName.toLowerCase()}.edit.before`, `${collectionName.toLowerCase()}.edit.sync`], dataToModifier(data), document, currentUser, newDocument));
data = modifierToData(await runCallbacks(`${collectionName.toLowerCase()}.edit.before`, dataToModifier(data), document, currentUser, newDocument));
data = modifierToData(await runCallbacks(`${collectionName.toLowerCase()}.edit.sync`, dataToModifier(data), document, currentUser, newDocument));
// update connector requires a modifier, so get it from data
const modifier = dataToModifier(data);
@ -247,12 +255,14 @@ export const updateMutator = async ({ collection, documentId, selector, data, se
}
// run any post-operation sync callbacks
newDocument = await runCallbacks({ name: [`${typeName.toLowerCase()}.update.after`, '*.update.after'], iterator: newDocument, properties, });
newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.update.after`, iterator: newDocument, properties, });
newDocument = await runCallbacks({ name: '*.update.after', iterator: newDocument, properties, });
// OpenCRUD backwards compatibility
newDocument = await runCallbacks(`${collectionName.toLowerCase()}.edit.after`, newDocument, document, currentUser);
// run async callbacks
await runCallbacksAsync({ name: [`${typeName.toLowerCase()}.update.async`, '*.update.async'], properties,});
await runCallbacksAsync({ name: `${typeName.toLowerCase()}.update.async`, properties,});
await runCallbacksAsync({ name: '*.update.async', properties,});
// OpenCRUD backwards compatibility
await runCallbacksAsync(`${collectionName.toLowerCase()}.edit.async`, newDocument, document, currentUser, collection);
@ -295,7 +305,8 @@ export const deleteMutator = async ({ collection, documentId, selector, currentU
let validationErrors = [];
validationErrors = await runCallbacks({ name: [`${typeName.toLowerCase()}.delete.validate`, '*.delete.validate'], iterator: validationErrors, properties, });
validationErrors = await runCallbacks({ name: `${typeName.toLowerCase()}.delete.validate`, iterator: validationErrors, properties, });
validationErrors = await runCallbacks({ name: '*.delete.validate', iterator: validationErrors, properties, });
// OpenCRUD backwards compatibility
document = await runCallbacks(`${collectionName.toLowerCase()}.remove.validate`, document, currentUser);
@ -321,9 +332,11 @@ export const deleteMutator = async ({ collection, documentId, selector, currentU
}
}
await runCallbacks({ name: [`${typeName.toLowerCase()}.delete.before`, '*.delete.before'], iterator: document, properties, });
await runCallbacks({ name: `${typeName.toLowerCase()}.delete.before`, iterator: document, properties, });
await runCallbacks({ name: '*.delete.before', iterator: document, properties, });
// OpenCRUD backwards compatibility
await runCallbacks([`${collectionName.toLowerCase()}.remove.before`, `${collectionName.toLowerCase()}.remove.sync`], document, currentUser);
await runCallbacks(`${collectionName.toLowerCase()}.remove.before`, document, currentUser);
await runCallbacks(`${collectionName.toLowerCase()}.remove.sync`, document, currentUser);
await Connectors.delete(collection, selector);
@ -333,7 +346,8 @@ export const deleteMutator = async ({ collection, documentId, selector, currentU
collection.loader.clear(selector.documentId);
}
await runCallbacksAsync({ name: [`${typeName.toLowerCase()}.delete.async`, '*.delete.async'], properties, });
await runCallbacksAsync({ name: `${typeName.toLowerCase()}.delete.async`, properties, });
await runCallbacksAsync({ name: '*.delete.async', properties, });
// OpenCRUD backwards compatibility
await runCallbacksAsync(`${collectionName.toLowerCase()}.remove.async`, document, currentUser, collection);