Skip to content
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
26 changes: 23 additions & 3 deletions calico/src/main/scala/calico/html/Prop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package calico
package html

import calico.syntax.*
import cats.Functor
import cats.FunctorFilter
import cats.Id
import cats.effect.kernel.Async
import cats.effect.kernel.Resource
import cats.syntax.all.*
Expand Down Expand Up @@ -133,15 +136,32 @@ private trait PropModifiers[F[_]](using F: Async[F]):
Modifier.forSignalResource[F, Any, OptionSignalResourceModifier[F, Any, Any], Option[Any]](
_.values) { (m, n) => setPropOption(n, m.name, m.codec) }

final class EventProp[F[_], E] private[calico] (key: String):
final class EventProp[F[_], E, A] private[calico] (key: String, pipe: Pipe[F, E, A]):
import EventProp.*
inline def -->(sink: Pipe[F, E, Nothing]): PipeModifier[F, E] = PipeModifier(key, sink)

@inline def -->(sink: Pipe[F, A, Nothing]): PipeModifier[F, E] =
PipeModifier(key, pipe.andThen(sink))

@inline private def through[B](pipe: Pipe[F, A, B]): EventProp[F, E, B] =
new EventProp(key, this.pipe.andThen(pipe))

object EventProp:
final class PipeModifier[F[_], E](
def apply[F[_], E](key: String): EventProp[F, E, E] =
new EventProp(key, identity(_))

final class PipeModifier[F[_], E] private[calico] (
private[calico] val key: String,
private[calico] val sink: Pipe[F, E, Nothing])

inline given [F[_], E]: (Functor[EventProp[F, E, _]] & FunctorFilter[EventProp[F, E, _]]) =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Intersection type instance, exciting 😄

_functor.asInstanceOf[Functor[EventProp[F, E, _]] & FunctorFilter[EventProp[F, E, _]]]
private val _functor: Functor[EventProp[Id, Any, _]] & FunctorFilter[EventProp[Id, Any, _]] =
new Functor[EventProp[Id, Any, _]] with FunctorFilter[EventProp[Id, Any, _]]:
def map[A, B](fa: EventProp[Id, Any, A])(f: A => B) = fa.through(_.map(f))
def functor = this
def mapFilter[A, B](fa: EventProp[Id, Any, A])(f: A => Option[B]) =
fa.through(_.mapFilter(f))

private trait EventPropModifiers[F[_]](using F: Async[F]):
import EventProp.*
inline given forPipeEventProp[T <: fs2.dom.Node[F], E]: Modifier[F, T, PipeModifier[F, E]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private[codegen] class CalicoGenerator(srcManaged: File)
val baseImplDef =
if (outputBaseImpl)
List(
s"@inline private[calico] def ${keyImplName}[Ev <: ${baseScalaJsEventType}](key: String): ${keyKind}[F, Ev] = ${keyKindConstructor(keyKind)}(key)"
s"@inline private[calico] def ${keyImplName}[Ev <: ${baseScalaJsEventType}](key: String): ${keyKind}[F, Ev, Ev] = ${keyKind}(key)"
)
else {
Nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ object DomDefsGenerator {
case (key, vals) =>
(
key,
vals.map(attr => attr.copy(scalaJsEventType = "F, " + attr.scalaJsEventType)))
vals.map(attr =>
attr.copy(scalaJsEventType =
s"F, ${attr.scalaJsEventType}, ${attr.scalaJsEventType}")))
},
printDefGroupComments = true,
traitCommentLines = Nil,
Expand Down Expand Up @@ -311,7 +313,8 @@ object DomDefsGenerator {
(
key,
vals.map(attr =>
attr.copy(scalaJsEventType = "F, " + attr.scalaJsEventType)))
attr.copy(scalaJsEventType =
s"F, ${attr.scalaJsEventType}, ${attr.scalaJsEventType}")))
},
printDefGroupComments = true,
traitCommentLines = List(eventPropsDefGroups.head._1),
Expand Down