Skip to content

Commit e1e059d

Browse files
committed
More Scalaz and Monocle methods
1 parent fe70c0a commit e1e059d

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed

TODO

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
parentSel = id => c.modStateIO(State._detailRowSelParent set id),

doc/CHANGELOG-0.7.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ https://gist.github.com/japgolly/c68482dbadb0077f550c
99
* Moved `.experiment` into a new module called `extra`.
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.
12-
* More ScalazReact extensions: `{state,setState,modState,modStateF}IO`.
13-
* New Scalaz operator `~~>?` which is `~~>` but for optional callbacks.
12+
* More ScalazReact extensions:
13+
* `stateIO`
14+
* `setStateIO`
15+
* `modStateIO`
16+
* `modStateIOF`
17+
* `_setStateIO`
18+
* `_modStateIO`
19+
* `_modStateIOF`
20+
* Attr operator `~~>?` which is `~~>` but for optional callbacks.
1421
* Removed deprecated methods marked for removal in 0.7.0.
1522
* Deprecated `modStateO` and `modStateU`.
1623

monocle/src/main/scala/japgolly/scalajs/react/MonocleReact.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package japgolly.scalajs.react
22

33
import monocle._
44
import scalaz.Functor
5+
import scalaz.effect.IO
56
import japgolly.scalajs.react.ScalazReact._
67

