Skip to content

Commit

Permalink
Chore: dry up start_link vs. start functions for Socket
Browse files Browse the repository at this point in the history
  • Loading branch information
mindreframer committed Nov 9, 2023
1 parent 5ee06e1 commit 41460e0
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/surrealix/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@ defmodule Surrealix.Socket do

@spec start(Config.socket_opts()) :: WebSockex.on_start()
def start(opts \\ []) do
opts = Keyword.merge(Config.base_conn_opts(), opts)

hostname = Keyword.get(opts, :hostname)
port = Keyword.get(opts, :port)

WebSockex.start("ws://#{hostname}:#{port}/rpc", __MODULE__, SocketState.new(), opts)
generic_start(opts, :start)
end

@spec start_link(Config.socket_opts()) :: WebSockex.on_start()
def start_link(opts \\ []) do
generic_start(opts, :start_link)
end

defp generic_start(opts, fun_name) do
opts = Keyword.merge(Config.base_conn_opts(), opts)

hostname = Keyword.get(opts, :hostname)
port = Keyword.get(opts, :port)

WebSockex.start_link("ws://#{hostname}:#{port}/rpc", __MODULE__, SocketState.new(), opts)
apply(WebSockex, fun_name, [
"ws://#{hostname}:#{port}/rpc",
__MODULE__,
SocketState.new(),
opts
])
end

@spec stop(pid()) :: :ok
Expand Down

0 comments on commit 41460e0

Please sign in to comment.