From ce1e2f68f7d724a63588120729374f19fed677ae Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Mon, 11 Sep 2023 09:49:00 +0200 Subject: [PATCH] Load all suggestions asynchronously, unless `Mix.env == :test` --- .../live/components/combo_box.ex | 30 ++++++++------ lib/plausible_web/live/goal_settings/form.ex | 2 - lib/plausible_web/live/props_settings/form.ex | 39 ++++++++++++++----- .../live/goal_settings/form_test.exs | 11 ------ 4 files changed, 48 insertions(+), 34 deletions(-) diff --git a/lib/plausible_web/live/components/combo_box.ex b/lib/plausible_web/live/components/combo_box.ex index e4efef515..186008050 100644 --- a/lib/plausible_web/live/components/combo_box.ex +++ b/lib/plausible_web/live/components/combo_box.ex @@ -17,16 +17,24 @@ defmodule PlausibleWeb.Live.Components.ComboBox do Any function can be supplied via `suggest_fun` attribute - see the provided `ComboBox.StaticSearch`. - In case the `suggest_fun` runs an operation that could be deferred, - the `async=true` attr calls it in a background Task and updates the - suggestions asynchronously. - - Similarly, the initial `options` don't have to be provided up-front - if e.g. querying the database for suggestions at initial render is - undesirable. In such case, lack of `options` attr value combined - with `async=true` will call `suggest_fun.("", [])` asynchronously - - that special clause can be used to provide the initial set - of suggestions updated right after the initial render. + In most cases the `suggest_fun` runs an operation that could be deferred, + so by default, the `async={true}` attr calls it in a background Task + and updates the suggestions asynchronously. This way, you can render + the component without having to wait for suggestions to load. + + If you explicitly need to make the operation sychronous, you may + pass `async={false}` option. + + If your initial `options` are not provided up-front at initial render, + lack of `options` attr value combined with `async=true` calls the + `suggest_fun.("", [])` asynchronously - that special clause can be used + to provide the initial set of suggestions updated right after the initial render. + + To simplify integration testing, suggestions load up synchronously during + tests. This lets you skip waiting for suggestions messages + to arrive. The asynchronous behaviour alone is already tested in + ComboBox own test suite, so there is no need for additional + verification. """ use Phoenix.LiveComponent alias Phoenix.LiveView.JS @@ -60,7 +68,7 @@ defmodule PlausibleWeb.Live.Components.ComboBox do attr(:required, :boolean, default: false) attr(:creatable, :boolean, default: false) attr(:errors, :list, default: []) - attr(:async, :boolean, default: false) + attr(:async, :boolean, default: Mix.env() != :test) def render(assigns) do assigns = diff --git a/lib/plausible_web/live/goal_settings/form.ex b/lib/plausible_web/live/goal_settings/form.ex index 671f33b16..d3ba08706 100644 --- a/lib/plausible_web/live/goal_settings/form.ex +++ b/lib/plausible_web/live/goal_settings/form.ex @@ -86,7 +86,6 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do ]} module={ComboBox} suggest_fun={fn input, options -> suggest_page_paths(input, options, @site) end} - async={true} creatable /> @@ -185,7 +184,6 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do ComboBox.StaticSearch.suggest(input, options, weight_threshold: 0.8) end } - async={true} /> diff --git a/lib/plausible_web/live/props_settings/form.ex b/lib/plausible_web/live/props_settings/form.ex index 452cfe7f2..4b065ba5d 100644 --- a/lib/plausible_web/live/props_settings/form.ex +++ b/lib/plausible_web/live/props_settings/form.ex @@ -20,17 +20,12 @@ defmodule PlausibleWeb.Live.PropsSettings.Form do form = new_form(site) - prop_key_options = - site - |> Plausible.Props.suggest_keys_to_allow() - |> Enum.map(&{&1, &1}) - {:ok, assign(socket, form: form, - prop_key_options: prop_key_options, domain: domain, rendered_by: pid, + prop_key_options_count: 0, site: site )} end @@ -66,8 +61,24 @@ defmodule PlausibleWeb.Live.PropsSettings.Form do "py-2" ]} module={ComboBox} - options={@prop_key_options} - suggest_fun={&ComboBox.StaticSearch.suggest/2} + suggest_fun={ + pid = self() + + fn + "", [] -> + options = + @site + |> Plausible.Props.suggest_keys_to_allow() + |> Enum.map(&{&1, &1}) + + send(pid, {:update_prop_key_options_count, Enum.count(options)}) + + options + + input, options -> + ComboBox.StaticSearch.suggest(input, options) + end + } creatable /> @@ -85,12 +96,12 @@ defmodule PlausibleWeb.Live.PropsSettings.Form do @@ -98,6 +109,14 @@ defmodule PlausibleWeb.Live.PropsSettings.Form do """ end + def handle_info({:update_prop_key_options_count, count}, socket) do + {:noreply, assign(socket, prop_key_options_count: count)} + end + + def handle_info({:selection_made, %{submit_value: _prop}}, socket) do + {:noreply, socket} + end + def handle_event("allow-prop", %{"prop" => prop}, socket) do case Plausible.Props.allow(socket.assigns.site, prop) do {:ok, site} -> diff --git a/test/plausible_web/live/goal_settings/form_test.exs b/test/plausible_web/live/goal_settings/form_test.exs index 853426871..9a6d7c801 100644 --- a/test/plausible_web/live/goal_settings/form_test.exs +++ b/test/plausible_web/live/goal_settings/form_test.exs @@ -95,20 +95,13 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do test "currency combo works", %{conn: conn, site: site} do lv = get_liveview(conn, site) - # Account for asynchronous updates. Here, the options, while static, - # are still a sizeable payload that we can defer before the user manages - # to click "this is revenue goal" switch. - # There's also throttling applied to the ComboBox. - :timer.sleep(200) type_into_combo(lv, "currency_input", "Polish") - :timer.sleep(200) html = render(lv) assert element_exists?(html, ~s/a[phx-value-display-value="PLN - Polish Zloty"]/) refute element_exists?(html, ~s/a[phx-value-display-value="EUR - Euro"]/) type_into_combo(lv, "currency_input", "Euro") - :timer.sleep(200) html = render(lv) refute element_exists?(html, ~s/a[phx-value-display-value="PLN - Polish Zloty"]/) @@ -135,16 +128,12 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do type_into_combo(lv, "page_path_input", "/go/to/p") - # Account some large-ish margin for Clickhouse latency when providing suggestions asynchronously - # Might be too much, but also not enough on sluggish CI - :timer.sleep(350) html = render(lv) assert html =~ "Create "/go/to/p"" assert html =~ "/go/to/page/1" refute html =~ "/go/home" type_into_combo(lv, "page_path_input", "/go/h") - :timer.sleep(350) html = render(lv) assert html =~ "/go/home" refute html =~ "/go/to/page/1"