-
-
Save samdvr/a063000f128bef5eb723dc1dfd9153a8 to your computer and use it in GitHub Desktop.
GenServer
This file contains 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 BankAccount do | |
# Define default implementations for the 6 GenServer | |
# callbacks with `use GenServer` | |
use GenServer | |
# Wrap calls to GenServer in public functions | |
def start(balance) do | |
{:ok, account} = GenServer.start(__MODULE__, balance) | |
account | |
end | |
def deposit(account, amount) do | |
GenServer.cast(account, {:deposit, amount}) | |
end | |
def withdraw(account, amount) do | |
GenServer.cast(account, {:withdraw, amount}) | |
end | |
def balance(account) do | |
GenServer.call(account, :balance) | |
end | |
### | |
# GenServer API | |
### | |
# Handle cast messages with `handle_cast`. No response is expected, | |
# so reply with the {:noreply, ...} tuple format | |
def handle_cast({:deposit, amount}, balance) do | |
{:noreply, balance + amount} | |
end | |
def handle_cast({:withdraw, amount}, balance) do | |
{:noreply, balance - amount} | |
end | |
# Handle call messages with the `handle_call` callback. Responses are usually | |
# expected, so respond with the {:reply, ...} tuple format. | |
def handle_call(:balance, _from, balance) do | |
{:reply, balance, balance} | |
end | |
end | |
account = BankAccount.start(0) | |
BankAccount.deposit(account, 50) | |
BankAccount.withdraw(account, 25) | |
BankAccount.balance(account) # => 25 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment