Skip to content

Commit 6c8d4ad

Browse files
committed
start directly
1 parent adc07e5 commit 6c8d4ad

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/client/workflowMutation.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { BaseChannel } from "async-channel";
22
import { assert } from "convex-helpers";
33
import { validate, ValidationError } from "convex-helpers/validators";
44
import {
5+
createFunctionHandle,
56
internalMutationGeneric,
7+
makeFunctionReference,
68
type RegisteredMutation,
79
} from "convex/server";
810
import {
@@ -22,11 +24,18 @@ import { type RunResult, type WorkpoolOptions } from "@convex-dev/workpool";
2224
import { type WorkflowComponent } from "./types.js";
2325
import { vWorkflowId } from "../types.js";
2426
import { formatErrorWithStack } from "../shared.js";
27+
import { safeFunctionName } from "./safeFunctionName.js";
2528

26-
const workflowArgs = v.object({
27-
workflowId: vWorkflowId,
28-
generationNumber: v.number(),
29-
});
29+
const workflowArgs = v.union(
30+
v.object({
31+
workflowId: vWorkflowId,
32+
generationNumber: v.number(),
33+
}),
34+
v.object({
35+
fn: v.string(),
36+
args: v.any(),
37+
}),
38+
);
3039
const INVALID_WORKFLOW_MESSAGE = `Invalid arguments for workflow: Did you invoke the workflow with ctx.runMutation() instead of workflow.start()?`;
3140

3241
// This function is defined in the calling component but then gets passed by
@@ -47,6 +56,16 @@ export function workflowMutation<ArgsValidator extends PropertyValidators>(
4756
if (!validate(workflowArgs, args)) {
4857
throw new Error(INVALID_WORKFLOW_MESSAGE);
4958
}
59+
if ("fn" in args) {
60+
const fn = makeFunctionReference(args.fn);
61+
await ctx.runMutation(component.workflow.create, {
62+
workflowName: safeFunctionName(fn),
63+
workflowHandle: await createFunctionHandle(fn),
64+
workflowArgs: args.args,
65+
maxParallelism: workpoolOptions.maxParallelism,
66+
});
67+
return;
68+
}
5069
const { workflowId, generationNumber } = args;
5170
const { workflow, logLevel, journalEntries, ok } = await ctx.runQuery(
5271
component.journal.load,

0 commit comments

Comments
 (0)