io.youi.ajax.AjaxAction.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of youi-core_sjs0.6_2.13 Show documentation
Show all versions of youi-core_sjs0.6_2.13 Show documentation
Core functionality leveraged and shared by most other sub-projects of YouI.
The newest version!
package io.youi.ajax
import org.scalajs.dom.XMLHttpRequest
import reactify._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
class AjaxAction(request: AjaxRequest) {
lazy val future: Future[XMLHttpRequest] = request.promise.future
private[ajax] val _state = Var[ActionState](ActionState.New)
def state: Val[ActionState] = _state
def loaded: Val[Double] = request.loaded
def total: Val[Double] = request.total
def percentage: Val[Int] = request.percentage
def cancelled: Val[Boolean] = request.cancelled
private[ajax] def start(manager: AjaxManager): Unit = {
if (!cancelled()) {
_state @= ActionState.Running
future.onComplete { result =>
_state @= ActionState.Finished
manager.remove(this)
}
request.send()
} else {
manager.remove(this)
}
}
// TODO: dequeue if not already running
def cancel(): Unit = request.cancel() // TODO: does cancel fire onComplete with a failure?
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy