I have been having trouble using a cast with GenStage. I'm trying to feed data to a Producer from different parts of my code, but when I try to do a cast call, I get this error:
no function clause matching in ModuleName.handle_cast/2
The code is as follows:
def new_entry(entry) do
GenStage.cast(__MODULE__, {:new_entry, entry})
end
def handle_cast({:new_entry, entry}, {queue, [_head | tail] = list, demand} = state) do
case length(list) > 2 do
true ->
new_list = tail ++ [entry]
dispatch_events(:queue.in(get_average(new_list), queue), new_list, demand, [])
false ->
new_list = list ++ [entry]
{:noreply, [], {queue, new_list, demand}}
end
end
defp dispatch_events(queue, list, demand, events) do
with d when d > 0 <- demand,
{{:value, event}, queue} <- :queue.out(queue) do
dispatch_events(queue, list, demand - 1, [event | events])
else
_ -> {:noreply, Enum.reverse(events), {queue, list, demand}}
end
end