Packages

package binding

Ordering
  1. Alphabetic
Visibility
  1. Public
  2. Protected

Type Members

  1. final class JsPromiseBinding[A] extends Binding[Option[Either[Any, A]]]

    A wrapper that wraps a scala.scalajs.js.Thenable to a com.thoughtworks.binding.Binding.

    Author:

    杨博 (Yang Bo) <[email protected]>

    Example:
    1. This JsPromiseBinding will cache the result of the thenable. Given a scala.scalajs.js.Thenable that will be delay executed.

      import scala.scalajs.js
      var upstreamEvaluationCount1 = 0
      val delayedThenable = js.Promise.resolve[Unit](()).`then`[Double] { case () =>
        upstreamEvaluationCount1 += 1
        math.random
      }

      The execution will not be performed right now.

      val jsPromiseBinding = JsPromiseBinding(delayedThenable)
      upstreamEvaluationCount1 should be(0)

      When there are multiple usages of jsPromiseBinding, each usage should be triggered with the same value.

      var evaluationCount1 = 0
      var evaluationCount2 = 0
      var evaluationCount3 = 0
      
      val usage1 = Binding {
        jsPromiseBinding.bind match {
          case Some(Right(value)) =>
            evaluationCount1 += 1
          case _ =>
        }
      }
      
      val usage2 = Binding {
        jsPromiseBinding.bind match {
          case Some(Right(value)) =>
            evaluationCount2 += 1
      
            val usage3 = Binding {
              jsPromiseBinding.bind match {
                case Some(Right(value)) =>
                  evaluationCount3 += 1
                case _ =>
              }
            }
      
            val _ = usage1.bind
            usage3.bind
          case _ =>
        }
      }
      
      usage2.watch()
      
      upstreamEvaluationCount1 should be(0)
      evaluationCount1 should be(0)
      evaluationCount2 should be(0)
      evaluationCount3 should be(0)

      And each usage should be triggered once and only once.

      delayedThenable.toFuture.map { _ =>
        upstreamEvaluationCount1 should be(1)
        evaluationCount1 should be(1)
        evaluationCount2 should be(1)
        evaluationCount3 should be(1)
      }

Value Members

  1. object JsPromiseBinding

Ungrouped