Created
April 14, 2023 00:50
-
-
Save rayrayzayzay/7b77055154050d3472c8c2649aaf7335 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule ChannelHandler do | |
def process_plugs(plugs, socket, payload, bindings \\ %{}) do | |
result = | |
Enum.reduce_while(plugs, {socket, payload, bindings}, | |
fn plug, {socket, payload, bindings} -> | |
case process_plug(plug, socket, payload, bindings) do | |
{:cont, socket, payload, bindings} -> {:cont, {:cont, socket, payload, bindings}} | |
{:halt, socket, status, reason} -> {:halt, {:halt, socket, status, reason}} | |
end | |
end) | |
case result do | |
{:cont, socket, payload, bindings} -> | |
{:cont, socket, payload, bindings} | |
{:halt, status, reason, socket} -> | |
{:reply, {status, reason}, socket} | |
end | |
end | |
defp process_plug(plug, socket, payload, bindings) when is_function(plug) do | |
process_plug({plug, []}, socket, payload, bindings) | |
end | |
defp process_plug({plug, opts}, socket, payload, bindings) do | |
plug.(socket, payload, bindings, opts) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment