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
def join_game(user_id, game_id) do | |
with {:ok, user} <- Users.get(user_id), | |
{:ok, game} <- Games.get(game_id), | |
false <- Game.is_full?(game), | |
false <- Game.is_started?(game), | |
true <- User.has_permission?(user, game) | |
do | |
Game.add_user(game, user) | |
else | |
:not_found -> {:error, "User or game not found"} |
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
def join_game(user_id, game_id) do | |
with {:user, {:ok, user}} <- {:user, Users.get(user_id)}, | |
{:game, {:ok, game}} <- {:game, Games.get(game_id)}, | |
{:full, false} <- {:full, Game.is_full?(game)}, | |
{:started, false} <- {:started, Game.is_started?(game)}, | |
{:allowed, true} <- {:allowed, User.has_permission?(user, game)} | |
do | |
Game.add_user(game, user) | |
else | |
{:user, :not_found} -> {:error, "User not found"} |
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
# I don't want to have to do this in other parts of my code, | |
# this function should clearly just return a boolean value. | |
{_, res} = Game.is_started?(game) |
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
def join_game(user_id, game_id) do | |
with {:ok, user} <- Users.get(user_id), | |
{:ok, game} <- Games.get(game_id), | |
false <- Game.is_full?(game), | |
false <- Game.is_started?(game), | |
true <- User.has_permission?(user, game) | |
do | |
Game.add_user(game, user) | |
else | |
# Don't care what specific thing failed |
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
def join_game(user_id, game_id) do | |
with {:user, {:ok, user}} <- get_user(user_id), | |
{:game, {:ok, game}} <- get_game(game_id), | |
# Etc... | |
end | |
defp get_user(id) do | |
{:user, Users.get(id)} | |
end |
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 HandleContinueBlogpost.ApplicationSlowSync do | |
use Application | |
defmodule MyServer do | |
use GenServer | |
require Logger | |
def start_link(type) do | |
GenServer.start_link(__MODULE__, type, name: type) | |
end |
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
def init(type) do | |
Logger.info("#{type} - init start") | |
state = %{ | |
type: type, | |
data: nil | |
} | |
# Send a message to ourself to load the data outside of the synchronous | |
# init block | |
self() |> send(:more_init) |
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 MyServer do | |
# ... | |
# Some sort of async operation that needs to be done on the data | |
def handle_cast({:increment, id}, state) do | |
Logger.info("#{state.type} - increment #{id}") | |
updated_data = Map.update(state.data, id, 1, fn v -> v + 1 end) | |
updated_state = Map.put(state, :data, updated_data) | |
{:noreply, updated_state} | |
end |
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
def start(_type, _args) do | |
children = [ | |
Spammer, # New process | |
Supervisor.child_spec({MyServer, :users}, id: make_ref()), | |
Supervisor.child_spec({MyServer, :messages}, id: make_ref()), | |
Supervisor.child_spec({MyServer, :items}, id: make_ref()) | |
] | |
opts = [strategy: :one_for_one, name: ContinueTest.Supervisor] | |
Supervisor.start_link(children, opts) |
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
def init(type) do | |
Logger.info("#{type} - init start") | |
state = %{ | |
type: type, | |
data: nil | |
} | |
Logger.info("#{type} - init end") | |
# Modified return value that will trigger the handle_continue callback |