Last active
May 20, 2019 02:24
-
-
Save LukeWood/89b4357e985c1b9ebef405d53f558fb0 to your computer and use it in GitHub Desktop.
Example of decoupling entities using message passing
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 Player.Client do | |
def effect(pid, effect) do | |
GenServer.cast(pid, {:effect, effect}) | |
end | |
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 Player.Impl do | |
def effect(state, effect) do | |
effect.(state) |> | |
update() # This is an internal function that tells web clients about this update. | |
end | |
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 Player.Server do | |
use GenServer | |
def handle_cast({:effect, effect}, state) do | |
{:noreply, Impl.effect(state, effect)} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment