How to stream data from callback-based API?

Today, I was searching for the answer, too, but can’t find any. Inspired by `Req.request(…, into: :self)`, I did it in an extremely unclean way. Assume the stream will be consumed in the same process creating it.

event_ref = make_ref()
parent_pid = self()

{child_pid, child_ref} =
  spawn_monitor(fn ->
    SomeModule.callback_api(fn event ->
      send(parent_pid, {:my_event, event_ref, event})
    end)
  end)

stream = Stream.unfold(:whatever, fn _ ->
  receive do
    {:DOWN, ^child_ref, :process, ^child_pid, :normal} -> nil
    {:my_event, ^event_ref, event} -> {event, :whatever}
  end
end)