Last active
April 1, 2018 13:26
-
-
Save Kalimaha/9a0623355bc8935262adaae8c1d2add4 to your computer and use it in GitHub Desktop.
GenStage playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Tizio; Tizio.Source.start_link(); Tizio.DBManager.start_link(); Tizio.TwitterManager.start_link(); Tizio.Source.broadcast_update(42); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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