Last active
July 21, 2022 11:12
-
-
Save amokan/a9d63786fbed7a215030f11a8e93aab7 to your computer and use it in GitHub Desktop.
Revisions
-
amokan revised this gist
Jan 28, 2019 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()` 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 -
amokan revised this gist
Jan 28, 2019 . 2 changed files with 43 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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} -
amokan created this gist
Jan 28, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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