Skip to content

Commit

Permalink
add wait_for_event
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin67 committed Nov 5, 2023
1 parent 019aafd commit eafb1f9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
7 changes: 7 additions & 0 deletions dev/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ defmodule Inngest.Dev.EventFn2 do

IO.inspect("Second log")

step.wait_for_event(ctx, "wait-test", %{
event: "test/yolo",
timeout: "1h"
# match: "data.foo"
})
|> IO.inspect()

name =
step.run(ctx, "name", fn ->
"John Doe"
Expand Down
42 changes: 41 additions & 1 deletion lib/inngest/step_tool.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Inngest.StepTool do
@moduledoc false

alias Inngest.Event
alias Inngest.Function.{Context, UnhashedOp, GeneratorOpCode}

@type id() :: binary()
Expand Down Expand Up @@ -72,7 +73,46 @@ defmodule Inngest.StepTool do
end
end

def wait_for_event() do
@spec wait_for_event(Context.t(), id(), map()) :: map()
def wait_for_event(%{steps: steps} = _ctx, step_id, opts) do
op = %UnhashedOp{name: step_id, op: "WaitForEvent"}
hashed_id = UnhashedOp.hash(op)

if steps |> Map.has_key?(hashed_id) do
case steps |> Map.get(hashed_id) do
nil -> nil
event -> Event.from(event)
end
else
opts =
opts
|> Enum.reduce(%{}, fn
{key, value}, acc -> Map.put(acc, key, value)
keyword, acc when is_list(keyword) -> Enum.into(keyword, acc)
end)

opts =
cond do
Map.has_key?(opts, :match) ->
match = Map.get(opts, :match)
timeout = Map.get(opts, :timeout)
event = Map.get(opts, :event)
%{event: event, timeout: timeout, if: "event.#{match} == async.#{match}"}

Map.has_key?(opts, :if) ->
Map.take(opts, [:event, :timeout, :if])

true ->
Map.take(opts, [:event, :timeout])
end

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

def send_event() do
Expand Down

0 comments on commit eafb1f9

Please sign in to comment.