Skip to content

Instantly share code, notes, and snippets.

@Kalimaha
Last active April 1, 2018 13:26
Show Gist options
  • Select an option

  • Save Kalimaha/9a0623355bc8935262adaae8c1d2add4 to your computer and use it in GitHub Desktop.

Select an option

Save Kalimaha/9a0623355bc8935262adaae8c1d2add4 to your computer and use it in GitHub Desktop.
GenStage playground
import Tizio
{:ok, source} = Tizio.Source.start_link(%{:spam => "eggs"})
{:ok, consumer} = Tizio.Consumer.start_link(42)
{:ok, another_consumer} = Tizio.AnotherConsumer.start_link(42)
GenStage.sync_subscribe(consumer, to: source)
GenStage.sync_subscribe(another_consumer, to: source);
import Tizio; Tizio.Source.start_link(); Tizio.DBManager.start_link(); Tizio.TwitterManager.start_link(); Tizio.Source.broadcast_update(42);
defmodule Tizio do
defmodule Source do
use GenStage
def start_link(update) do
GenStage.start_link(Tizio.Source, update)
end
def init(update) do
{ :producer, update }
end
def handle_demand(_demand, update) do
{ :noreply, [ %{ :update => update } ], update }
end
end
defmodule Consumer do
use GenStage
def start_link(_unused) do
GenStage.start_link(Tizio.Consumer, :ok)
end
def init(:ok) do
{ :consumer, :ok }
end
def handle_events(events, _from, state) do
IO.puts "WRITE TO DB: #{IO.inspect Enum.at(events, 0).update.spam}"
{ :noreply, [], state }
end
end
defmodule AnotherConsumer do
use GenStage
def start_link(_unused) do
GenStage.start_link(Tizio.AnotherConsumer, :ok)
end
def init(:ok) do
{ :consumer, :ok }
end
def handle_events(events, _from, state) do
IO.puts "SEND TO TWITTER: #{IO.inspect Enum.at(events, 0).update.spam}"
{ :noreply, [], state }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment