Skip to content

Instantly share code, notes, and snippets.

@amokan
Last active July 21, 2022 11:12
Show Gist options
  • Save amokan/a9d63786fbed7a215030f11a8e93aab7 to your computer and use it in GitHub Desktop.
Save amokan/a9d63786fbed7a215030f11a8e93aab7 to your computer and use it in GitHub Desktop.

Revisions

  1. amokan revised this gist Jan 28, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions grocery_cart_old.ex
    Original file line number Diff line number Diff line change
    @@ -25,14 +25,14 @@ defmodule GroceryCartOld do
    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()` with a message format that will be picked up by a `handle_cast/2` callback
    send(self(), {:"$gen_cast", :add_milk})
    # 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_cast(:add_milk, %__MODULE__{items: items} = state) do
    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
  2. amokan revised this gist Jan 28, 2019. 2 changed files with 43 additions and 2 deletions.
    42 changes: 42 additions & 0 deletions grocery_cart_new.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    defmodule GroceryCart do
    @moduledoc """
    Simple example showing the `handle_continue` callback in Erlang/OTP 21+
    """

    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
    {:ok, %__MODULE__{}, {:continue, :add_cheese}}
    end

    @doc false
    def handle_continue(:add_cheese, %__MODULE__{items: items} = state) do
    updated_state = %__MODULE__{state | items: ["cheese" | items]}

    {:noreply, updated_state, {:continue, :add_milk}}
    end

    @doc false
    def handle_continue(:add_milk, %__MODULE__{items: items} = state) do
    updated_state = %__MODULE__{state | items: ["milk" | items]}

    {:noreply, updated_state, {:continue, :print_and_exit}}
    end

    @doc false
    def handle_continue(:print_and_exit, %__MODULE__{items: items} = state) do
    IO.inspect(items, label: "Items")
    IO.puts("Will Exit Now")

    {:stop, :normal, state}
    end
    end
    3 changes: 1 addition & 2 deletions grocery_cart_old.ex
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ defmodule GroceryCartOld do

    use GenServer


    # simple contrived struct for state - didn't need to be a struct at all
    defstruct items: []

    @doc """
    @@ -44,7 +44,6 @@ defmodule GroceryCartOld do
    @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}
  3. amokan created this gist Jan 28, 2019.
    52 changes: 52 additions & 0 deletions grocery_cart_old.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    defmodule GroceryCartOld do
    @moduledoc """
    Simple example showing how to orchestrate a GenServer progressing it's own state
    """

    use GenServer


    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()` with a message format that will be picked up by a `handle_cast/2` callback
    send(self(), {:"$gen_cast", :add_milk})

    {:noreply, updated_state}
    end

    @doc false
    def handle_cast(: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