From 296dd1595915610fc7439fd95a66ed3ad2bc9703 Mon Sep 17 00:00:00 2001 From: Tomek Gryszkiewicz Date: Fri, 22 Jun 2018 19:52:57 +0200 Subject: [PATCH] v0.8.3 --- CHANGELOG.md | 58 +++++++++++++++++++++++++++++++++++++++++++--- lib/drab/config.ex | 2 ++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d686d5..8b7fb60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,61 @@ # CHANGELOG ## v0.8.3 -Subscribe and unsubscribe from external topics in a runtime. -Presence. -Updated behaviour of enable disable. + +This version brings two useful features: presence and ability to subscribe to topics in the runtime. + +### Upgrading from =< 0.8.2 + +Please ensure you have set `:main_phoenix_app` in your `config.exs`. The way how Drab is searching +for the Phoenix app it is working on, has been changed. + +### Subscribe and unsubscribe from external topics in a runtime + +Finally, you are not limited to the compile-time topic you’ve set with `broadcasting/1` macro in the +commander. Now you can `subscribe/2` to the external topic, receiving broadcasts sent to it. + +```elixir +subscribe(socket, same_action(MyApp.MyController, :index)) +subscribe(socket, same_topic("user_#{user_id}")) +``` + +### Presence + +Conveniences for Phoenix.Presence +If configured (it is disabled by default), tracks the user presence on the topic. The following +example shows the number of connected users, live: + +```elixir +defmodule MyAppWeb.MyCommander + use Drab.Commander + + broadcasting "global" + onconnect :connected + ondisconnect :disconnected + + def connected(socket) do + broadcast_html socket, "#number_of_users", Drab.Presence.count_users(socket) + end + + def disconnected(_store, _session) do + topic = same_topic("global") + broadcast_html topic, "#number_of_users", Drab.Presence.count_users(topic) + end +end +``` + +By default, presence map key is set as a browser UUID (which is shamelessly stored in the local +store in the browser), but it may be also any session value. This may be useful, if you have the +`user_id` already in the session, just configure it: + +```elixir +config :drab, :presence, id: :user_id +``` + +### Updated enable/disable when processing behaviour + +After launching an event from the page, the control (button) is disable until processing stops. +Now it is even better, as it recognizes previously disabled controls (#146). ## v0.8.2 diff --git a/lib/drab/config.ex b/lib/drab/config.ex index 3074677..4132e8c 100644 --- a/lib/drab/config.ex +++ b/lib/drab/config.ex @@ -87,6 +87,7 @@ defmodule Drab.Config do get(:main_phoenix_app) || find_app_in_mix_exs() end + @spec find_app_in_mix_exs :: atom | no_return defp find_app_in_mix_exs() do # try to find out the app name in config.exs, in compile time only with {:ok, pwd} <- Map.fetch(System.get_env(), "PWD"), @@ -126,6 +127,7 @@ defmodule Drab.Config do get(:endpoint) || find_endpoint_in_app_env() || find_endpoint_in_config_exs() end + @spec endpoint :: atom defp find_endpoint_in_app_env() do case app_env() do [{ep, _}] -> ep