Skip to content

Commit

Permalink
feat: Add support for invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin67 committed May 28, 2024
1 parent 210e2bf commit d4c96e9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
42 changes: 42 additions & 0 deletions lib/inngest/step_tool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,48 @@ defmodule Inngest.StepTool do
end
end

@spec invoke(Context.t(), binary(), map()) :: map()
def invoke(%{steps: steps} = ctx, step_id, opts) do
op = UnhashedOp.new(ctx, "InvokeFunction", step_id, opts)
hashed_id = UnhashedOp.hash(op)

case Map.get(steps, hashed_id) do
nil ->
func = Map.get(opts, :function)
data = Map.get(opts, :data)
timeout = Map.get(opts, :timeout)
v = Map.get(opts, :v)

generator_otps =
cond do
Map.has_key?(opts, :timeout) ->
%{
function_id: func.slug(),
payload: %{data: data, v: v},
timeout: timeout
}

true ->
%{
function_id: func.slug(),
payload: %{data: data, v: v}
}
end

throw(%GeneratorOpCode{
id: hashed_id,
name: step_id,
display_name: step_id,
op: op.op,
opts: generator_otps
})

# return value if found
val ->
val
end
end

def send_event(%{steps: steps} = ctx, step_id, events) do
op = UnhashedOp.new(ctx, "Step", step_id)
hashed_id = UnhashedOp.hash(op)
Expand Down
2 changes: 1 addition & 1 deletion test/support/cases/invoke_fn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Inngest.Test.Case.InvokeCallerFn do
_ = step.run(ctx, "step-1", fn -> %{hello: "world"} end)

res =
step.invoke(ctx, %{
step.invoke(ctx, "caller", %{
function: Inngest.Test.Case.InvokedFn,
data: %{yolo: true},
timeout: "5m"
Expand Down

0 comments on commit d4c96e9

Please sign in to comment.