Skip to content

Commit

Permalink
#345 remove unecessary dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
simerplaha committed Dec 18, 2021
1 parent 0cf5b22 commit 8337681
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions swaydb/actor/src/main/scala/swaydb/Scheduler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,24 @@ object Scheduler {
}
}

class Scheduler private(timer: Timer)(implicit val ec: ExecutionContext) {
class Scheduler private(timer: Timer) {

def apply[T](delayFor: FiniteDuration)(block: => Future[T]): Future[T] = {
val promise = Promise[T]()
val task =
new TimerTask {
def run(): Unit = {
ec.execute(
new Runnable {
override def run(): Unit =
promise.completeWith(block)
}
)
}
def run(): Unit =
promise.completeWith(block)
}

timer.schedule(task, delayFor.toMillis max 0)
promise.future
}

def future[T](delayFor: FiniteDuration)(block: => T): Future[T] =
def future[T](delayFor: FiniteDuration)(block: => T)(implicit ec: ExecutionContext): Future[T] =
apply(delayFor)(Future(block))

def futureFromIO[T](delayFor: FiniteDuration)(block: => IO[swaydb.Error.Segment, T]): Future[T] =
def futureFromIO[T](delayFor: FiniteDuration)(block: => IO[swaydb.Error.Segment, T])(implicit ec: ExecutionContext): Future[T] =
apply(delayFor)(Future(block).flatMap(_.toFuture))

def task(delayFor: FiniteDuration)(block: => Unit): TimerTask = {
Expand Down

0 comments on commit 8337681

Please sign in to comment.