Skip to content

Commit 51aabcd

Browse files
committed
fix: flatten dispatch_push nesting in TradeContext
Extract dispatch_notification/3 and push_callback/3 so the push dispatch chain stays within credo's nesting depth limit. Callback fallback order (topic -> order_changed -> default) is unchanged.
1 parent 3433e43 commit 51aabcd

1 file changed

Lines changed: 21 additions & 19 deletions

File tree

lib/longbridge/trade_context.ex

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -778,25 +778,7 @@ defmodule Longbridge.TradeContext do
778778
notif = Protox.decode!(body, Longbridge.Trade.V1.Notification)
779779

780780
if notif.topic != "" and notif.data != "" do
781-
case JSON.decode(notif.data) do
782-
{:ok, event} ->
783-
event = unwrap_order_changed(event)
784-
785-
if callback = Map.get(callbacks, notif.topic) do
786-
callback.(event)
787-
else
788-
# The server echoes the subscribed topic ("private") on pushes,
789-
# while callbacks are registered under the canonical topic path.
790-
if callback = Map.get(callbacks, @order_changed_topic) do
791-
callback.(event)
792-
else
793-
if default_callback, do: default_callback.(event)
794-
end
795-
end
796-
797-
_ ->
798-
:ok
799-
end
781+
dispatch_notification(notif, callbacks, default_callback)
800782
end
801783
end
802784

@@ -811,6 +793,26 @@ defmodule Longbridge.TradeContext do
811793

812794
defp dispatch_push(_other, _callbacks, _default), do: :ok
813795

796+
defp dispatch_notification(notif, callbacks, default_callback) do
797+
case JSON.decode(notif.data) do
798+
{:ok, event} ->
799+
event = unwrap_order_changed(event)
800+
801+
if callback = push_callback(callbacks, notif.topic, default_callback) do
802+
callback.(event)
803+
end
804+
805+
_ ->
806+
:ok
807+
end
808+
end
809+
810+
# The server echoes the subscribed topic ("private") on pushes, while
811+
# callbacks are registered under the canonical topic path.
812+
defp push_callback(callbacks, topic, default_callback) do
813+
Map.get(callbacks, topic) || Map.get(callbacks, @order_changed_topic) || default_callback
814+
end
815+
814816
defp unwrap_order_changed(%{"event" => "order_changed_lb", "data" => %{} = data}), do: data
815817
defp unwrap_order_changed(event), do: event
816818
end

0 commit comments

Comments
 (0)