Skip to content

Commit

Permalink
add send_event
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin67 committed Nov 5, 2023
1 parent eafb1f9 commit 61ff0a6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
14 changes: 10 additions & 4 deletions dev/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ defmodule Inngest.Dev.EventFn2 do

IO.inspect("Second log")

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

step.send_event(ctx, "test-event-sending", %{
name: "test/foobar",
data: %{"foo" => "bar"}
})
|> IO.inspect()

Expand Down
5 changes: 4 additions & 1 deletion lib/inngest/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ end

defimpl Jason.Encoder, for: Inngest.Event do
def encode(value, opts) do
Jason.Encode.map(Map.from_struct(value), opts)
value
|> Map.from_struct()
|> Map.drop([:datetime])
|> Jason.Encode.map(opts)
end
end
29 changes: 28 additions & 1 deletion lib/inngest/step_tool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,33 @@ defmodule Inngest.StepTool do
end
end

def send_event() do
def send_event(%{steps: steps} = _ctx, step_id, events) do
op = %UnhashedOp{name: step_id, op: "Step"}
hashed_id = UnhashedOp.hash(op)

case Map.get(steps, hashed_id) do
nil ->
display_name =
cond do
is_map(events) -> Map.get(events, :name, step_id)
true -> step_id
end

# if not, execute function
result = Inngest.Client.send(events)

# cancel execution and return with opcode
throw(%GeneratorOpCode{
id: hashed_id,
name: "sendEvent",
display_name: "Send " <> display_name,
op: op.op,
data: result
})

# if found, return value
val ->
val
end
end
end

0 comments on commit 61ff0a6

Please sign in to comment.