Skip to content
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

Enable modifiers to be reified #234

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
22 changes: 22 additions & 0 deletions calico/src/main/scala/calico/html/Modifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ trait Modifier[F[_], E, A]:
inline final def contramap[B](inline f: B => A): Modifier[F, E, B] =
(b: B, e: E) => outer.modify(f(b), e)

extension (a: A)
inline final def toMod[E0 <: E]: Mod[F, E0] =
Mod(a)(using this.asInstanceOf[Modifier[F, E0, A]])

/**
* A reified modifier
*/
trait Mod[F[_], E]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make E a type member and point it to Node[F]?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not, esp. if I go forward with proposed changes in #180 so that modifiers can only be applied to elements/nodes on which they are valid. Right now it's a bit wild west.

protected type M
protected def mod: M
protected def modifier: Modifier[F, E, M]

object Mod:
def apply[F[_], E, A](a: A)(using Modifier[F, E, A]): Mod[F, E] =
new:
type M = A
def mod = a
def modifier = summon[Modifier[F, E, M]]

given [F[_], E, E0 <: E]: Modifier[F[_], E0, Mod[F, E]] = (m, e) =>
m.modifier.modify(m.mod, e)

object Modifier:
inline given forUnit[F[_], E]: Modifier[F, E, Unit] =
_forUnit.asInstanceOf[Modifier[F, E, Unit]]
Expand Down
5 changes: 4 additions & 1 deletion calico/src/test/scala/calico/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import calico.syntax.*
import cats.effect.*
import cats.syntax.all.*
import fs2.concurrent.*
import fs2.dom.*

class SyntaxSuite:

Expand All @@ -30,10 +31,12 @@ class SyntaxSuite:
def nodeSignal: SignallingRef[IO, Resource[IO, fs2.dom.Node[IO]]] = ???
def nodeOptionSignal: SignallingRef[IO, Option[Resource[IO, fs2.dom.Node[IO]]]] = ???

def mod = (cls := "bar").toMod[Node[IO]]

def signalModifiers =
div(
stringSignal,
stringOptionSignal,
nodeSignal,
nodeOptionSignal
).flatTap(_.modify(cls := "foo"))
).flatTap(_.modify(cls := "foo")).flatTap(_.modify(mod))