Last active
October 25, 2017 13:49
-
-
Save Kociamber/cc10ce05a05ecf4f628e3d06e68bb5d4 to your computer and use it in GitHub Desktop.
Revisions
-
Kociamber revised this gist
Oct 25, 2017 . 1 changed file with 1 addition and 1 deletion.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 @@ -15,7 +15,7 @@ defmodule ExampleGenServer do GenServer.call(pid, {:some_call}) end # Sample client cast function. First arg is a PID, second one a tuple in format: {:some_cast, arg1, arg2,...} def set_state(pid) do GenServer.cast(pid, {:some_cast}) end -
Kociamber revised this gist
Oct 23, 2017 . 1 changed file with 1 addition 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 @@ -1,8 +1,7 @@ defmodule ExampleGenServer do use GenServer ## Client API # Starts detached process of GenServer def start_link do # Fisrt arg is a GenServer's module name, second one is its initial state and/or its type -
Kociamber revised this gist
Oct 23, 2017 . 1 changed file with 1 addition and 4 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 @@ -1,7 +1,6 @@ defmodule ExampleGenServer do ## Client API use GenServer # Starts detached process of GenServer @@ -23,15 +22,13 @@ defmodule ExampleGenServer do end ## Server API / Callbacks # Purpose of this function is to return initial state in required format: {:ok, initial_state} # If it will not store any state we can skip it def init(initial_state) do {:ok, initial_state} end # When called, all below callbacks get access to current GenServet process state and they are resetting the state while returning response # Synchronous function which handles GenServer calls and returns reply tuple: {:reply, state, state} def handle_call({:some_call}, _from, state) do # stuff -
Kociamber revised this gist
Aug 20, 2017 . No changes.There are no files selected for viewing
-
Kociamber revised this gist
Jul 20, 2017 . 1 changed file with 1 addition and 0 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 @@ -1,6 +1,7 @@ defmodule ExampleGenServer do ## Client API use GenServer # Starts detached process of GenServer -
Kociamber revised this gist
Jul 7, 2017 . 1 changed file with 1 addition and 0 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 @@ -24,6 +24,7 @@ defmodule ExampleGenServer do ## Server API / Callbacks # Purpose of this function is to return initial state in required format: {:ok, initial_state} # If it will not store any state we can skip it def init(initial_state) do {:ok, initial_state} end -
Kociamber renamed this gist
Jul 6, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Kociamber revised this gist
Jul 6, 2017 . 1 changed file with 7 additions and 6 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 @@ -5,7 +5,8 @@ defmodule ExampleGenServer do # Starts detached process of GenServer def start_link do # Fisrt arg is a GenServer's module name, second one is its initial state and/or its type # Name of a process can be specified (name: :name) but it also prevents of running multiple process of the same type GenServer.start_link(__MODULE__, []) end @@ -19,7 +20,7 @@ defmodule ExampleGenServer do def set_state(pid) do GenServer.cast(pid, {:some_cast}) end ## Server API / Callbacks # Purpose of this function is to return initial state in required format: {:ok, initial_state} @@ -31,20 +32,20 @@ defmodule ExampleGenServer do # Synchronous function which handles GenServer calls and returns reply tuple: {:reply, state, state} def handle_call({:some_call}, _from, state) do # stuff return_value = "return value" {:reply, return_value, state} end # Asynchronous function which handles GenServer casts and returns no-reply tuple: {:noreply, state} def handle_cast({:some_cast}, state) do # stuff {:noreply, state} end # Asynchronous function which handles messages from abny other places (than GenServer) and returns no-reply tuple: {:noreply, state} def handle_info({:some_info}, state) do # stuff {:noreply, state} end end -
Kociamber revised this gist
Jul 5, 2017 . 1 changed file with 1 addition and 1 deletion.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 @@ -32,7 +32,7 @@ defmodule ExampleGenServer do # Synchronous function which handles GenServer calls and returns reply tuple: {:reply, state, state} def handle_call({:some_call}, _from, state) do # do stuff return_value = "return value" {:reply, return_value, state} end -
Kociamber created this gist
Jul 5, 2017 .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,50 @@ defmodule ExampleGenServer do ## Client API use GenServer # Starts detached process of GenServer def start_link do # Fisrt arg is a GenServer name, second one is its initial state GenServer.start_link(__MODULE__, []) end # Sample client call function. First arg is a PID, other are of course optional def get_state(pid) do # second one a tuple in format: {:some_call, arg1, arg2,...} GenServer.call(pid, {:some_call}) end # Sample client cast function. First arg is a PID, second one a tuple in format: {:some_call, arg1, arg2,...} def set_state(pid) do GenServer.cast(pid, {:some_cast}) end ## Server API / Callbacks # Purpose of this function is to return initial state in required format: {:ok, initial_state} def init(initial_state) do {:ok, initial_state} end # When called, all three callbacks get access to current GenServet process state and they are resetting the state while returning response # Synchronous function which handles GenServer calls and returns reply tuple: {:reply, state, state} def handle_call({:some_call}, _from, state) do # do stuff return_value = "return value" ++ state {:reply, return_value, state} end # Asynchronous function which handles GenServer casts and returns no-reply tuple: {:noreply, state} def handle_cast({:some_cast}, state) do # do stuff {:noreply, state} end # Asynchronous function which handles messages from abny other places (than GenServer) and returns no-reply tuple: {:noreply, state} def handle_info({:some_info}, state) do # do stuff {:noreply, state} end end