OlivierBlanvillain / monadic-html

Tiny DOM binding library for Scala.js
https://olivierblanvillain.github.io/monadic-html/examples/
MIT License
225 stars 24 forks source link

keepIf check twice everytime. #101

Closed sometao closed 6 years ago

sometao commented 6 years ago
@JSExportTopLevel("tmp.ShowKeepIfBug")
object ShowKeepIfBug {

  @JSExport
  def run(): Cancelable = {
    val a = Var(10)
    val foo = a.keepIf { i =>
      println(s"println check i: $i")
      i > 0
    }(0).map(i => <div>value: [{i}]</div>)

    val page =
      <div>
        {foo}
        <button onclick={_: Event => a.update( _ + 1)}>clickMe.</button>
      </div>
    mount(dom.document.body, page)
  }

}

run the page in chrome. when i click the button, the console output like this:

println check i: 10
18:20:08.161 System.scala:367 println check i: 10
18:20:14.138 System.scala:367 println check i: 11
18:20:14.138 System.scala:367 println check i: 11
18:20:15.109 System.scala:367 println check i: 12
18:20:15.109 System.scala:367 println check i: 12
18:20:15.740 System.scala:367 println check i: 13
18:20:15.740 System.scala:367 println check i: 13
18:20:16.412 System.scala:367 println check i: 14
18:20:16.412 System.scala:367 println check i: 14
18:20:16.889 System.scala:367 println check i: 15
18:20:16.889 System.scala:367 println check i: 15
18:20:17.186 System.scala:367 println check i: 16
18:20:17.186 System.scala:367 println check i: 16

I'm not sure if this is a bug or where I got it wrong.

versions: mhtml-0.4.0-RC1 scala-2.12.5 scala.js-0.6.22 scalajs-dom-0.9.2 sbt.version = 1.1.2

sometao commented 6 years ago

collect has the same problem.

  @JSExport
  def run(): Unit = {
    val a = Var(1)
    def check(n: Int) = {
      println(s"check num: [$n]")
      n < 10
    }
    val b = a.collect {
      case x if check(x) => x
    }(0).map(i =>
      <div>value: [{i}]</div>)

    val page =
      <div>
        {b}<button onclick={_: Event => a.update(_ + 1)}>clickMe.</button>
      </div>
    mount(dom.document.body, page)
  }
check num: [1]
19:01:50.559 System.scala:367 check num: [1]
19:01:51.947 System.scala:367 check num: [2]
19:01:51.947 System.scala:367 check num: [2]
19:01:52.166 System.scala:367 check num: [3]
19:01:52.167 System.scala:367 check num: [3]
19:01:52.376 System.scala:367 check num: [4]
19:01:52.377 System.scala:367 check num: [4]
19:01:52.573 System.scala:367 check num: [5]
19:01:52.573 System.scala:367 check num: [5]
bbarker commented 6 years ago

You are accessing (running) the Rx twice; see #94 for the planned fix, or use .sharing for now

On Tue, Apr 17, 2018, 7:01 AM Tao notifications@github.com wrote:

collect has the same problem.

@JSExport def run(): Unit = { val a = Var(1) def check(n: Int) = { println(s"check num: [$n]") n < 10 } val b = a.collect { case x if check(x) => x }(0).map(i =>

value: [{i}]
) val page =
{b}
mount(dom.document.body, page) } — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub , or mute the thread .
OlivierBlanvillain commented 6 years ago

Nice find, there is indeed a bug in collect!

bbarker commented 6 years ago

Sorry, I didn't look closely enough at this, apparently

sometao commented 6 years ago

@OlivierBlanvillain Thanks for such a quick response. When will there be a new stable version on maven?