From 68ccfefec146c3938c18babfb22df690f350b356 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Fri, 21 Aug 2020 23:07:10 -0400 Subject: execute room requests in a queue --- lib/polyjuice/client.ex | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib/polyjuice/client.ex') diff --git a/lib/polyjuice/client.ex b/lib/polyjuice/client.ex index 6cf1c84..39cadf0 100644 --- a/lib/polyjuice/client.ex +++ b/lib/polyjuice/client.ex @@ -177,6 +177,18 @@ defmodule Polyjuice.Client do ) :: any def call(client_api, endpoint) + @doc """ + Execute a function in a queue for a room. + + This is to make sure that, for example, messages are sent in order. + """ + @spec room_queue( + client_api :: Polyjuice.Client.API.t(), + room_id :: String.t(), + func :: function + ) :: any + def room_queue(client_api, room_id, func) + @doc """ Generate a unique transaction ID. """ @@ -244,6 +256,10 @@ defmodule Polyjuice.Client do end end + def room_queue(%{id: id}, room_id, func) when is_binary(room_id) and is_function(func) do + Mutex.under(Polyjuice.Client.Mutex, {id, room_id}, func) + end + def transaction_id(_) do "#{Node.self()}_#{:erlang.system_time(:millisecond)}_#{:erlang.unique_integer()}" end -- cgit v1.2.3