Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda,event-bridge): lambda event sources #179

Merged
merged 58 commits into from Jun 11, 2022
Merged

Conversation

thantos
Copy link
Collaborator

@thantos thantos commented May 31, 2022

Closes #44

Depends on #161 - This change made major changes to EventBus, stacking these changes to reduce churn.

  • Lambda
  • Step Functions - Already done
    ❌ Api Gateway
    ❌ App Sync
    ❌ Dynamo

Lambda

Unlike Step Functions, Lambda does not emit events by default. Lambda has a concept of a destination or async invocation. Each lambda, version, and alias can only have a single onSuccess and onFailure destination. Destinations can be EventBuses, Sns topics, Sqs queues, or other lambda functions.

const func = new Function(stack, 'func', {
   onSuccess: bus, // IEventBus | IFunction | aws_lambda.IDestination
   onFailure: bus // IEventBus | IFunction | aws_lambda.IDestination
});

// fail when onSuccess is not set and not a bus?
func.onSuccess(bus).map().pipe();
func.onFailure(bus).map().pipe();

@thantos thantos changed the base branch from main to event_bus_v2_2 May 31, 2022 21:33
src/function.ts Show resolved Hide resolved
src/function.ts Outdated Show resolved Hide resolved
@thantos thantos changed the title feat(lambda,step-functions,event-bridge): event sources feat(lambda,event-bridge): event sources Jun 1, 2022
@thantos thantos marked this pull request as ready for review June 1, 2022 13:46
@thantos thantos requested a review from sam-goodwin June 7, 2022 22:26
src/event-bridge/event-bus.ts Show resolved Hide resolved
@@ -115,16 +120,16 @@ export interface IEventBusFilterable<E extends Event> {
): Rule<E, O>;
}

export interface IEventBus<E extends Event = Event>
export interface IEventBus<in E extends Event = Event, O extends E = E>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to document the type parameters. Not immediately obvious what E and O mean

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In standard libraries, I usually prefer to use words for type parameters instead of symbols.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that was a helpful exercise.

/**
 * @typeParam Evnt - the union type of events that this EventBus can accept.
 *                   `Evnt` is the covariant version of `OutEnvt` in that
 *                   the bus will accept any of `Evnt` while the EventBus can
 *                   emit any of `OutEvnt`.
 * @typeParam OutEvnt - the union type of events that this EventBus will emit through rules.
 *                      `OutEvnt` is the contravariant version of `Envt` in that
 *                      the bus will emit any of `OutEvnt` while the EventBus can
 *                      can accept any of `Evnt`. This type parameter should be left
 *                      empty to be inferred.
 */

src/event-bridge/event-bus.ts Outdated Show resolved Hide resolved
*
* ```ts
* const bus = new EventBus(stack, 'bus');
* new Function(stack, 'func', { onSuccess: bus }, async () => {});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

src/function.ts Outdated Show resolved Hide resolved
@sam-goodwin
Copy link
Collaborator

Should the title of the PR be more granular? Would someone reading the CHANGELOG know what this change is about based purely on the title?

@thantos
Copy link
Collaborator Author

thantos commented Jun 8, 2022

Should the title of the PR be more granular? Would someone reading the CHANGELOG know what this change is about based purely on the title?

Ha... the change started with higher ambitions and then I realized that no one else but lambda and step function publish events!

Will update.

@thantos thantos changed the title feat(lambda,event-bridge): event sources feat(lambda,event-bridge): lambda event sources Jun 8, 2022
* @typeParam - Evnt - The original event type from the {@link EventBus}.
* @typeParam - OutEvnt - The narrowed event type after the predicate is applied.
*/
export interface IRule<in Evnt extends Event, out OutEvnt extends Evnt> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need two type parameters?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Evnt is the event in. The input to the predicate on the rule.

OutEvnt is the narrowed type as the input to pipe or map or when.

Essentially splitting and showing the two sides of the predicate.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need Evnt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its being used on integration: IntegrationWithEventBus<Evnt, Props>,

But that doesn't seem right... will look.

when<O extends E>(
predicate: RulePredicateFunction<InEvnt, NewEvnt>
): Rule<InEvnt, NewEvnt>;
when<InEvnt extends OutEvnt, NewEvnt extends InEvnt>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two parameters seem overly complex and redundant. Why did you add them?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to invert the inEvnttoout NewEvntandout OutEvnttoin InEvnt` for the when method.

extends IEventBusFilterable<E>,
/**
* @typeParam Evnt - the union type of events that this EventBus can accept.
* `Evnt` is the covariant version of `OutEvnt` in that
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Evnt is contra-variant, not co-variant. out is for covariance

* can accept any of `Evnt`. This type parameter should be left
* empty to be inferred. ex: `EventBus<Event<Detail1> | Event<Detail2>>`.
*/
export interface IEventBus<in Evnt extends Event = Event, OutEvnt extends Evnt = Evnt>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right. I think we can remove the OutEvnt

Copy link
Collaborator

@sam-goodwin sam-goodwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can modify the types in a subsequent change. Ship it

@thantos
Copy link
Collaborator Author

thantos commented Jun 11, 2022

We can modify the types in a subsequent change. Ship it

Removed the unessecary in Evnt from IRule and RuleBase which cleaned up a bit.

Also fixed the covariance/contravariant naming issues and think I understand them better now :-)

https://dmitripavlutin.com/typescript-covariance-contravariance/

@thantos
Copy link
Collaborator Author

thantos commented Jun 11, 2022

Error: Cannot find module '/opt/build/repo/scripts/compile-error-code-page'

I don't understand what changed

@thantos thantos merged commit 509e6f7 into main Jun 11, 2022
@thantos thantos deleted the event_source_2 branch June 11, 2022 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Event Sources
2 participants