Skip to content

Commit 83f8f08

Browse files
committed
refactor + constrain CompStateAccessOps earlier
1 parent 9d90800 commit 83f8f08

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

core/src/main/scala/japgolly/scalajs/react/package.scala

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -260,42 +260,43 @@ package object react {
260260
type OpCallback = UndefOr[() => Unit]
261261

262262
trait CompStateAccess[C[_]] {
263-
def state[A](f: C[A]): A
264-
def setState[A](f: C[A], a: A, cb: OpCallback): Unit
263+
def state[S](c: C[S]): S
264+
def setState[S](f: C[S], s: S, cb: OpCallback): Unit
265265
}
266266

267267
implicit object CompStateAccess_SS extends CompStateAccess[ComponentScope_SS] {
268-
override def state[A](u: ComponentScope_SS[A]): A =
269-
u._state.v
268+
override def state[S](c: ComponentScope_SS[S]): S =
269+
c._state.v
270270

271-
override def setState[A](u: ComponentScope_SS[A], a: A, cb: OpCallback = undefined): Unit =
272-
u._setState(WrapObj(a), cb.map[JFn](f => f))
271+
override def setState[S](c: ComponentScope_SS[S], s: S, cb: OpCallback = undefined): Unit =
272+
c._setState(WrapObj(s), cb.map[JFn](f => f))
273273
}
274274

275-
// CompStateAccess[C] should really be a class param but then we lose the AnyVal
276-
@inline implicit final class CompStateAccessOps[C[_], A](val _c: C[A]) extends AnyVal {
275+
@inline implicit final def toCompStateAccessOps[C[_]: CompStateAccess, S](c: C[S]) = new CompStateAccessOps(c)
276+
final class CompStateAccessOps[C[_], S](val _c: C[S]) extends AnyVal {
277+
// CompStateAccess[C] should really be a class param but then we lose the AnyVal
277278
type CC = CompStateAccess[C]
278279

279-
@inline def state(implicit C: CC): A =
280+
@inline def state(implicit C: CC): S =
280281
C.state(_c)
281282

282-
@inline def setState(a: A, cb: OpCallback = undefined)(implicit C: CC): Unit =
283-
C.setState(_c, a, cb)
283+
@inline def setState(s: S, cb: OpCallback = undefined)(implicit C: CC): Unit =
284+
C.setState(_c, s, cb)
284285

285-
@inline def modState(f: A => A, cb: OpCallback = undefined)(implicit C: CC): Unit =
286+
@inline def modState(f: S => S, cb: OpCallback = undefined)(implicit C: CC): Unit =
286287
setState(f(state), cb)
287288

288-
@inline def modStateO(f: A => Option[A], cb: OpCallback = undefined)(implicit C: CC): Unit =
289+
@inline def modStateO(f: S => Option[S], cb: OpCallback = undefined)(implicit C: CC): Unit =
289290
f(state).fold(())(setState(_, cb))
290291

291-
@inline def modStateU(f: A => UndefOr[A], cb: OpCallback = undefined)(implicit C: CC): Unit =
292+
@inline def modStateU(f: S => UndefOr[S], cb: OpCallback = undefined)(implicit C: CC): Unit =
292293
f(state).fold(())(setState(_, cb))
293294

294-
@inline def focusStateId(implicit C: CC) = new ComponentStateFocus[A](
295+
@inline def focusStateId(implicit C: CC) = new ComponentStateFocus[S](
295296
() => _c.state,
296-
(a: A, cb: OpCallback) => _c.setState(a, cb))
297+
(a: S, cb: OpCallback) => _c.setState(a, cb))
297298

298-
@inline def focusState[B](f: A => B)(g: (A, B) => A)(implicit C: CC) = new ComponentStateFocus[B](
299+
@inline def focusState[B](f: S => B)(g: (S, B) => S)(implicit C: CC) = new ComponentStateFocus[B](
299300
() => f(_c.state),
300301
(b: B, cb: OpCallback) => _c.setState(g(_c.state, b), cb))
301302
}
@@ -305,8 +306,8 @@ package object react {
305306
private[react] val _s: (B, OpCallback) => Unit)
306307

307308
implicit object ComponentStateFocusAccess extends CompStateAccess[ComponentStateFocus] {
308-
override def state[A](u: ComponentStateFocus[A]): A = u._g()
309-
override def setState[A](u: ComponentStateFocus[A], a: A, cb: OpCallback = undefined): Unit = u._s(a, cb)
309+
override def state[S](c: ComponentStateFocus[S]): S = c._g()
310+
override def setState[S](c: ComponentStateFocus[S], a: S, cb: OpCallback = undefined): Unit = c._s(a, cb)
310311
}
311312

312313
@inline final implicit def autoFocusEntireState[S](s: ComponentScope_SS[S]): ComponentStateFocus[S] = s.focusStateId

0 commit comments

Comments
 (0)