View Source Strom.Source behaviour (strom v0.8.6)

Produces stream of events.

## Example with Enumerable
iex> alias Strom.Source
iex> source = :numbers |> Source.new([1, 2, 3]) |> Source.start()
iex> %{numbers: stream} = Source.call(%{}, source)
iex> Enum.to_list(stream)
[1, 2, 3]

## Example with file
iex> alias Strom.{Source, Source.ReadLines}
iex> source = :numbers |> Source.new(ReadLines.new("test/data/numbers1.txt")) |> Source.start()
iex> %{numbers: stream} = Source.call(%{}, source)
iex> Enum.to_list(stream)
["1", "2", "3", "4", "5"]

## If two sources are applied to one stream, the streams will be concatenated (Stream.concat/2)
iex> alias Strom.{Source, Source.ReadLines}
iex> source1 = :numbers |> Source.new([1, 2, 3]) |> Source.start()
iex> source2 = :numbers |> Source.new(ReadLines.new("test/data/numbers1.txt")) |> Source.start()
iex> %{numbers: stream} = %{} |> Source.call(source1) |> Source.call(source2)
iex> Enum.to_list(stream)
[1, 2, 3, "1", "2", "3", "4", "5"]

Source defines a @behaviour. One can easily implement their own sources. See Strom.Source.ReadLines, Strom.Source.Events, Strom.Source.IOGets

Summary

Types

@type event() :: any()
@type t() :: %Strom.Source{
  buffer: term(),
  chunk: term(),
  data: term(),
  name: term(),
  opts: term(),
  origin: term(),
  pid: term(),
  task: term(),
  waiting_client: term()
}

Callbacks

@callback call(map()) :: {:ok, {[term()], map()}} | {:error, {:halt, map()}}
@callback infinite?(map()) :: true | false
@callback start(map()) :: map()
@callback stop(map()) :: map()

Functions

@spec call(t()) :: event()
@spec call(Strom.flow(), t()) :: Strom.flow()
@spec call(Strom.flow(), t()) :: Strom.flow()

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

new(name, origin, opts \\ [])

View Source
@spec new(Strom.stream_name(), struct() | [event()] | Strom.stream(), list()) :: t()
@spec stop(t()) :: :ok