Skip to content

Commit

Permalink
docs: add function documentation for ViralSpiral.Room; renamed path t…
Browse files Browse the repository at this point in the history
…o room_name
  • Loading branch information
dennyabrain committed Sep 19, 2024
1 parent b9f3fa5 commit b71aac5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
24 changes: 19 additions & 5 deletions lib/viral_spiral/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ defmodule ViralSpiral.Room do
Rooms in viral spiral need to manage mutable state of the game, hence they are wrapped in a `GenServer`. These genservers are then supervised by a Dynamic Supervisor, which is managed by this Supervisor. This supervisor is started by the application and is part of its supervision tree.
## Usage :
```elixir
Room.new("vanilla-bean-23")
engine = ViralSpiral.Room.room_gen!("ok-pista-4")
send(engine, msg)
```
"""

use Supervisor
Expand All @@ -18,6 +20,9 @@ defmodule ViralSpiral.Room do
@registry ViralSpiral.Room.Registry
@supervisor ViralSpiral.Room.GameEngineSupervisor

@doc """
Used by `Application`
"""
def start_link(_opts) do
Supervisor.start_link(__MODULE__, :ok, name: __MODULE__)
end
Expand All @@ -32,15 +37,21 @@ defmodule ViralSpiral.Room do
Supervisor.init(children, strategy: :one_for_all)
end

def new(path) when is_binary(path) do
@doc """
Registers and creates a new Room.
This ensures there is only one room registered with a path.
"""
@spec new(String.t()) :: no_return()
def new(room_name) when is_binary(room_name) do
pid =
case Registry.lookup(@registry, path) do
case Registry.lookup(@registry, room_name) do
[{pid, _}] ->
pid

[] ->
pid =
case DynamicSupervisor.start_child(@supervisor, {@room_gen, path}) do
case DynamicSupervisor.start_child(@supervisor, {@room_gen, room_name}) do
{:ok, pid} -> pid
{:error, {:already_started, pid}} -> pid
end
Expand All @@ -51,8 +62,11 @@ defmodule ViralSpiral.Room do
send(pid, :new_room)
end

def room_gen!(path) do
case Registry.lookup(@registry, path) do
@doc """
Returns the GenServer associated with a room name
"""
def room_gen!(room_name) do
case Registry.lookup(@registry, room_name) do
[{pid, _}] -> pid
_ -> raise NotFound
end
Expand Down
4 changes: 2 additions & 2 deletions lib/viral_spiral/room/game_engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ defmodule ViralSpiral.Room.GameEngine do

@registry ViralSpiral.Room.Registry

def start_link(path) do
GenServer.start_link(__MODULE__, path, name: {:via, Registry, {@registry, path}})
def start_link(room_name) do
GenServer.start_link(__MODULE__, room_name, name: {:via, Registry, {@registry, room_name}})
end

@impl true
Expand Down

0 comments on commit b71aac5

Please sign in to comment.