Skip to content

Instantly share code, notes, and snippets.

@Kociamber
Last active October 25, 2017 13:49
Show Gist options
  • Save Kociamber/cc10ce05a05ecf4f628e3d06e68bb5d4 to your computer and use it in GitHub Desktop.
Save Kociamber/cc10ce05a05ecf4f628e3d06e68bb5d4 to your computer and use it in GitHub Desktop.

Revisions

  1. Kociamber revised this gist Oct 25, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ExampleGenServer.ex
    Original 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_call, arg1, arg2,...}
    # 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
  2. Kociamber revised this gist Oct 23, 2017. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions ExampleGenServer.ex
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,7 @@
    defmodule ExampleGenServer do

    ## Client API
    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
  3. Kociamber revised this gist Oct 23, 2017. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions ExampleGenServer.ex
    Original 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 three callbacks get access to current GenServet process state and they are resetting the state while returning response

    # 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
  4. Kociamber revised this gist Aug 20, 2017. No changes.
  5. Kociamber revised this gist Jul 20, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ExampleGenServer.ex
    Original 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
  6. Kociamber revised this gist Jul 7, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ExampleGenServer.ex
    Original 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
  7. Kociamber renamed this gist Jul 6, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. Kociamber revised this gist Jul 6, 2017. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions examplegenserver.ex
    Original 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 name, second one is its initial state
    # 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
    # do stuff
    # stuff
    return_value = "return value"
    {:reply, 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
    # 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
    # stuff
    {:noreply, state}
    end
    end
  9. Kociamber revised this gist Jul 5, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion examplegenserver.ex
    Original 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" ++ state
    return_value = "return value"
    {:reply, return_value, state}
    end

  10. Kociamber created this gist Jul 5, 2017.
    50 changes: 50 additions & 0 deletions examplegenserver.ex
    Original 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