From 41460e08bbf88053b405b0c0e80a13c6377ec6bc Mon Sep 17 00:00:00 2001 From: Roman Heinrich Date: Thu, 9 Nov 2023 17:26:32 +0100 Subject: [PATCH] Chore: dry up start_link vs. start functions for Socket --- lib/surrealix/socket.ex | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/surrealix/socket.ex b/lib/surrealix/socket.ex index f3d3c39..b58ddc6 100644 --- a/lib/surrealix/socket.ex +++ b/lib/surrealix/socket.ex @@ -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