Skip to content
Permalink
Browse files
Merge two streams.
  • Loading branch information
steveklabnik committed Apr 23, 2013
1 parent 651cf1b commit dca32dc8d3708d2c9738c1698aadf24dd5c20595
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
@@ -5,8 +5,12 @@ module Frappuccino
class Stream
include Observable

def initialize(source)
source.extend(Frappuccino::Source).add_observer(self)
def initialize(sources)
sources = Array(sources)

sources.each do |source|
source.extend(Frappuccino::Source).add_observer(self)
end
end

def update(event)
@@ -21,6 +25,10 @@ def map(&blk)
def inject(start, &blk)
Inject.new(self, start, &blk)
end

def self.merge(stream_one, stream_two)
new([stream_one, stream_two])
end
end

private
@@ -0,0 +1,21 @@
require 'test_helper'

describe "merging steams" do
it "produces one stream with both sets of events" do
button_one = Button.new
button_two = Button.new

stream_one = Frappuccino::Stream.new(button_one)
stream_two = Frappuccino::Stream.new(button_two)

merged_stream = Frappuccino::Stream.merge(stream_one, stream_two)
counter = merged_stream
.map {|event| event == :pushed ? 1 : 0 }
.inject(0) {|sum, n| sum + n }

button_one.push
button_two.push

assert_equal 2, counter.to_i
end
end
@@ -2,12 +2,6 @@

require 'frappuccino'

class Button
def push
emit(:pushed)
end
end

describe "MVP interaction" do
it "can subscribe to an event stream" do
button = Button.new
@@ -1 +1,8 @@
require 'minitest/autorun'

class Button
def push
emit(:pushed)
end
end

0 comments on commit dca32dc

Please sign in to comment.