From e98cb7df0276c31f1f17e341ef557c42d04f3eb1 Mon Sep 17 00:00:00 2001 From: Regis Kuckaertz Date: Mon, 19 Feb 2018 10:02:47 +0000 Subject: [PATCH] Alteration should return a Future --- src/main/scala/com/gu/Box.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/scala/com/gu/Box.scala b/src/main/scala/com/gu/Box.scala index b334473..8654f43 100644 --- a/src/main/scala/com/gu/Box.scala +++ b/src/main/scala/com/gu/Box.scala @@ -2,7 +2,7 @@ package com.gu import java.util.concurrent.atomic.AtomicReference -import scala.concurrent.Future +import scala.concurrent.{ExecutionContext, Future} abstract class Box[T] { def get(): T @@ -12,7 +12,7 @@ abstract class Box[T] { def send(f: T => T): Unit def alter(t: T): Future[T] - def alter(f: T => T): Future[T] + def alter(f: T => Future[T])(implicit ec: ExecutionContext): Future[T] def map[A](f: T => A): Box[A] def flatMap[A](f: T => Box[A]): Box[A] @@ -32,7 +32,7 @@ private class AtomicRefBox[T](t: T) extends Box[T] { def send(f: T => T): Unit = ref.updateAndGet(t => f(t)) def alter(t: T): Future[T] = Future.successful(ref.updateAndGet(_ => t)) - def alter(f: T => T): Future[T] = Future.successful(ref.updateAndGet(t => f(t))) + def alter(f: T => Future[T])(implicit ec: ExecutionContext): Future[T] = f(t).map(t => ref.updateAndGet(_ => t)) def map[A](f: T => A): Box[A] = new AtomicRefBox[A](f(get())) def flatMap[A](f: T => Box[A]): Box[A] = f(get())