Skip to content

Commit 3223e3b

Browse files
committed
New Scalaz operator ~~>? which is ~~> but for optional callbacks.
Closes #58
1 parent ac490ce commit 3223e3b

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

doc/CHANGELOG-0.7.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ https://gist.github.com/japgolly/c68482dbadb0077f550c
1010
* Added a `ext-monocle` module with a few extensions for [Monocle](https://github.com/julien-truffaut/Monocle).
1111
* More supported React tags and attributes.
1212
* More ScalazReact extensions: `{state,setState,modState,modStateF}IO`.
13+
* New Scalaz operator `~~>?` which is `~~>` but for optional callbacks.
1314
* Removed deprecated methods marked for removal in 0.7.0.
1415
* Deprecated `modStateO` and `modStateU`.
1516

scalaz-7.0/src/main/scala/japgolly/scalajs/react/ScalazReact.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package japgolly.scalajs.react
22

33
import org.scalajs.dom
4-
import japgolly.scalajs.react.vdom.Attr
4+
import japgolly.scalajs.react.vdom.{TagMod, Attr, Optional, EmptyTag}
55
import japgolly.scalajs.react.vdom.Implicits._
66
import scala.scalajs.js.{UndefOr, undefined}
77
import scalaz._
@@ -20,11 +20,18 @@ object ScalazReact {
2020

2121
@inline implicit final class SzRExt_Attr(val _a: Attr) extends AnyVal {
2222

23-
def ~~>(io: => IO[Unit]) =
23+
@inline final def ~~>(io: => IO[Unit]): TagMod =
2424
_a --> io.unsafePerformIO()
2525

26-
def ~~>[N <: dom.Node, E <: SyntheticEvent[N]](eventHandler: E => IO[Unit]) =
26+
@inline final def ~~>[N <: dom.Node, E <: SyntheticEvent[N]](eventHandler: E => IO[Unit]): TagMod =
2727
_a.==>[N, E](eventHandler(_).unsafePerformIO())
28+
29+
@inline final def ~~>?[T[_]](t: => T[IO[Unit]])(implicit o: Optional[T]): TagMod =
30+
_a --> o.foreach(t)(_.unsafePerformIO()) // This implementation keeps the argument lazy
31+
//o.fold[IO[Unit], TagMod](t, ~~>(_), EmptyTag)
32+
33+
@inline final def ~~>?[T[_], N <: dom.Node, E <: SyntheticEvent[N]](eventHandler: E => T[IO[Unit]])(implicit o: Optional[T]): TagMod =
34+
_a.==>[N, E](e => o.foreach(eventHandler(e))(_.unsafePerformIO()))
2835
}
2936

3037
@inline implicit final class SzRExt_C_M(val _c: ComponentScope_M[_]) extends AnyVal {

scalaz-7.1/src/main/scala/japgolly/scalajs/react/ScalazReact.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package japgolly.scalajs.react
22

33
import org.scalajs.dom
4-
import japgolly.scalajs.react.vdom.Attr
4+
import japgolly.scalajs.react.vdom.{TagMod, Attr, Optional, EmptyTag}
55
import japgolly.scalajs.react.vdom.Implicits._
66
import scala.scalajs.js.{UndefOr, undefined}
7-
import scalaz._
7+
import scalaz.{Optional => _, _}
88
import scalaz.effect.IO
99
import Scalaz.Id
1010
import Leibniz.===
1111

1212
object ScalazReact {
1313

14-
implicit object OptionalMaybe extends vdom.Optional[Maybe] {
14+
implicit object OptionalMaybe extends Optional[Maybe] {
1515
@inline final override def foreach[A](m: Maybe[A])(f: (A) => Unit): Unit = m.cata(f, ())
1616
@inline final override def fold[A, B](t: Maybe[A], f: A => B, b: => B): B = t.cata(f, b)
1717
}
@@ -23,11 +23,18 @@ object ScalazReact {
2323

2424
@inline implicit final class SzRExt_Attr(val _a: Attr) extends AnyVal {
2525

26-
def ~~>(io: => IO[Unit]) =
26+
@inline final def ~~>(io: => IO[Unit]): TagMod =
2727
_a --> io.unsafePerformIO()
2828

29-
def ~~>[N <: dom.Node, E <: SyntheticEvent[N]](eventHandler: E => IO[Unit]) =
29+
@inline final def ~~>[N <: dom.Node, E <: SyntheticEvent[N]](eventHandler: E => IO[Unit]): TagMod =
3030
_a.==>[N, E](eventHandler(_).unsafePerformIO())
31+
32+
@inline final def ~~>?[T[_]](t: => T[IO[Unit]])(implicit o: Optional[T]): TagMod =
33+
_a --> o.foreach(t)(_.unsafePerformIO()) // This implementation keeps the argument lazy
34+
//o.fold[IO[Unit], TagMod](t, ~~>(_), EmptyTag)
35+
36+
@inline final def ~~>?[T[_], N <: dom.Node, E <: SyntheticEvent[N]](eventHandler: E => T[IO[Unit]])(implicit o: Optional[T]): TagMod =
37+
_a.==>[N, E](e => o.foreach(eventHandler(e))(_.unsafePerformIO()))
3138
}
3239

3340
@inline implicit final class SzRExt_C_M(val _c: ComponentScope_M[_]) extends AnyVal {

sync-scala70

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ s1=scalaz-7.1
66

77
f=src/main/scala/japgolly/scalajs/react/ScalazReact.scala
88
echo "$f"
9-
perl -pe 's!(?<=[^A-Z_]M\[)_!+_!' < $s1/$f > $s0/$f
9+
perl -pe '
10+
s!(?<=[^A-Z_]M\[)_!+_!;
11+
s/{Optional => _, _}/_/;
12+
' < $s1/$f > $s0/$f
1013
sed -i "
1114
/OptionalMaybe/,/^ *} *$/d;
1215
/^object/a\ // Don't edit this directly. Run $(basename "$0")

0 commit comments

Comments
 (0)