Skip to content

Commit

Permalink
feat(event-bridge): schedule, whenAny, rename, when without scope, do…
Browse files Browse the repository at this point in the history
…cs (#161)

BREAKING_CHANGE: Event bus integration from `Function` and `StepFunction` has been updated to use the pattern `bus.putEvent(event)`. Previously events were sent by invoking the bus `bus(event)`.
BREAKING_CHANGE: Type names have been changed: `EventtBusRule` ->`Rule`, `EventBusTransform` -> `EventTransform`, `EventBusRuleInput` -> `Event`
  • Loading branch information
thantos committed Jun 2, 2022
1 parent 23d2191 commit 82c72d3
Show file tree
Hide file tree
Showing 31 changed files with 1,666 additions and 843 deletions.
56 changes: 26 additions & 30 deletions src/checker.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as ts from "typescript";
import * as tsserver from "typescript/lib/tsserverlibrary";
import { AppsyncResolver } from "./appsync";
import { EventBus, EventBusRule } from "./event-bridge";
import { EventBusTransform } from "./event-bridge/transform";
import { EventBus, Rule } from "./event-bridge";
import { EventTransform } from "./event-bridge/transform";
import { Function } from "./function";
import { ExpressStepFunction, StepFunction } from "./step-function";

Expand All @@ -17,11 +17,11 @@ export type TsFunctionParameter =
| ts.ElementAccessExpression
| ts.CallExpression;

export type EventBusRuleInterface = ts.NewExpression & {
export type RuleInterface = ts.NewExpression & {
arguments: [any, any, any, TsFunctionParameter];
};

export type EventBusTransformInterface = ts.NewExpression & {
export type EventTransformInterface = ts.NewExpression & {
arguments: [TsFunctionParameter, any];
};

Expand Down Expand Up @@ -64,11 +64,11 @@ export function makeFunctionlessChecker(
...checker,
isConstant,
isAppsyncResolver,
isEventBusRuleMapFunction,
isRuleMapFunction,
isEventBusWhenFunction,
isFunctionlessType,
isNewEventBusRule,
isNewEventBusTransform,
isNewRule,
isNewEventTransform,
isReflectFunction,
isStepFunction,
isNewFunctionlessFunction,
Expand All @@ -78,26 +78,24 @@ export function makeFunctionlessChecker(

/**
* Matches the patterns:
* * new EventBusRule()
* * new Rule()
*/
function isNewEventBusRule(node: ts.Node): node is EventBusRuleInterface {
return ts.isNewExpression(node) && isEventBusRule(node.expression);
function isNewRule(node: ts.Node): node is RuleInterface {
return ts.isNewExpression(node) && isRule(node.expression);
}

/**
* Matches the patterns:
* * new EventBusTransform()
* * new EventTransform()
*/
function isNewEventBusTransform(
node: ts.Node
): node is EventBusTransformInterface {
return ts.isNewExpression(node) && isEventBusTransform(node.expression);
function isNewEventTransform(node: ts.Node): node is EventTransformInterface {
return ts.isNewExpression(node) && isEventTransform(node.expression);
}

/**
* Matches the patterns:
* * IEventBus.when
* * IEventBusRule.when
* * IRule.when
*/
function isEventBusWhenFunction(
node: ts.Node
Expand All @@ -107,22 +105,20 @@ export function makeFunctionlessChecker(
ts.isPropertyAccessExpression(node.expression) &&
node.expression.name.text === "when" &&
(isEventBus(node.expression.expression) ||
isEventBusRule(node.expression.expression))
isRule(node.expression.expression))
);
}

/**
* Matches the patterns:
* * IEventBusRule.map()
* * IRule.map()
*/
function isEventBusRuleMapFunction(
node: ts.Node
): node is EventBusMapInterface {
function isRuleMapFunction(node: ts.Node): node is EventBusMapInterface {
return (
ts.isCallExpression(node) &&
ts.isPropertyAccessExpression(node.expression) &&
node.expression.name.text === "map" &&
isEventBusRule(node.expression.expression)
isRule(node.expression.expression)
);
}

Expand All @@ -138,25 +134,25 @@ export function makeFunctionlessChecker(
}

/**
* Checks to see if a node is of type {@link EventBusRule}.
* Checks to see if a node is of type {@link Rule}.
* The node could be any kind of node that returns an event bus rule.
*
* Matches the patterns:
* * IEventBusRule
* * IRule
*/
function isEventBusRule(node: ts.Node) {
return isFunctionlessClassOfKind(node, EventBusRule.FunctionlessType);
function isRule(node: ts.Node) {
return isFunctionlessClassOfKind(node, Rule.FunctionlessType);
}

/**
* Checks to see if a node is of type {@link EventBusTransform}.
* Checks to see if a node is of type {@link EventTransform}.
* The node could be any kind of node that returns an event bus rule.
*
* Matches the patterns:
* * IEventBusTransform
* * IEventTransform
*/
function isEventBusTransform(node: ts.Node) {
return isFunctionlessClassOfKind(node, EventBusTransform.FunctionlessType);
function isEventTransform(node: ts.Node) {
return isFunctionlessClassOfKind(node, EventTransform.FunctionlessType);
}

function isAppsyncResolver(node: ts.Node): node is ts.NewExpression & {
Expand Down
46 changes: 29 additions & 17 deletions src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import ts from "typescript";
import { assertDefined } from "./assert";
import {
EventBusMapInterface,
EventBusRuleInterface,
EventBusTransformInterface,
RuleInterface,
EventTransformInterface,
EventBusWhenInterface,
FunctionInterface,
makeFunctionlessChecker,
Expand Down Expand Up @@ -106,11 +106,11 @@ export function compile(
);
} else if (checker.isEventBusWhenFunction(node)) {
return visitEventBusWhen(node);
} else if (checker.isEventBusRuleMapFunction(node)) {
} else if (checker.isRuleMapFunction(node)) {
return visitEventBusMap(node);
} else if (checker.isNewEventBusRule(node)) {
return visitEventBusRule(node);
} else if (checker.isNewEventBusTransform(node)) {
} else if (checker.isNewRule(node)) {
return visitRule(node);
} else if (checker.isNewEventTransform(node)) {
return visitEventTransform(node);
} else if (checker.isNewFunctionlessFunction(node)) {
return visitFunction(node, ctx);
Expand Down Expand Up @@ -155,7 +155,7 @@ export function compile(
);
}

function visitEventBusRule(call: EventBusRuleInterface): ts.Node {
function visitRule(call: RuleInterface): ts.Node {
const [one, two, three, impl] = call.arguments;

return ts.factory.updateNewExpression(
Expand All @@ -171,7 +171,7 @@ export function compile(
);
}

function visitEventTransform(call: EventBusTransformInterface): ts.Node {
function visitEventTransform(call: EventTransformInterface): ts.Node {
const [impl, ...rest] = call.arguments;

return ts.factory.updateNewExpression(
Expand All @@ -183,14 +183,26 @@ export function compile(
}

function visitEventBusWhen(call: EventBusWhenInterface): ts.Node {
const [one, two, impl] = call.arguments;
// support the 2 or 3 parameter when.
if (call.arguments.length === 3) {
const [one, two, impl] = call.arguments;

return ts.factory.updateCallExpression(
call,
call.expression,
call.typeArguments,
[one, two, errorBoundary(() => toFunction("FunctionDecl", impl))]
);
return ts.factory.updateCallExpression(
call,
call.expression,
call.typeArguments,
[one, two, errorBoundary(() => toFunction("FunctionDecl", impl))]
);
} else {
const [one, impl] = call.arguments;

return ts.factory.updateCallExpression(
call,
call.expression,
call.typeArguments,
[one, errorBoundary(() => toFunction("FunctionDecl", impl))]
);
}
}

function visitEventBusMap(call: EventBusMapInterface): ts.Node {
Expand Down Expand Up @@ -288,7 +300,7 @@ export function compile(
* const bus = new EventBus() // an example of an Integration, could be any Integration
*
* (arg1: string) => {
* bus({ source: "src" })
* bus.putEvents({ source: "src" })
* }
* ```
*
Expand Down Expand Up @@ -1039,7 +1051,7 @@ export function compile(
* const bus = new EventBus()
* new Function(() => {
* const busbus = bus;
* busbus(...)
* busbus.putEvents(...)
* })
* ```
*
Expand Down

0 comments on commit 82c72d3

Please sign in to comment.