defmodule GroceryCartOld do @moduledoc """ Simple example showing how to orchestrate a GenServer progressing it's own state """ use GenServer # simple contrived struct for state - didn't need to be a struct at all defstruct items: [] @doc """ Start a new instance of the Grocery Cart server """ def start_link, do: GenServer.start_link(__MODULE__, :ok, name: __MODULE__) @doc false def init(_) do # queues up a msg to itself using `send()` which will be picked up by a `handle_info/2` callback send(self(), :add_cheese) {:ok, %__MODULE__{}} end @doc false def handle_info(:add_cheese, %__MODULE__{items: items} = state) do updated_state = %__MODULE__{state | items: ["cheese" | items]} # queues up a msg to itself using `send()` which will be picked up by a `handle_info/2` callback send(self(), :add_milk) {:noreply, updated_state} end @doc false def handle_info(:add_milk, %__MODULE__{items: items} = state) do updated_state = %__MODULE__{state | items: ["milk" | items]} # queues up a msg to itself using `Process.send_after()` with a delay of 1 second - this will be picked up by a `handle_info/2` callback Process.send_after(self(), :print_and_exit, 1_000) {:noreply, updated_state} end @doc false def handle_info(:print_and_exit, %__MODULE__{items: items} = state) do IO.inspect(items, label: "Items") IO.puts("Will Exit Now") {:stop, :normal, state} end end