Skip to content

Commit

Permalink
Move checking if removed child exists to the private function
Browse files Browse the repository at this point in the history
  • Loading branch information
FelonEkonom committed May 12, 2023
1 parent 0b541e4 commit 2137940
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions lib/membrane/core/parent/child_life_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -500,24 +500,7 @@ defmodule Membrane.Core.Parent.ChildLifeController do
def handle_remove_children(children_or_children_groups, state) do
children_or_children_groups = Bunch.listify(children_or_children_groups)

all_children_and_children_groups =
state.children
|> Enum.flat_map(fn {ref, %{group: group}} -> [ref, group] end)
|> Enum.uniq()
|> List.delete(nil)

children_or_children_groups
|> Enum.find(&(&1 not in all_children_and_children_groups))
|> case do
nil ->
:ok

child_ref ->
raise Membrane.ParentError, """
Trying to remove child #{inspect(child_ref)}, while such a child or children group does not exist.
Existing children and children groups are: #{inspect(all_children_and_children_groups, pretty: true)}
"""
end
:ok = ensure_removed_children_exist!(children_or_children_groups, state)

refs =
state.children
Expand Down Expand Up @@ -553,6 +536,27 @@ defmodule Membrane.Core.Parent.ChildLifeController do
Parent.ChildrenModel.update_children!(state, refs, &%{&1 | terminating?: true})
end

defp ensure_removed_children_exist!(removed_children_or_groups, state) do
children_groups =
MapSet.new(state.children, fn {_ref, data} -> data.group end)
|> MapSet.delete(nil)

Enum.find(removed_children_or_groups, fn name ->
not Map.has_key?(state.children, name) and not MapSet.member?(children_groups, name)
end)
|> case do
nil ->
:ok

child_ref ->
raise Membrane.ParentError, """
Trying to remove child #{inspect(child_ref)}, while such a child or children group does not exist.
Existing children are: #{Map.keys(state.children) |> inspect(pretty: true)}
Existing children groups are: #{MapSet.to_list(children_groups) |> inspect(pretty: true)}
"""
end
end

@spec handle_remove_link(Child.name(), Pad.ref(), Parent.state()) ::
Parent.state()
def handle_remove_link(child_name, pad_ref, state) do
Expand Down

0 comments on commit 2137940

Please sign in to comment.