Skip to content

Commit 819dafd

Browse files
committed
Move Reusable & Reusability into core
1 parent ea27584 commit 819dafd

File tree

15 files changed

+42
-44
lines changed

15 files changed

+42
-44
lines changed

extra/src/main/scala/japgolly/scalajs/react/extra/Reusability.scala renamed to core/src/main/scala/japgolly/scalajs/react/Reusability.scala

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
package japgolly.scalajs.react.extra
1+
package japgolly.scalajs.react
22

3-
import scala.annotation.tailrec
4-
import scala.reflect.ClassTag
3+
import japgolly.scalajs.react.internal.{OptionLike, ReusabilityMacros}
54
import java.util.{Date, UUID}
65
import org.scalajs.dom.console
6+
import scala.annotation.tailrec
7+
import scala.reflect.ClassTag
78
import scala.scalajs.js.{Date => JsDate}
8-
import japgolly.scalajs.react._
9-
import japgolly.scalajs.react.extra.internal.ReusabilityMacros
10-
import japgolly.scalajs.react.internal.OptionLike
119

1210
/**
1311
* Tests whether one instance can be used in place of another.
@@ -321,9 +319,6 @@ object Reusability {
321319
def shouldComponentUpdateAndLog[P: Reusability, C <: Children, S: Reusability, B, U <: UpdateSnapshot](name: String): ScalaComponent.Config[P, C, S, B, U, U] =
322320
shouldComponentUpdateAnd(_ log name)
323321

324-
def shouldComponentUpdateWithOverlay[P: Reusability, C <: Children, S: Reusability, B, U <: UpdateSnapshot]: ScalaComponent.Config[P, C, S, B, U, U] =
325-
ReusabilityOverlay.install(DefaultReusabilityOverlay.defaults)
326-
327322
final case class ShouldComponentUpdateResult[P: Reusability, S: Reusability, B](self: ScalaComponent.Lifecycle.ShouldComponentUpdate[P, S, B]) {
328323
def mounted = self.mountedImpure
329324
def backend = self.backend

extra/src/main/scala/japgolly/scalajs/react/extra/Reusable.scala renamed to core/src/main/scala/japgolly/scalajs/react/Reusable.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
package japgolly.scalajs.react.extra
1+
package japgolly.scalajs.react
22

33
import scala.reflect.ClassTag
4-
import japgolly.scalajs.react._
54

65
/**
76
* A value that has been explicitly paired with a (potentially ad-hoc) [[Reusability]] instance.

extra/src/main/scala/japgolly/scalajs/react/extra/internal/ReusabilityMacros.scala renamed to core/src/main/scala/japgolly/scalajs/react/internal/ReusabilityMacros.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package japgolly.scalajs.react.extra.internal
1+
package japgolly.scalajs.react.internal
22

33
import scala.reflect.macros.blackbox.Context
4-
import japgolly.scalajs.react.extra.Reusability
5-
import japgolly.scalajs.react.internal.MacroUtils
4+
import japgolly.scalajs.react.Reusability
65

76
class ReusabilityMacros(val c: Context) extends MacroUtils {
87
import c.universe._
@@ -27,10 +26,10 @@ class ReusabilityMacros(val c: Context) extends MacroUtils {
2726

2827
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
2928

30-
private val Reusability = q"_root_.japgolly.scalajs.react.extra.Reusability"
29+
private val Reusability = q"_root_.japgolly.scalajs.react.Reusability"
3130

3231
private def ReusabilityA(t: Type): Type =
33-
appliedType(c.typeOf[japgolly.scalajs.react.extra.Reusability[_]], t)
32+
appliedType(c.typeOf[japgolly.scalajs.react.Reusability[_]], t)
3433

3534
private def implSumType[T: c.WeakTypeTag](debug: Boolean, derive: Boolean): c.Expr[Reusability[T]] = {
3635
val T = weakTypeOf[T]

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,11 @@ package object react extends ReactEventTypes {
8080
case Some(h) => withRef(h)
8181
}
8282
}
83+
84+
type ~=>[-A, +B] = Reusable[A => B]
85+
86+
implicit final class ReactExtrasExt_Any[A](private val self: A) extends AnyVal {
87+
@inline def ~=~(a: A)(implicit r: Reusability[A]): Boolean = r.test(self, a)
88+
@inline def ~/~(a: A)(implicit r: Reusability[A]): Boolean = !r.test(self, a)
89+
}
8390
}

doc/changelog/1.4.0.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
use the new `AsyncCallback` instead.
3535
* The new `getDerivedStateFromError` has *not* been implemented, and cannot be until facebook/react#13986 is merged.
3636

37+
* `Reusability` & `Reusable` have been moved from the `extra` module, into `core`.
38+
1. It was required to implement the new `React.memo`
39+
2. It's necessary for using core React feature `shouldComponentUpdate` reasonably
40+
3741
* `Callback` changes
3842
* `.attemptTry` now catches all types of exception
3943
* Add `.memo` which memoises the result
@@ -60,20 +64,24 @@
6064

6165
# Migration
6266

63-
```
64-
# Replace .(async|delay|delayMs) on Callback
67+
* `Reusability` & `Reusable` have been moved from package `japgolly.scalajs.react.extra` to `japgolly.scalajs.react`.
6568

69+
```
6670
find . -type f -name '*.scala' -exec perl -pi -e '
71+
72+
# Replace .(async|delay|delayMs) on Callback
6773
s/([. ])((?:async|delay|delayMs) *\([^()]+\))/$1$2$1asCallbackToFuture/;
6874
s/asCallbackToFuture[. ]void/toCallback/;
75+
76+
s/Reusability.shouldComponentUpdateWithOverlay/ReusabilityOverlay.install/;
77+
6978
' {} +
7079
```
7180

7281

7382
# TODO
7483

7584
* `React.memo`
76-
* Move Reusability into core
7785
* Add `React.memo`
7886
* Add `.memo` (?) to `JsFnComponent`
7987
* Test from `ScalaFnComponent`

extra/src/main/scala/japgolly/scalajs/react/extra/ReusabilityOverlay.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ object ReusabilityOverlay {
1818

1919
private val key = "reusabilityOverlay"
2020

21+
def install[P: Reusability, C <: Children, S: Reusability, B, U <: UpdateSnapshot]: ScalaComponent.Config[P, C, S, B, U, U] =
22+
install(DefaultReusabilityOverlay.defaults)
23+
2124
def install[P: Reusability, C <: Children, S: Reusability, B, U <: UpdateSnapshot]
2225
(newOverlay: ScalaComponent.MountedImpure[P, S, B] => ReusabilityOverlay): ScalaComponent.Config[P, C, S, B, U, U] = {
2326

extra/src/main/scala/japgolly/scalajs/react/extra/components/TriStateCheckbox.scala

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

33
import japgolly.scalajs.react._
44
import japgolly.scalajs.react.vdom.html_<^._
5-
import japgolly.scalajs.react.extra._
65
import org.scalajs.dom.ext.KeyCode
76
import org.scalajs.dom.html.Input
87

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

Lines changed: 0 additions & 12 deletions
This file was deleted.

extra/src/main/scala/japgolly/scalajs/react/extra/router/RouterCtl.scala

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

33
import japgolly.scalajs.react._
4-
import japgolly.scalajs.react.extra.Reusability
54
import japgolly.scalajs.react.vdom.html_<^._
65
import org.scalajs.dom.html
76

gh-pages/src/main/scala/ghpages/examples/ReuseExample.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object ReuseExample {
1212
lazy val main = addIntro(topLevelComponent.withKey(_)(), _(
1313
^.marginBottom := "2.4em",
1414
"The colourful overlays here are provided by ",
15-
<.code("Reusability.shouldComponentUpdateWithOverlay"),
15+
<.code("ReusabilityOverlay.install"),
1616
" and demonstrate how many updates are prevented vs rendered in each component.",
1717
<.br,
1818
"Hover for more info. Click one to print detail to the JS console."))
@@ -26,7 +26,7 @@ object ReuseExample {
2626
.render_P(sum =>
2727
<.h1(
2828
"The sum of all inputs is", <.br, sum))
29-
.configure(Reusability.shouldComponentUpdateWithOverlay)
29+
.configure(ReusabilityOverlay.install)
3030
.build
3131

3232
case class InputControl(current: Int, change: Int ~=> Callback)
@@ -39,7 +39,7 @@ object ReuseExample {
3939
<.span(^.padding := "0 1ex", p.current),
4040
<.button("+1", ^.onClick --> p.change(1)))
4141
)
42-
.configure(Reusability.shouldComponentUpdateWithOverlay)
42+
.configure(ReusabilityOverlay.install)
4343
.build
4444

4545
val numberRegex = "^-?\\d+$".r
@@ -54,7 +54,7 @@ object ReuseExample {
5454
^.value := v.value.toString,
5555
^.onChange ==>? update)
5656
}
57-
.configure(Reusability.shouldComponentUpdateWithOverlay)
57+
.configure(ReusabilityOverlay.install)
5858
.build
5959

6060
// ---------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)