Skip to content

Add Reusability for component refs #1055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ object Reusability extends ReusabilityMacros with ScalaVersionSpecificReusabilit
implicit def refFullF[F[_], I, A, O]: Reusability[Ref.FullF[F, I, A, O]] =
refRaw[A | Null].contramap(_.raw)

/** Updating a reference doesn't trigger a component re-rendering, nor is the current reference value considered for reusability.
*
* Any `map`/`contramap` functions installed in the ref are ignored for the sake of reusability. If this is undesirable, pass around
* a [[Reusable]] ref instead.
*/
implicit def refToComponentF[F[_], I, R, O, C]: Reusability[Ref.ToComponentF[F, I, R, O, C]] =
refFullF[F, I, R, O].narrow

/** Updating a reference doesn't trigger a component re-rendering, nor is the current reference value considered for reusability. */
implicit def nonEmptyRefHandleF[F[_], A]: Reusability[NonEmptyRef.HandleF[F, A]] =
refRaw[A].contramap(_.raw)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ object ReusabilityTest extends TestSuite {
assertEq(r.test(a, b), a == b)
}

private def testByRef[A: Reusability](create: => A)(implicit l: Line): Unit = {
val a = create
val b = create
assertEq(a ~=~ a, true)
assertEq(a ~=~ b, false)
}

override def tests = Tests {

"macros" - {
Expand Down Expand Up @@ -477,5 +484,18 @@ object ReusabilityTest extends TestSuite {
assert(BigInt("10") ~=~ BigInt("10"))
assert(BigInt("10") ~/~ BigInt("11"))
}

"ref" - {
"handle" - testByRef(Ref[Int]: Ref.Handle[Int])
"simple" - testByRef(Ref[Int])
"vdom" - testByRef(Ref.toAnyVdom())
"scala" - testByRef(Ref.toScalaComponent(SampleComponent1.component))
"js" - testByRef(Ref.toJsComponent(JsComponentEs6PTest.Component))
}

"nonEmptyRef" - {
"handle" - testByRef(NonEmptyRef[Int](6): NonEmptyRef.Handle[Int])
"simple" - testByRef(NonEmptyRef[Int](6))
}
}
}