-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arman's implementation from #114, rebased on master, moved into a separate package, etc.
- Loading branch information
Showing
5 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
34 changes: 34 additions & 0 deletions
34
src/main/scala/com/raquo/airstream/javaflow/FlowPublisherStream.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.raquo.airstream.javaflow | ||
|
||
import com.raquo.airstream.core.EventStream | ||
|
||
import java.util.concurrent.Flow | ||
|
||
object FlowPublisherStream { | ||
|
||
def apply[A](publisher: Flow.Publisher[A], emitOnce: Boolean = false): EventStream[A] = { | ||
var subscription: Flow.Subscription = null | ||
|
||
EventStream.fromCustomSource[A]( | ||
shouldStart = startIndex => if (emitOnce) startIndex == 1 else true, | ||
start = (fireEvent, fireError, _, _) => { | ||
val subscriber = new Flow.Subscriber[A] { | ||
def onNext(value: A): Unit = fireEvent(value) | ||
def onError(err: Throwable): Unit = fireError(err) | ||
def onComplete(): Unit = () | ||
def onSubscribe(sub: Flow.Subscription): Unit = { | ||
sub.request(Long.MaxValue) // unlimited demand for events | ||
subscription = sub | ||
} | ||
} | ||
publisher.subscribe(subscriber) | ||
}, | ||
stop = _ => { | ||
if (subscription ne null) { | ||
subscription.cancel() | ||
subscription = null | ||
} | ||
} | ||
) | ||
} | ||
} |
This file contains 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
43 changes: 43 additions & 0 deletions
43
src/test/scala/com/raquo/airstream/javaflow/FlowPublisherStreamSpec.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.raquo.airstream.javaflow | ||
|
||
import com.raquo.airstream.UnitSpec | ||
import com.raquo.airstream.core.EventStream | ||
import com.raquo.airstream.fixtures.{Effect, TestableOwner} | ||
import com.raquo.airstream.ownership.Owner | ||
|
||
import java.util.concurrent.Flow | ||
import scala.collection.mutable | ||
|
||
class FlowPublisherStreamSpec extends UnitSpec { | ||
|
||
class RangePublisher(range: Range) extends Flow.Publisher[Int] { | ||
def subscribe(subscriber: Flow.Subscriber[_ >: Int]): Unit = { | ||
val subscription = new Flow.Subscription { | ||
def request(n: Long): Unit = range.foreach(subscriber.onNext(_)) | ||
def cancel(): Unit = () | ||
} | ||
subscriber.onSubscribe(subscription) | ||
} | ||
} | ||
|
||
it("EventStream.fromPublisher") { | ||
|
||
implicit val owner: Owner = new TestableOwner | ||
|
||
val range = 1 to 3 | ||
val stream = EventStream.fromPublisher(new RangePublisher(range)) | ||
|
||
val effects = mutable.Buffer[Effect[_]]() | ||
val sub1 = stream.foreach(newValue => effects += Effect("obs1", newValue)) | ||
|
||
effects.toList shouldBe range.map(i => Effect("obs1", i)) | ||
effects.clear() | ||
|
||
sub1.kill() | ||
|
||
val sub2 = stream.foreach(newValue => effects += Effect("obs2", newValue)) | ||
|
||
effects.toList shouldBe range.map(i => Effect("obs2", i)) | ||
effects.clear() | ||
} | ||
} |