do receive do {:hello, message} -> IO.puts "Hello, #{message}" # Will accept only once without this: accepting_messages(state) {:counter} -> new_state = state + 1 IO.puts "New state is #{new_state}" accepting_messages(new_state) _ -> IO.puts "What?" accepting_messages(state) end end end
Agent process with an empty Map. """ def start_link do Agent.start_link(fn -> %{} end, name: __MODULE__) # this process is a singleton end def get(key) do Agent.get(__MODULE__, &Map.get(&1, key)) end def put(key, value) do Agent.update(__MODULE__, fn (state) -> Map.put(state, key, value) # happens on server end) end end
name: __MODULE__) end def init(state) do {:producer_consumer, state, subscribe_to: [ FpconfElixir.GenStageType.Producer]} end def handle_events(events, _from, state) do numbers = events |> Enum.filter(&(rem(&1, 2) == 0)) {:noreply, numbers, state} end end
end def init(state) do {:consumer, state, subscribe_to: [ FpconfElixir.GenStageType.ProducerConsumer]} end def handle_events(events, _from, state) do for event <- events do IO.inspect({self(), event, state}) end # As a consumer we never emit events {:noreply, [], state} end end