Skip to content

Commit

Permalink
Exclude triggers with empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-McFarlin committed Jun 10, 2024
1 parent 48f5c56 commit c483e0b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/entity/src/__tests__/EntityCompanion-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ describe(EntityCompanion, () => {
expect(mergedTriggers.afterCreate![1]).toBe(globalMutationTriggers.afterCreate![0]);
expect(mergedTriggers.afterAll![0]).toBe(localTriggers!.afterAll![0]);
expect(mergedTriggers.afterAll![1]).toBe(globalMutationTriggers!.afterAll![0]);
expect(mergedTriggers.afterCommit![0]).toBe(localTriggers!.afterCommit![0]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { EntityCompanionDefinition } from '../EntityCompanionProvider';
import EntityConfiguration from '../EntityConfiguration';
import { StringField, UUIDField } from '../EntityFields';
import { EntityTriggerMutationInfo } from '../EntityMutationInfo';
import { EntityMutationTrigger } from '../EntityMutationTriggerConfiguration';
import {
EntityMutationTrigger,
EntityNonTransactionalMutationTrigger,
} from '../EntityMutationTriggerConfiguration';
import EntityPrivacyPolicy from '../EntityPrivacyPolicy';
import { EntityQueryContext } from '../EntityQueryContext';
import ViewerContext from '../ViewerContext';
Expand Down Expand Up @@ -97,6 +100,33 @@ export class TestMutationTrigger extends EntityMutationTrigger<
): Promise<void> {}
}

export class NonTransactionalTestMutationTrigger extends EntityNonTransactionalMutationTrigger<
TestMTFields,
string,
ViewerContext,
TestEntityWithMutationTriggers,
keyof TestMTFields
> {
constructor(
// @ts-expect-error key is never used but is helpful for debugging
private readonly key: string,
) {
super();
}

async executeAsync(
_viewerContext: ViewerContext,
_entity: TestEntityWithMutationTriggers,
_mutationInfo: EntityTriggerMutationInfo<
TestMTFields,
string,
ViewerContext,
TestEntityWithMutationTriggers,
keyof TestMTFields
>,
): Promise<void> {}
}

/**
* A test Entity that has one afterCreate and one afterAll trigger
*/
Expand All @@ -119,6 +149,7 @@ export default class TestEntityWithMutationTriggers extends Entity<
mutationTriggers: {
afterCreate: [new TestMutationTrigger('localAfterCreate')],
afterAll: [new TestMutationTrigger('localAfterAll')],
afterCommit: [new NonTransactionalTestMutationTrigger('localAfterCommit')],
},
};
}
Expand Down
11 changes: 10 additions & 1 deletion packages/entity/src/utils/mergeMutationTriggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function mergeEntityMutationTriggerConfigurations<
TSelectedFields
>[]
): EntityMutationTriggerConfiguration<TFields, TID, TViewerContext, TEntity, TSelectedFields> {
return {
const merged = {
beforeCreate: mutationTriggerConfigurations.flatMap((c) => c.beforeCreate).filter(nonNullish),
afterCreate: mutationTriggerConfigurations.flatMap((c) => c.afterCreate).filter(nonNullish),
beforeUpdate: mutationTriggerConfigurations.flatMap((c) => c.beforeUpdate).filter(nonNullish),
Expand All @@ -32,4 +32,13 @@ export function mergeEntityMutationTriggerConfigurations<
afterAll: mutationTriggerConfigurations.flatMap((c) => c.afterAll).filter(nonNullish),
afterCommit: mutationTriggerConfigurations.flatMap((c) => c.afterCommit).filter(nonNullish),
};

/** Remove any trigger that is an empty array */
for (const key of Object.keys(merged) as (keyof typeof merged)[]) {
if (!merged[key]?.length) {
delete merged[key];
}
}

return merged;
}

0 comments on commit c483e0b

Please sign in to comment.