-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Custom event sources, rework AjaxEventStream, and more
- New: Signal.fromValue and Signal.fromTry - New: Observer.toJsFn1
- Loading branch information
Showing
13 changed files
with
472 additions
and
147 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
41 changes: 41 additions & 0 deletions
41
src/main/scala/com/raquo/airstream/custom/CustomSignalSource.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,41 @@ | ||
package com.raquo.airstream.custom | ||
|
||
import com.raquo.airstream.custom.CustomSource._ | ||
import com.raquo.airstream.signal.Signal | ||
|
||
import scala.util.{Success, Try} | ||
|
||
// @TODO[Test] needs testing | ||
|
||
/** Use this to easily create a custom signal from an external source | ||
* | ||
* See docs on custom sources, and [[CustomSource.Config]] | ||
*/ | ||
class CustomSignalSource[A] ( | ||
getInitialValue: => Try[A], | ||
makeConfig: (SetCurrentValue[A], GetCurrentValue[A], GetStartIndex, GetIsStarted) => CustomSource.Config, | ||
) extends Signal[A] with CustomSource[A] { | ||
|
||
override protected[this] def initialValue: Try[A] = getInitialValue | ||
|
||
override protected[this] val config: Config = makeConfig(_fireTry, tryNow, getStartIndex, getIsStarted) | ||
} | ||
|
||
object CustomSignalSource { | ||
|
||
def apply[A]( | ||
initial: => A | ||
)( | ||
config: (SetCurrentValue[A], GetCurrentValue[A], GetStartIndex, GetIsStarted) => Config | ||
): Signal[A] = { | ||
new CustomSignalSource[A](Success(initial), config) | ||
} | ||
|
||
def fromTry[A]( | ||
initial: => Try[A] | ||
)( | ||
config: (SetCurrentValue[A], GetCurrentValue[A], GetStartIndex, GetIsStarted) => Config | ||
): Signal[A] = { | ||
new CustomSignalSource[A](initial, config) | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/main/scala/com/raquo/airstream/custom/CustomSource.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,112 @@ | ||
package com.raquo.airstream.custom | ||
|
||
import com.raquo.airstream.core.{Observable, Transaction} | ||
import com.raquo.airstream.custom.CustomSource._ | ||
|
||
import scala.util.Try | ||
|
||
// @TODO[Docs] Write docs and link to that. | ||
|
||
/** Base functionality for a custom observable based on start and stop callbacks. | ||
* | ||
* See: | ||
* - [[com.raquo.airstream.custom.CustomStreamSource]] | ||
* - [[com.raquo.airstream.custom.CustomSignalSource]] | ||
*/ | ||
trait CustomSource[A] extends Observable[A] { | ||
|
||
protected[this] val config: Config | ||
|
||
// -- | ||
|
||
/** CustomSource is intended for observables that don't synchronously depend on other observables. */ | ||
override protected[airstream] val topoRank: Int = 1 | ||
|
||
protected[this] var startIndex: StartIndex = 0 | ||
|
||
|
||
protected[this] val _fireValue: FireValue[A] = { value => | ||
//println(s"> init trx from CustomSource(${value})") | ||
new Transaction(fireValue(value, _)) | ||
} | ||
|
||
protected[this] val _fireError: FireError = { error => | ||
//println(s"> init error trx from CustomSource(${error})") | ||
new Transaction(fireError(error, _)) | ||
} | ||
|
||
protected[this] val _fireTry: SetCurrentValue[A] = { value => | ||
//println(s"> init try trx from CustomSource(${value})") | ||
new Transaction(fireTry(value, _)) | ||
} | ||
|
||
protected[this] val getStartIndex: GetStartIndex = () => startIndex | ||
|
||
protected[this] val getIsStarted: GetIsStarted = () => isStarted | ||
|
||
override protected[this] def onStart(): Unit = { | ||
startIndex += 1 | ||
Try(config.onStart()).recover { | ||
case err: Throwable => _fireError(err) | ||
} | ||
} | ||
|
||
override protected[this] def onStop(): Unit = { | ||
config.onStop() | ||
} | ||
} | ||
|
||
object CustomSource { | ||
|
||
/** See docs for custom sources */ | ||
final class Config private ( | ||
val onStart: () => Unit, | ||
val onStop: () => Unit | ||
) { | ||
|
||
/** Create a version of a config that only runs start / stop if the predicate passes. | ||
* - `start` will be run when the CustomSource is about to start | ||
* if `passes` returns true at that time | ||
* - `stop` will be run when the CustomSource is about to stop | ||
* if your `start` code ran the last time CustomSource started | ||
*/ | ||
def when(passes: () => Boolean): Config = { | ||
var started = false | ||
new Config( | ||
() => { | ||
if (passes()) { | ||
started = true | ||
onStart() | ||
} | ||
}, | ||
onStop = () => { | ||
if (started) { | ||
onStop() | ||
} | ||
started = false | ||
} | ||
) | ||
} | ||
} | ||
|
||
object Config { | ||
|
||
def apply(onStart: => Unit, onStop: => Unit): Config = { | ||
new Config(() => onStart, () => onStop) | ||
} | ||
} | ||
|
||
type StartIndex = Int | ||
|
||
type FireValue[A] = A => Unit | ||
|
||
type FireError = Throwable => Unit | ||
|
||
type SetCurrentValue[A] = Try[A] => () | ||
|
||
type GetCurrentValue[A] = () => Try[A] | ||
|
||
type GetStartIndex = () => StartIndex | ||
|
||
type GetIsStarted = () => Boolean | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/scala/com/raquo/airstream/custom/CustomStreamSource.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,25 @@ | ||
package com.raquo.airstream.custom | ||
|
||
import com.raquo.airstream.custom.CustomSource._ | ||
import com.raquo.airstream.eventstream.EventStream | ||
|
||
/** Use this to easily create a custom signal from an external source | ||
* | ||
* See docs on custom sources, and [[CustomSource.Config]] | ||
*/ | ||
class CustomStreamSource[A] private ( | ||
makeConfig: (FireValue[A], FireError, GetStartIndex, GetIsStarted) => CustomSource.Config, | ||
) extends EventStream[A] with CustomSource[A] { | ||
|
||
override protected[this] val config: Config = makeConfig(_fireValue, _fireError, getStartIndex, getIsStarted) | ||
|
||
} | ||
|
||
object CustomStreamSource { | ||
|
||
def apply[A]( | ||
config: (FireValue[A], FireError, GetStartIndex, GetIsStarted) => Config | ||
): EventStream[A] = { | ||
new CustomStreamSource[A](config) | ||
} | ||
} |
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
32 changes: 0 additions & 32 deletions
32
src/main/scala/com/raquo/airstream/eventstream/SeqEventStream.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.