Skip to content

Instantly share code, notes, and snippets.

@LukeWood
Last active May 20, 2019 02:24
Show Gist options
  • Save LukeWood/89b4357e985c1b9ebef405d53f558fb0 to your computer and use it in GitHub Desktop.
Save LukeWood/89b4357e985c1b9ebef405d53f558fb0 to your computer and use it in GitHub Desktop.
Example of decoupling entities using message passing
defmodule Player.Client do
def effect(pid, effect) do
GenServer.cast(pid, {:effect, effect})
end
end
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
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