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.
Comparison of GenServer state orchestration using send/2 vs the new handle_continue callback
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment