This file contains hidden or 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
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
// YESS :( | |
someFutureJob().map { value => | |
operate(value) | |
} | |
This file contains hidden or 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
import scala.concurrent.Future | |
trait AsyncJob { | |
def execute(): Future[Boolean] | |
} | |
class MyLongRunningJob(asyncJob: AsyncJob) extends Runnable { | |
override def run(): Unit = { | |
asyncJob.execute() | |
} |
This file contains hidden or 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
import scala.concurrent.duration.Duration | |
import scala.concurrent.{Await, Future} | |
trait AsyncJob { | |
def execute(): Future[Boolean] | |
} | |
class MyLongRunningJob(asyncJob: AsyncJob) extends Runnable { | |
override def run(): Unit = { | |
val result = Await.result(asyncJob.execute(), Duration.Inf) |
This file contains hidden or 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
import scala.concurrent.Future | |
trait WaterSupply { | |
def turnOn(): Future[Unit] | |
} | |
trait PowerPlant { | |
def waterSupply: WaterSupply | |
def increaseVolumeControl(): Future[Boolean] |
This file contains hidden or 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
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
trait WaterSupply { | |
def turnOn(): Future[Unit] | |
} | |
trait PowerPlant { | |
def waterSupply: WaterSupply |
This file contains hidden or 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
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.{ExecutionContext, Future} | |
trait Request | |
trait Response | |
trait CacheService { | |
def isDuplicateRequest(request: Request)(implicit ec: ExecutionContext): Future[Option[Request]] |
This file contains hidden or 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
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.{ExecutionContext, Future} | |
trait Request | |
trait Response | |
trait CacheService { | |
def myOwnEc: ExecutionContext |
This file contains hidden or 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
def executeOperation(): Future[Boolean] = { | |
someHeavyCPUOperation() | |
Future(anotherOperation()) | |
} |
This file contains hidden or 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
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.{ExecutionContext, Future} | |
class API(cacheService: CacheService, processor: Processor) { | |
def process(request: Request): Future[Response] = { | |
for { | |
dedupedRequest <- cacheService.isDuplicateRequest(request) | |
response <- processor.process(dedupedRequest) | |
_ = cacheService.setCacheForDeduplication(dedupedRequest) // now unplugged from the flow |
This file contains hidden or 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
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.{ExecutionContext, Future} | |
class API(cacheService: CacheService, processor: Processor) { | |
def process(request: Request): Future[Response] = { | |
for { | |
dedupedRequest <- cacheService.isDuplicateRequest(request) | |
response <- processor.process(dedupedRequest) | |
_ = cacheService.setCacheForDeduplication(dedupedRequest) |