-
-
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.
API: New collectSome / collectOpt operators; rename contramapOpt -> c…
…ontracollectOpt
- Loading branch information
Showing
7 changed files
with
182 additions
and
36 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
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
31 changes: 31 additions & 0 deletions
31
src/main/scala/com/raquo/airstream/misc/CollectEventStream.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,31 @@ | ||
package com.raquo.airstream.misc | ||
|
||
import com.raquo.airstream.common.{InternalNextErrorObserver, SingleParentObservable} | ||
import com.raquo.airstream.core.{EventStream, Protected, Transaction, WritableEventStream} | ||
|
||
import scala.util.Try | ||
|
||
/** This stream applies `fn` to the parent stream's events, and emits `x` from the resulting `Some(x)` value (if `None`, nothing is fired). | ||
* | ||
* This stream emits an error if the parent stream emits an error (Note: no filtering applied), or if `fn` throws | ||
* | ||
* @param fn Note: guarded against exceptions | ||
*/ | ||
class CollectEventStream[A, B]( | ||
override protected val parent: EventStream[A], | ||
fn: A => Option[B], | ||
) extends WritableEventStream[B] with SingleParentObservable[A, B] with InternalNextErrorObserver[A] { | ||
|
||
override protected val topoRank: Int = Protected.topoRank(parent) + 1 | ||
|
||
override protected def onNext(nextParentValue: A, transaction: Transaction): Unit = { | ||
Try(fn(nextParentValue)).fold( | ||
onError(_, transaction), | ||
nextValue => nextValue.foreach(fireValue(_, transaction)) | ||
) | ||
} | ||
|
||
override protected def onError(nextError: Throwable, transaction: Transaction): Unit = { | ||
fireError(nextError, transaction) | ||
} | ||
} |
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