Skip to content

Commit

Permalink
Refactor composite
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmi committed Jun 9, 2024
1 parent e7adec8 commit 1f224db
Showing 1 changed file with 8 additions and 61 deletions.
69 changes: 8 additions & 61 deletions lib/composite.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,9 @@ defmodule Strom.Composite do
end

def build(components) do
components
|> Enum.map(fn component ->
case component do
%Strom.Source{} = source ->
Strom.Source.start(source)

%Strom.Sink{} = sink ->
Strom.Sink.start(sink)

%Strom.Mixer{} = mixer ->
Strom.Mixer.start(mixer)

%Strom.Splitter{} = splitter ->
Strom.Splitter.start(splitter)

%Strom.Transformer{} = transformer ->
Strom.Transformer.start(transformer)

%Strom.Renamer{} = renamer ->
Strom.Renamer.start(renamer)
end
Enum.map(components, fn %{__struct__: module} = component ->
component
|> module.start()
|> tap(&monitor_component/1)
end)
end
Expand Down Expand Up @@ -130,51 +112,16 @@ defmodule Strom.Composite do
end

def reduce_flow(components, init_flow) do
Enum.reduce(components, init_flow, fn component, flow ->
case component do
%Strom.Source{} = source ->
Strom.Source.call(flow, source)

%Strom.Sink{} = sink ->
Strom.Sink.call(flow, sink)

%Strom.Mixer{} = mixer ->
Strom.Mixer.call(flow, mixer)

%Strom.Splitter{} = splitter ->
Strom.Splitter.call(flow, splitter)

%Strom.Transformer{} = transformer ->
Strom.Transformer.call(flow, transformer)

%Strom.Renamer{} = renamer ->
Strom.Renamer.call(flow, renamer)
end
Enum.reduce(components, init_flow, fn %{__struct__: module} = component, flow ->
flow
|> module.call(component)
|> tap(fn _flow -> collect_garbage(component) end)
end)
end

def stop_components(components) do
Enum.each(components, fn component ->
case component do
%Strom.Source{} = source ->
Strom.Source.stop(source)

%Strom.Sink{} = sink ->
Strom.Sink.stop(sink)

%Strom.Mixer{} = mixer ->
Strom.Mixer.stop(mixer)

%Strom.Splitter{} = splitter ->
Strom.Splitter.stop(splitter)

%Strom.Transformer{} = transformer ->
Strom.Transformer.stop(transformer)

%Strom.Renamer{} = renamer ->
Strom.Renamer.stop(renamer)
end
Enum.each(components, fn %{__struct__: module} = component ->
module.stop(component)
end)
end

Expand Down

0 comments on commit 1f224db

Please sign in to comment.