-
Notifications
You must be signed in to change notification settings - Fork 7
/
package.scala
118 lines (94 loc) · 3.79 KB
/
package.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2017 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.thoughtworks
import com.thoughtworks.future.continuation.Continuation
import com.thoughtworks.tryt.covariant.TryT
import scala.concurrent.ExecutionContext
import scalaz.{@@, Applicative, BindRec, ContT, MonadError, Semigroup, Trampoline}
import scalaz.Free.Trampoline
import scala.language.higherKinds
import scala.util.{Success, Try}
import scalaz.Tags.Parallel
/**
* @author 杨博 (Yang Bo)
*/
package object future {
private[future] trait OpacityTypes {
type Future[+A]
type ParallelFuture[A] = Future[A] @@ Parallel
def fromTryT[A](tryT: TryT[Continuation, A]): Future[A]
def toTryT[A](future: Future[A]): TryT[Continuation, A]
def futureMonadError: MonadError[Future, Throwable] with BindRec[Future]
def futureParallelApplicative(implicit throwableSemigroup: Semigroup[Throwable]): Applicative[ParallelFuture]
}
private[future] val opacityTypes: OpacityTypes = new OpacityTypes {
type Future[+A] = TryT[Continuation, A]
override def fromTryT[A](tryT: TryT[Continuation, A]): Future[A] = tryT
override def toTryT[A](future: Future[A]): TryT[Continuation, A] = future
def futureMonadError: MonadError[Future, Throwable] with BindRec[Future] = {
TryT.tryTBindRec(Continuation.continuationMonad, Continuation.continuationMonad)
}
def futureParallelApplicative(implicit throwableSemigroup: Semigroup[Throwable]): Applicative[ParallelFuture] = {
TryT.tryTParallelApplicative[Continuation](Continuation.continuationParallelApplicative, throwableSemigroup)
}
}
object Future {
def jump()(implicit executionContext: ExecutionContext): Future[Unit] = {
Future.shift { handler: (Try[Unit] => Trampoline[Unit]) =>
Trampoline.delay {
executionContext.execute {
new Runnable {
override def run(): Unit = handler(Success(())).run
}
}
}
}
}
def now[A](a: A): Future[A] = {
Future(TryT(Continuation.delay(Success(a))))
}
def delay[A](a: => A): Future[A] = {
Future(TryT(Continuation.delay(Try(a))))
}
implicit def futureMonadError: MonadError[Future, Throwable] with BindRec[Future] = {
opacityTypes.futureMonadError
}
implicit def futureParallelApplicative(
implicit throwableSemigroup: Semigroup[Throwable]): Applicative[ParallelFuture] = {
opacityTypes.futureParallelApplicative
}
def apply[A](tryT: TryT[Continuation, A]): Future[A] = {
opacityTypes.fromTryT(tryT)
}
def unapply[A](future: Future[A]): Some[TryT[Continuation, A]] = {
Some(opacityTypes.toTryT(future))
}
def shift[A](run: (Try[A] => Trampoline[Unit]) => Trampoline[Unit]): Future[A] = {
opacityTypes.fromTryT(TryT(Continuation.shift(run)))
}
def run[A](future: Future[A])(handler: Try[A] => Trampoline[Unit]): Trampoline[Unit] = {
val TryT(continuation) = opacityTypes.toTryT(future)
Continuation.run(continuation)(handler)
}
def listen[A](future: Future[A])(handler: Try[A] => Trampoline[Unit]): Unit = {
run(future)(handler).run
}
}
/** @template */
type ParallelFuture[A] = Future[A] @@ Parallel
/** @template */
type Future[+A] = opacityTypes.Future[A]
}