Skip to content

Commit

Permalink
Added simple future implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegIlyenko committed Oct 7, 2016
1 parent 1cecfa6 commit 59a3fbd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/scala/sangria/streaming/SubscriptionStream.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package sangria.streaming

import scala.language.higherKinds

import scala.annotation.implicitNotFound
import scala.concurrent.Future
import scala.language.higherKinds

@implicitNotFound(msg =
"Can't find suitable `SubscriptionStream` type-class instance for type `${StreamSource}`. " +
Expand Down
39 changes: 39 additions & 0 deletions src/main/scala/sangria/streaming/future.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package sangria.streaming

import language.higherKinds

import scala.concurrent.{ExecutionContext, Future}

object future {
class FutureSubscriptionStream(implicit ec: ExecutionContext) extends SubscriptionStream[Future] {
def supported[T[_]](other: SubscriptionStream[T]) = other.isInstanceOf[FutureSubscriptionStream]

def map[A, B](source: Future[A])(fn: A B) = source.map(fn)

def singleFuture[T](value: Future[T]) = value

def single[T](value: T) = Future.successful(value)

def mapFuture[A, B](source: Future[A])(fn: A Future[B]) =
source.flatMap(fn)

def first[T](s: Future[T]) = s

def failed[T](e: Throwable) = Future.failed(e)

def onComplete[Ctx, Res](result: Future[Res])(op: Unit) =
result
.map {x op; x}
.recover {case e op; throw e}

def flatMapFuture[Ctx, Res, T](future: Future[T])(resultFn: T Future[Res]) =
future flatMap resultFn

def merge[T](streams: Vector[Future[T]]) = Future.firstCompletedOf(streams)

def recover[T](stream: Future[T])(fn: Throwable T) =
stream recover {case e fn(e)}
}

implicit def futureSubscriptionStream(implicit ec: ExecutionContext) = new FutureSubscriptionStream
}

0 comments on commit 59a3fbd

Please sign in to comment.