Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add replay and multicast variants to RxScala #1160

Merged
merged 8 commits into from
May 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,54 @@ class RxScalaDemo extends JUnitSuite {

@Test def exampleWithReplay() {
val numbers = Observable.interval(1000 millis).take(6)
val (startFunc, sharedNumbers) = numbers.replay
val sharedNumbers = numbers.replay
sharedNumbers.subscribe(n => println(s"subscriber 1 gets $n"))
startFunc()
sharedNumbers.connect
// subscriber 2 subscribes later but still gets all numbers
doLater(3500 millis, () => { sharedNumbers.subscribe(n => println(s"subscriber 2 gets $n")) })
waitFor(sharedNumbers)
}

@Test def exampleWithReplay2() {
val numbers = Observable.interval(100 millis).take(10)
val sharedNumbers = numbers.replay(3)
sharedNumbers.subscribe(n => println(s"subscriber 1 gets $n"))
sharedNumbers.connect
// subscriber 2 subscribes later but only gets the 3 buffered numbers and the following numbers
Thread.sleep(700)
sharedNumbers.subscribe(n => println(s"subscriber 2 gets $n"))
waitFor(sharedNumbers)
}

@Test def exampleWithReplay3() {
val numbers = Observable.interval(100 millis).take(10)
val sharedNumbers = numbers.replay(300 millis)
sharedNumbers.subscribe(n => println(s"subscriber 1 gets $n"))
sharedNumbers.connect
// subscriber 2 subscribes later but only gets the buffered numbers and the following numbers
Thread.sleep(700)
sharedNumbers.subscribe(n => println(s"subscriber 2 gets $n"))
waitFor(sharedNumbers)
}

@Test def exampleWithReplay4() {
val numbers = Observable.interval(100 millis).take(10)
val sharedNumbers = numbers.replay(2, 300 millis)
sharedNumbers.subscribe(n => println(s"subscriber 1 gets $n"))
sharedNumbers.connect
// subscriber 2 subscribes later but only gets the buffered numbers and the following numbers
Thread.sleep(700)
sharedNumbers.subscribe(n => println(s"subscriber 2 gets $n"))
waitFor(sharedNumbers)
}

@Test def exampleWithReplay5() {
val numbers = Observable.interval(100 millis).take(10)
val sharedNumbers = numbers.replay[Long, Long]((o: Observable[Long]) => o.map(_ * 2))
sharedNumbers.subscribe(n => println(s"subscriber gets $n"))
waitFor(sharedNumbers)
}

@Test def testSingleOption() {
assertEquals(None, List(1, 2).toObservable.toBlockingObservable.singleOption)
assertEquals(Some(1), List(1).toObservable.toBlockingObservable.singleOption)
Expand Down Expand Up @@ -720,7 +760,7 @@ class RxScalaDemo extends JUnitSuite {
}

@Test def repeatExample1(): Unit = {
val o : Observable[String] = List("alice", "bob", "carol").toObservable.repeat().take(6)
val o : Observable[String] = List("alice", "bob", "carol").toObservable.repeat.take(6)
assertEquals(List("alice", "bob", "carol", "alice", "bob", "carol"), o.toBlockingObservable.toList)
}

Expand Down Expand Up @@ -802,6 +842,21 @@ class RxScalaDemo extends JUnitSuite {
}
}

@Test def multicastExample1(): Unit = {
val unshared = Observable.from(1 to 4)
val shared = unshared.multicast(Subject())
shared.subscribe(n => println(s"subscriber 1 gets $n"))
shared.subscribe(n => println(s"subscriber 2 gets $n"))
shared.connect
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example does not really enlighten users on what multicast is good for, because they just see the same output as if they hadn't used it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this example from exampleWithPublish since publish = multicast(Subject()).

  @Test def exampleWithPublish() {
    val unshared = Observable.from(1 to 4)
    val shared = unshared.publish
    shared.subscribe(n => println(s"subscriber 1 gets $n"))
    shared.subscribe(n => println(s"subscriber 2 gets $n"))
    shared.connect
  }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I got it, we can l leave it, because the output is different


@Test def multicastExample2(): Unit = {
val unshared = Observable.from(1 to 4)
val shared = unshared.multicast[Int, String](() => Subject(), o => o.map("No. " + _))
shared.subscribe(n => println(s"subscriber 1 gets $n"))
shared.subscribe(n => println(s"subscriber 2 gets $n"))
}

@Test def startWithExample(): Unit = {
val o1 = List(3, 4).toObservable
val o2 = 1 +: 2 +: o1
Expand Down
Loading