Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Time.delay as a convenience feature for cross-platform delayed …
…Future
- Loading branch information
1 parent
3ed7275
commit 3cb02ca
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |