From 117b8df893880705e8d76f3d2bc74c4641f4dd59 Mon Sep 17 00:00:00 2001 From: Maarten van Vliet Date: Mon, 18 Mar 2024 08:53:58 +0100 Subject: [PATCH] Add init_producer/4 --- lib/gen_stage.ex | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/gen_stage.ex b/lib/gen_stage.ex index 9a031c6..f629375 100644 --- a/lib/gen_stage.ex +++ b/lib/gen_stage.ex @@ -1867,10 +1867,10 @@ defmodule GenStage do def init({mod, args}) do case mod.init(args) do {:producer, state} -> - init_producer(mod, [], state, nil) + init_producer(mod, [], state) {:producer, state, opts} when is_list(opts) -> - init_producer(mod, opts, state, nil) + init_producer(mod, opts, state) {:producer, state, opts, additional_info} when is_list(opts) -> init_producer(mod, opts, state, additional_info) @@ -1904,7 +1904,7 @@ defmodule GenStage do end end - defp init_producer(mod, opts, state, additional_info) do + defp init_producer(mod, opts, state) do with {:ok, dispatcher_mod, dispatcher_state, opts} <- init_dispatcher(opts), {:ok, buffer_size, opts} <- Utils.validate_integer(opts, :buffer_size, 10000, 0, :infinity, true), @@ -1923,13 +1923,18 @@ defmodule GenStage do dispatcher_mod: dispatcher_mod, dispatcher_state: dispatcher_state } - - if additional_info, do: {:ok, stage, additional_info}, else: {:ok, stage} + {:ok, stage} else {:error, message} -> {:stop, {:bad_opts, message}} end end + defp init_producer(mod, opts, state, additional_info) do + with {:ok, stage} <- init_producer(mod, opts, state) do + {:ok, stage, additional_info} + end + end + defp init_dispatcher(opts) do case Keyword.pop(opts, :dispatcher, GenStage.DemandDispatcher) do {dispatcher, opts} when is_atom(dispatcher) ->