Skip to content

Commit

Permalink
Optimize checking gates
Browse files Browse the repository at this point in the history
  • Loading branch information
up2jj committed Apr 8, 2024
1 parent 56a5b8e commit 1857fbc
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions lib/fun_with_flags/flag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,26 @@ defmodule FunWithFlags.Flag do

defp check_actor_gates(gates, item) do
gates
|> actor_gates()
|> do_check_actor_gates(item)
end


defp do_check_actor_gates([], _), do: :ignore

defp do_check_actor_gates([gate|rest], item) do
defp do_check_actor_gates([%Gate{type: :actor} = gate | rest], item) do
case Gate.enabled?(gate, for: item) do
:ignore -> do_check_actor_gates(rest, item)
result -> result
result -> result
end
end

defp do_check_actor_gates([_gate | rest], item) do
do_check_actor_gates(rest, item)
end


defp check_group_gates(gates, item) do
gates
|> group_gates()
|> do_check_group_gates(item)
end

Expand All @@ -92,14 +95,18 @@ defmodule FunWithFlags.Flag do

defp do_check_group_gates([], _, result), do: result

defp do_check_group_gates([gate|rest], item, temp_result) do
defp do_check_group_gates([%Gate{type: :group} = gate|rest], item, temp_result) do
case Gate.enabled?(gate, for: item) do
:ignore -> do_check_group_gates(rest, item, temp_result)
{:ok, false} -> {:ok, false}
{:ok, true} -> do_check_group_gates(rest, item, {:ok, true})
end
end

defp do_check_group_gates([_gate | rest], item, temp_result) do
do_check_group_gates(rest, item, temp_result)
end


defp check_boolean_gate(gates) do
gate = boolean_gate(gates)
Expand Down Expand Up @@ -133,14 +140,6 @@ defmodule FunWithFlags.Flag do
Enum.find(gates, &Gate.boolean?/1)
end

defp actor_gates(gates) do
Enum.filter(gates, &Gate.actor?/1)
end

defp group_gates(gates) do
Enum.filter(gates, &Gate.group?/1)
end

defp percentage_of_time_gate(gates) do
Enum.find(gates, &Gate.percentage_of_time?/1)
end
Expand Down

0 comments on commit 1857fbc

Please sign in to comment.