Created
December 23, 2018 00:13
-
-
Save blackode/2d429596a3d56f0b856ef53f7dba901a to your computer and use it in GitHub Desktop.
Demo GenServer
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 characters
defmodule DemoServer do | |
use GenServer | |
## Server API | |
def init(employee) do # initiating the state with the value 1 passed | |
{:ok, employee} | |
end | |
# add the value to the state and returns :ok | |
def handle_call({:add,value},_from,state) do | |
{:reply, "#{value} added",%{state + value} | |
end | |
# returns the state to the caller | |
def handle_call(:get,_from,state) do | |
{:reply,state,state} | |
end | |
# This executes periodically | |
def handle_info(:work,state) do | |
IO.puts " This message prints every after 2 seconds" | |
schedule_work() | |
{:noreply,state} | |
end | |
#catch-all clause for the handle_info for handling unkown messages | |
def handle_info(_msg, state) do | |
IO.puts "unknown message" | |
{:noreply, state} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment