-
-
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:
.splitByIndex
and .splitOption
for vars
- Loading branch information
1 parent
b0fa6a1
commit 5da5634
Showing
5 changed files
with
349 additions
and
1 deletion.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/scala/com/raquo/airstream/extensions/OptionVar.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,59 @@ | ||
package com.raquo.airstream.extensions | ||
|
||
import com.raquo.airstream.core.Signal | ||
import com.raquo.airstream.state.{LazyDerivedVar, LazyStrictSignal, Var} | ||
import com.raquo.airstream.split.DuplicateKeysConfig | ||
|
||
class OptionVar[A](val v: Var[Option[A]]) extends AnyVal { | ||
|
||
/** This `.split`-s a Var of an Option by the Option's `isDefined` property. | ||
* If you want a different key, use the .split operator directly. | ||
* | ||
* @param project - (initialInput, varOfInput) => output | ||
* `project` is called whenever the parent var switches from `None` to `Some()`. | ||
* `varOfInput` starts with `initialInput` value, and updates when | ||
* the parent var updates from `Some(a)` to `Some(b)`. | ||
* @param ifEmpty - returned if Option is empty. Evaluated whenever the parent var | ||
* switches from `Some(a)` to `None`, or when the parent var | ||
* starts with a `None`. `ifEmpty` is NOT re-evaluated when the | ||
* parent var emits `None` if its value is already `None`. | ||
*/ | ||
def splitOption[B]( | ||
project: (A, Var[A]) => B, | ||
ifEmpty: => B | ||
): Signal[B] = { | ||
// Note: We never have duplicate keys here, so we can use | ||
// DuplicateKeysConfig.noWarnings to improve performance | ||
v.signal | ||
.distinctByFn((prev, next) => prev.isEmpty && next.isEmpty) // Ignore consecutive `None` events | ||
.split( | ||
key = _ => (), | ||
duplicateKeys = DuplicateKeysConfig.noWarnings | ||
)( | ||
(_, initial, signal) => { | ||
val displayNameSuffix = s".splitOption(Some)" | ||
val childVar = new LazyDerivedVar[Option[A], A]( | ||
parent = v, | ||
signal = new LazyStrictSignal[A, A]( | ||
signal, identity, signal.displayName, displayNameSuffix + ".signal" | ||
), | ||
zoomOut = (inputs, newInput) => { | ||
Some(newInput) | ||
}, | ||
displayNameSuffix = displayNameSuffix | ||
) | ||
project(initial, childVar) | ||
} | ||
) | ||
.map(_.getOrElse(ifEmpty)) | ||
} | ||
|
||
def splitOption[B]( | ||
project: (A, Var[A]) => B | ||
): Signal[Option[B]] = { | ||
splitOption( | ||
(initial, someVar) => Some(project(initial, someVar)), | ||
ifEmpty = None | ||
) | ||
} | ||
} |
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