Skip to content

Commit

Permalink
Added Time.delay as a convenience feature for cross-platform delayed …
Browse files Browse the repository at this point in the history
…Future
  • Loading branch information
darkfrog26 committed Nov 30, 2018
1 parent 3ed7275 commit 3cb02ca
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions core/js/src/main/scala/io/youi/YouIPlatform.scala
@@ -0,0 +1,14 @@
package io.youi

import scala.concurrent.{Future, Promise}
import org.scalajs.dom._

object YouIPlatform {
def delay(millis: Long): Future[Unit] = {
val promise = Promise[Unit]
window.setTimeout(() => {
promise.success(())
}, millis)
promise.future
}
}
17 changes: 17 additions & 0 deletions core/jvm/src/main/scala/io/youi/YouIPlatform.scala
@@ -0,0 +1,17 @@
package io.youi

import java.util.{Timer, TimerTask}

import scala.concurrent.{Future, Promise}

object YouIPlatform {
private lazy val timer = new Timer("io.youi.Time", true)

def delay(millis: Long): Future[Unit] = {
val promise = Promise[Unit]
timer.schedule(new TimerTask {
override def run(): Unit = promise.success(())
}, millis)
promise.future
}
}
17 changes: 17 additions & 0 deletions core/shared/src/main/scala/io/youi/util/Time.scala
@@ -0,0 +1,17 @@
package io.youi.util

import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration

/**
* Cross-platform functionality for dealing with time
*/
object Time {
/**
* Creates a non-blocking Future that is completed after `duration` is elapsed
*
* @param duration an amount of time that must elapse before the future is complete
* @return Future[Unit] that will be complete after `duration`
*/
def delay(duration: FiniteDuration): Future[Unit] = io.youi.YouIPlatform.delay(duration.toMillis)
}

0 comments on commit 3cb02ca

Please sign in to comment.