Skip to content

Commit 1b2ffcc

Browse files
committed
allow passing just eventId on send
1 parent 3e7a7be commit 1b2ffcc

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

example/convex/_generated/api.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export declare const components: {
6767
| { kind: "success"; returnValue: any }
6868
| { error: string; kind: "failed" }
6969
| { kind: "canceled" };
70-
workflowId: string;
70+
workflowId?: string;
7171
workpoolOptions?: {
7272
defaultRetryBehavior?: {
7373
base: number;

src/component/_generated/api.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export type Mounts = {
5757
| { kind: "success"; returnValue: any }
5858
| { error: string; kind: "failed" }
5959
| { kind: "canceled" };
60-
workflowId: string;
60+
workflowId?: string;
6161
workpoolOptions?: {
6262
defaultRetryBehavior?: {
6363
base: number;

src/component/event.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export async function awaitEvent(
6565

6666
async function getOrCreateEvent(
6767
ctx: MutationCtx,
68-
workflowId: Id<"workflows">,
68+
workflowId: Id<"workflows"> | undefined,
6969
args: { eventId?: Id<"events">; name?: string },
7070
statuses: Doc<"events">["state"]["kind"][],
7171
): Promise<Doc<"events">> {
@@ -79,6 +79,7 @@ async function getOrCreateEvent(
7979
return event;
8080
}
8181
assert(args.name, "Name is required if eventId is not specified");
82+
assert(workflowId, "workflowId is required if eventId is not specified");
8283
for (const status of statuses) {
8384
const event = await ctx.db
8485
.query("events")
@@ -101,7 +102,7 @@ async function getOrCreateEvent(
101102

102103
export const send = mutation({
103104
args: {
104-
workflowId: v.id("workflows"),
105+
workflowId: v.optional(v.id("workflows")),
105106
eventId: v.optional(v.id("events")),
106107
name: v.optional(v.string()),
107108
result: vResultValidator,
@@ -118,16 +119,17 @@ export const send = mutation({
118119
},
119120
["waiting", "created"],
120121
);
122+
const { workflowId } = event;
121123
const name = args.name ?? event.name;
122124
switch (event.state.kind) {
123125
case "sent": {
124126
throw new Error(
125-
`Event already sent: ${event._id} (${name}) in workflow ${args.workflowId}`,
127+
`Event already sent: ${event._id} (${name}) in workflow ${workflowId}`,
126128
);
127129
}
128130
case "consumed": {
129131
throw new Error(
130-
`Event already consumed: ${event._id} (${name}) in workflow ${args.workflowId}`,
132+
`Event already consumed: ${event._id} (${name}) in workflow ${workflowId}`,
131133
);
132134
}
133135
case "created": {
@@ -140,7 +142,7 @@ export const send = mutation({
140142
const step = await ctx.db.get(event.state.stepId);
141143
assert(
142144
step,
143-
`Entry ${event.state.stepId} not found when sending event ${event._id} (${name}) in workflow ${args.workflowId}`,
145+
`Entry ${event.state.stepId} not found when sending event ${event._id} (${name}) in workflow ${workflowId}`,
144146
);
145147
assert(step.step.kind === "event", "Step is not an event");
146148
step.step.eventId = event._id;
@@ -160,13 +162,13 @@ export const send = mutation({
160162
const anyMoreEvents = await ctx.db
161163
.query("events")
162164
.withIndex("workflowId_state", (q) =>
163-
q.eq("workflowId", args.workflowId).eq("state.kind", "waiting"),
165+
q.eq("workflowId", workflowId).eq("state.kind", "waiting"),
164166
)
165167
.order("desc")
166168
.first();
167169
if (!anyMoreEvents) {
168-
const workflow = await ctx.db.get(args.workflowId);
169-
assert(workflow, `Workflow ${args.workflowId} not found`);
170+
const workflow = await ctx.db.get(workflowId);
171+
assert(workflow, `Workflow ${workflowId} not found`);
170172
const workpool = await getWorkpool(ctx, args.workpoolOptions);
171173
await enqueueWorkflow(ctx, workflow, workpool);
172174
}

src/types.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ export type EventId<Name extends string = string> = string & {
2222
export type VEventId<Name extends string> = VString<EventId<Name>>;
2323
export const vEventId = v.string() as VString<EventId<string>>;
2424

25-
export type EventSpec<Name extends string = string, T = unknown> = {
26-
name: Name;
25+
export type EventSpec<Name extends string = string, T = unknown> = (
26+
| { name: Name; id?: EventId<Name> }
27+
| { name?: Name; id: EventId<Name> }
28+
) & {
2729
validator?: Validator<T, any, any>;
28-
id?: EventId<Name>;
2930
};
3031

3132
export type WorkflowStep = {
@@ -40,18 +41,9 @@ export type WorkflowStep = {
4041
startedAt: number;
4142
completedAt?: number;
4243
} & (
43-
| {
44-
kind: "function";
45-
workId: WorkId;
46-
}
47-
| {
48-
kind: "workflow";
49-
nestedWorkflowId: WorkflowId;
50-
}
51-
| {
52-
kind: "event";
53-
eventId: EventId;
54-
}
44+
| { kind: "function"; workId: WorkId }
45+
| { kind: "workflow"; nestedWorkflowId: WorkflowId }
46+
| { kind: "event"; eventId: EventId }
5547
);
5648

5749
export const vWorkflowStep = v.object({

0 commit comments

Comments
 (0)