78
object MonocleReact {
@@ -14,6 +15,9 @@ object MonocleReact {
1415
@inline def focusStateL[T](l: Lens[S, T])(implicit C: CC) = new ComponentStateFocus[T](
1516
() => l get _c.state,
1617
(t: T, cb: OpCallback) => _c.modState(l set t, cb))
18+
19+
@inline def _setStateL[L[_, _, _, _], B](l: L[S, S, _, B])(implicit C: CC, L: SetterMonocle[L]): B => IO[Unit] =
20+
_c._modStateIO(L set l)
1721
}
1822

1923
@inline implicit final class MonRExt_ReactSTOps[M[_], S, A](val _r: ReactST[M,S,A]) extends AnyVal {
@@ -25,4 +29,32 @@ object MonocleReact {
2529
// Seriously, Scala, get your shit together.
2630
@inline final implicit def moarScalaHandHoldingMon[P,S](b: BackendScope[P,S]): MonRExt_CompStateAccessOps[ComponentScope_SS, S] = (b: ComponentScope_SS[S])
2731
@inline final implicit def moarScalaHandHoldingMon[P,S,B](b: ComponentScopeU[P,S,B]): MonRExt_CompStateAccessOps[ComponentScope_SS, S] = (b: ComponentScope_SS[S])
32+
}
33+
34+
/**
35+
* Provide access to the set function on any compatible optic.
36+
* @tparam O The optic type.
37+
*/
38+
trait SetterMonocle[O[_, _, _, _]] {
39+
def set[S, B](l: O[S, S, _, B]): B => S => S
40+
}
41+
object SetterMonocle {
42+
implicit object LensS extends SetterMonocle[PLens] {
43+
@inline final override def set[S, B](l: PLens[S, S, _, B]): B => S => S = l.set
44+
}
45+
implicit object SetterS extends SetterMonocle[PSetter] {
46+
@inline final override def set[S, B](l: PSetter[S, S, _, B]): B => S => S = l.set
47+
}
48+
implicit object OptionalS extends SetterMonocle[POptional] {
49+
@inline final override def set[S, B](l: POptional[S, S, _, B]): B => S => S = l.set
50+
}
51+
implicit object IsoS extends SetterMonocle[PIso] {
52+
@inline final override def set[S, B](l: PIso[S, S, _, B]): B => S => S = l.set
53+
}
54+
implicit object PrismS extends SetterMonocle[PPrism] {
55+
@inline final override def set[S, B](l: PPrism[S, S, _, B]): B => S => S = l.set
56+
}
57+
implicit object TraversalS extends SetterMonocle[PTraversal] {
58+
@inline final override def set[S, B](l: PTraversal[S, S, _, B]): B => S => S = l.set
59+
}
2860
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,21 @@ object ScalazReact {
276276
@inline def setStateIO(s: S, cb: OpCallbackIO = undefined)(implicit C: CC): IO[Unit] =
277277
IO(C.setState(_c, s, cb))
278278

279+
@inline def _setStateIO[I](f: I => S, cb: OpCallbackIO = undefined)(implicit C: CC): I => IO[Unit] =
280+
i => setStateIO(f(i), cb)
281+
279282
@inline def modStateIO(f: S => S, cb: OpCallbackIO = undefined)(implicit C: CC): IO[Unit] =
280283
IO(_c.modState(f, cb))
281284

282285
def modStateIOF(f: S => S, cb: OpCallbackIO = undefined)(implicit C: CC, F: ChangeFilter[S]): IO[Unit] =
283286
stateIO.flatMap(s1 =>
284287
F(s1, f(s1), IO(()), setStateIO(_, cb)))
288+
289+
@inline def _modStateIO[I](f: I => S => S, cb: OpCallbackIO = undefined)(implicit C: CC): I => IO[Unit] =
290+
i => modStateIO(f(i), cb)
291+
292+
@inline def _modStateIOF[I](f: I => S => S, cb: OpCallbackIO = undefined)(implicit C: CC, F: ChangeFilter[S]): I => IO[Unit] =
293+
i => modStateIOF(f(i), cb)
285294
}
286295

287296
final case class ChangeFilter[S](allowChange: (S, S) => Boolean) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,21 @@ object ScalazReact {
279279
@inline def setStateIO(s: S, cb: OpCallbackIO = undefined)(implicit C: CC): IO[Unit] =
280280
IO(C.setState(_c, s, cb))
281281

282+
@inline def _setStateIO[I](f: I => S, cb: OpCallbackIO = undefined)(implicit C: CC): I => IO[Unit] =
283+
i => setStateIO(f(i), cb)
284+
282285
@inline def modStateIO(f: S => S, cb: OpCallbackIO = undefined)(implicit C: CC): IO[Unit] =
283286
IO(_c.modState(f, cb))
284287

285288
def modStateIOF(f: S => S, cb: OpCallbackIO = undefined)(implicit C: CC, F: ChangeFilter[S]): IO[Unit] =
286289
stateIO.flatMap(s1 =>
287290
F(s1, f(s1), IO(()), setStateIO(_, cb)))
291+
292+
@inline def _modStateIO[I](f: I => S => S, cb: OpCallbackIO = undefined)(implicit C: CC): I => IO[Unit] =
293+
i => modStateIO(f(i), cb)
294+
295+
@inline def _modStateIOF[I](f: I => S => S, cb: OpCallbackIO = undefined)(implicit C: CC, F: ChangeFilter[S]): I => IO[Unit] =
296+
i => modStateIOF(f(i), cb)
288297
}
289298

290299
final case class ChangeFilter[S](allowChange: (S, S) => Boolean) {

test/src/test/scala/japgolly/scalajs/react/MonocleTest.scala

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

33
import monocle._
4+
import scalaz.effect.IO
45
import utest._
56
import React._
67
import ScalazReact._
@@ -19,6 +20,7 @@ object MonocleTest extends TestSuite {
1920
"ComponentScopeM ops" - test[ComponentScopeM[U, S, U] ](_ focusStateL lensST).expect[ComponentStateFocus[T]]
2021
"ReactComponentM ops" - test[ReactComponentM[U, S, U, N]](_ focusStateL lensST).expect[ComponentStateFocus[T]]
2122
"ReactS.zoomL" - test[ReactST[M, S, A] ](_ zoomL lensTS ).expect[ReactST[M, T, A]]
23+
"c._setStateL" - test[BackendScope[Unit, S] ](_ _setStateL lensST ).expect[T => IO[Unit]]
2224
}
2325
}
24-
}
26+
}

0 commit comments

Comments
 (0)