Skip to content

Commit

Permalink
Calico interpreter returns signals
Browse files Browse the repository at this point in the history
  • Loading branch information
noelwelsh committed Apr 8, 2023
1 parent d729a34 commit f0b94ec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ lazy val commonSettings = Seq(
)
)

lazy val root = crossProject(JSPlatform, JVMPlatform)
lazy val root = project
.in(file("."))
.settings(moduleName := "gooey")
.aggregate(core)
.aggregate(core.js, core.jvm, calico)

lazy val core = crossProject(JSPlatform, JVMPlatform)
.in(file("core"))
Expand Down
29 changes: 24 additions & 5 deletions calico/src/main/scala/gooey/calico/Algebra.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package gooey.calico

import _root_.calico.html.io.{*, given}
import _root_.calico.*
import _root_.calico.html.io.{_, given}
import _root_.calico.syntax.*
import cats.effect.*
import cats.syntax.all.*
import fs2.concurrent.*
import fs2.dom.*
import gooey.component.*

Expand All @@ -10,18 +14,33 @@ object Algebra
Textbox.Algebra,
Checkbox.Algebra,
Above.Algebra { self =>
type UI[A] = Resource[IO, HtmlElement[IO]]

final case class Component[A](element: HtmlElement[IO], output: Signal[IO, A])

type UI[A] = Resource[IO, Component[A]]
def checkbox(c: Checkbox): UI[Boolean] = {
val Checkbox(label, default) = c
div(p("checkbox placeholder for $label"))
val element = div(p("checkbox placeholder for $label"))
val output = SignallingRef[IO].of(default).toResource

(element, output).mapN((e, o) => Component(e, o))
}

def textbox(t: Textbox): UI[String] = {
val Textbox(label, default, style) = t
div(p("textbox placeholder for $label"))
val element = div(p("textbox placeholder for $label"))
val output = SignallingRef[IO].of(default).toResource

(element, output).mapN((e, o) => Component(e, o))
}

def above[A, B](t: UI[A], b: UI[B]): UI[(A, B)] = {
div(t, b)
for {
top <- t
bot <- b
element = div(top.element, bot.element)
output = (top.output, bot.output).tupled
c <- element.map(e => Component(e, output))
} yield c
}
}

0 comments on commit f0b94ec

Please sign in to comment.