]> Entropealabs - wampex_client.git/commitdiff
make sure to send connected message to processes that add themselves after we've...
authorChristopher Coté <ccote@cohesionib.com>
Fri, 4 Dec 2020 04:20:22 +0000 (22:20 -0600)
committerChristopher Coté <ccote@cohesionib.com>
Fri, 4 Dec 2020 04:20:22 +0000 (22:20 -0600)
lib/client.ex
lib/client/session.ex

index fada2f2e6179ff45f96496c45dfd06482e19eb7e..9255846cdb62991deafa5763b0f9bba222c9a413 100644 (file)
@@ -89,7 +89,13 @@ defmodule Wampex.Client do
 
   @spec add(name :: module(), pid :: module() | pid()) :: {:ok, pid()} | {:error, term()}
   def add(name, pid) do
-    Registry.register(dependent_registry_name(name), pid, pid)
+    case session_exists(name) do
+      false ->
+        %Error{error: "wamp.error.no_session"}
+
+      sess_name ->
+        Sess.add(sess_name, pid)
+    end
   end
 
   @spec remove(name :: module(), pid :: module() | pid()) :: :ok
index c5e0e9f46547651c035cb2c578199b664919ab06..1ced2269a81619918f2047859f63fb78d0d8cf68 100644 (file)
@@ -39,6 +39,7 @@ defmodule Wampex.Client.Session do
     :challenge,
     :interrupt,
     message_queue: [],
+    connected: false,
     request_id: 0,
     protocol: "wamp.2.msgpack",
     transport: WebSocket,
@@ -61,6 +62,7 @@ defmodule Wampex.Client.Session do
           challenge: Challenge.t() | nil,
           interrupt: integer() | nil,
           message_queue: [],
+          connected: boolean(),
           request_id: integer(),
           protocol: binary(),
           transport: module(),
@@ -110,6 +112,24 @@ defmodule Wampex.Client.Session do
     __MODULE__.call(name, {:send_request, request}, timeout)
   end
 
+  def add(name, pid) do
+    __MODULE__.call(name, {:add, pid})
+  end
+
+  @impl true
+  def handle_call({:add, pid}, from, _, %SL{data: %Sess{name: name, connected: conn}} = sl) do
+    case conn do
+      true ->
+        send(pid, {:connected, name})
+
+      false ->
+        :noop
+    end
+
+    r = Registry.register(Client.dependent_registry_name(name), pid, pid)
+    {:ok, sl, [{:reply, from, r}]}
+  end
+
   @impl true
   def handle_call(
         {:send_request, request},
@@ -228,7 +248,7 @@ defmodule Wampex.Client.Session do
         @handle_welcome,
         _,
         @welcome,
-        %SL{data: %Sess{name: name}} = sl
+        %SL{data: %Sess{name: name} = session} = sl
       ) do
     deps = Registry.select(Client.dependent_registry_name(name), [{{:"$1", :_, :_}, [], [:"$1"]}])
 
@@ -236,7 +256,8 @@ defmodule Wampex.Client.Session do
       send(pid, {:connected, name})
     end)
 
-    {:ok, sl, [{:next_event, :internal, :established}]}
+    {:ok, %SL{sl | data: %Sess{session | connected: true}},
+     [{:next_event, :internal, :established}]}
   end
 
   @impl true