/
CounterExample.scala
61 lines (47 loc) · 1.46 KB
/
CounterExample.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package client.sodium.app.reactComponents.compositeComponents
import client.cache.{Cache, CacheMap, NormalizedStateHolder}
import client.sodium.app.actions.SActionWriteToConsole
import client.sodium.app.reactComponents.atomicComponents.{
CellTemplate,
SButton,
SPreformattedText,
STextArea,
StreamTemplate
}
import client.sodium.core.StreamSink
import client.sodium.core._
import dataStorage.User
import japgolly.scalajs.react.ScalaComponent
import japgolly.scalajs.react.vdom.html_<^.{<, _}
import scala.concurrent.ExecutionContextExecutor
case class CounterExample() {
implicit def executionContext: ExecutionContextExecutor =
scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
lazy val button = SButton("Inc")
val counter_ = Transaction.apply[CellTemplate[Int]]({ _ =>
lazy val afterUpdate: Stream[Int] = button.getClick.snapshot(counterValue, {
(_, c: Int) =>
c + 1
})
lazy val counterValue: CellLoop[Int] = new CellLoop[Int]()
counterValue.loop(afterUpdate.hold(0))
lazy val counter: CellTemplate[Int] = CellTemplate[Int](counterValue, { i =>
<.div(s"value is: $i")
})
counter
})
def getComp = {
val render: Unit => VdomElement = { _ =>
<.div(s"This is the counter example",
<.br,
counter_.getComp(),
button.vdom())
}
val rootComp =
ScalaComponent
.builder[Unit]("Hello")
.render_P(render)
.build
rootComp
}
}