@@ -3,6 +3,7 @@ defmodule Engine.Search.Store do
33 A persistent store for search entries
44 """
55
6+ alias Engine.Dispatch
67 alias Engine.Search.Store
78 alias Engine.Search.Store.State
89
@@ -195,19 +196,31 @@ defmodule Engine.Search.Store do
195196 end
196197
197198 def handle_call ( { :exact , subject , constraints } , _from , { ref , % State { } = state } ) do
198- { :reply , State . exact ( state , subject , constraints ) , { ref , state } }
199+ state
200+ |> State . exact ( subject , constraints )
201+ |> maybe_broadcast_loading ( state )
202+ |> then ( & { :reply , & 1 , { ref , state } } )
199203 end
200204
201205 def handle_call ( { :prefix , prefix , constraints } , _from , { ref , % State { } = state } ) do
202- { :reply , State . prefix ( state , prefix , constraints ) , { ref , state } }
206+ state
207+ |> State . prefix ( prefix , constraints )
208+ |> maybe_broadcast_loading ( state )
209+ |> then ( & { :reply , & 1 , { ref , state } } )
203210 end
204211
205212 def handle_call ( { :fuzzy , subject , constraints } , _from , { ref , % State { } = state } ) do
206- { :reply , State . fuzzy ( state , subject , constraints ) , { ref , state } }
213+ state
214+ |> State . fuzzy ( subject , constraints )
215+ |> maybe_broadcast_loading ( state )
216+ |> then ( & { :reply , & 1 , { ref , state } } )
207217 end
208218
209219 def handle_call ( { :all , constraints } , _from , { ref , % State { } = state } ) do
210- { :reply , State . all ( state , constraints ) , { ref , state } }
220+ state
221+ |> State . all ( constraints )
222+ |> maybe_broadcast_loading ( state )
223+ |> then ( & { :reply , & 1 , { ref , state } } )
211224 end
212225
213226 def handle_call ( { :update , path , entries } , _from , { ref , % State { } = state } ) do
@@ -217,13 +230,17 @@ defmodule Engine.Search.Store do
217230 end
218231
219232 def handle_call ( { :parent , entry } , _from , { _ , % State { } = state } = orig_state ) do
220- parent = State . parent ( state , entry )
221- { :reply , parent , orig_state }
233+ state
234+ |> State . parent ( entry )
235+ |> maybe_broadcast_loading ( state )
236+ |> then ( & { :reply , & 1 , orig_state } )
222237 end
223238
224239 def handle_call ( { :siblings , entry } , _from , { _ , % State { } = state } = orig_state ) do
225- siblings = State . siblings ( state , entry )
226- { :reply , siblings , orig_state }
240+ state
241+ |> State . siblings ( entry )
242+ |> maybe_broadcast_loading ( state )
243+ |> then ( & { :reply , & 1 , orig_state } )
227244 end
228245
229246 def handle_call ( :on_stop , _ , { ref , % State { } = state } ) do
@@ -300,4 +317,11 @@ defmodule Engine.Search.Store do
300317 defp enabled? do
301318 :persistent_term . get ( { __MODULE__ , :enabled? } , false )
302319 end
320+
321+ defp maybe_broadcast_loading ( { :error , :loading } = result , % State { project: project } ) do
322+ Dispatch . broadcast ( search_store_loading ( project: project ) )
323+ result
324+ end
325+
326+ defp maybe_broadcast_loading ( result , _state ) , do: result
303327end
0 commit comments