-
Notifications
You must be signed in to change notification settings - Fork 29
/
task.scala
191 lines (166 loc) · 6.19 KB
/
task.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.thoughtworks.dsl
package domains
import com.thoughtworks.dsl.Dsl.{!!, Continuation, reset}
import com.thoughtworks.dsl.keywords.Shift
import com.thoughtworks.{enableIf, enableMembersIf}
import scala.collection._
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.Builder
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future, Promise, SyncVar}
import scala.util.control.{NonFatal, TailCalls}
import scala.util.{Failure, Success, Try}
import scala.util.control.TailCalls.TailRec
/** @author 杨博 (Yang Bo)
*/
object task {
type TaskDomain = TailRec[Unit] !! Throwable
/** The asynchronous task that supports exception handling, resource management, and is stack-safe.
*
* @template
* @example A [[Task]] can be created from `for`-comprehension,
* where [[keywords.Each]] and [[keywords.Fork]] can be used together to asynchronously iterate collections.
*
* For example, the above `concatenateRemoteData` downloads and concatenates data from multiple URLs.
*
* {{{
* import com.thoughtworks.dsl.comprehension._
* import com.thoughtworks.dsl.keywords._
* import com.thoughtworks.dsl.keywords.Shift._
* import com.thoughtworks.dsl.domains.task.Task
* import java.net.URL
* def concatenateRemoteData(urls: List[URL], downloader: URL => Task[Vector[Byte]]): Task[Vector[Byte]] = {
* for {
* url <- Fork(urls)
* data <- downloader(url)
* byte <- Each(data)
* } yield byte
* }.as[Task[Vector[Byte]]]
* }}}
*
* A [[Task]] can be also created from [[Task.apply]]
*
* {{{
* def mockDownloader(url: URL) = Task {
* "mock data\n".getBytes.toVector
* }
* }}}
*
* A [[Task]] can be then converted to [[scala.concurrent.Future]] via [[Task.toFuture]],
* in order to integrate into other frameworks.
*
* In this example, it's a `Future[Assertion]` required by [[org.scalatest.freespec.AsyncFreeSpec]].
*
* {{{
* val mockUrls = List(new URL("http://example.com/file1"), new URL("http://example.com/file2"))
*
* import org.scalatest.Assertion
* def assertion: Task[Assertion] = Task {
* !concatenateRemoteData(mockUrls, mockDownloader) should be("mock data\nmock data\n".getBytes.toVector)
* }
*
* Task.toFuture(assertion)
* }}}
*/
type Task[+A] = TaskDomain !! A
object Task {
@inline
def now[A](a: A): Task[A] = _(a)
@inline
def delay[A](f: () => A): Task[A] = _(f())
@inline
def apply[A](a: => A): Task[A] @reset = delay(a _)
/** Returns a task that does nothing but let the succeeding tasks run on `executionContext`
*
* @example All the code after a `!switchExecutionContext` should be executed on `executionContext`
* {{{
* import com.thoughtworks.dsl.domains.task.Task
* import org.scalatest.Assertion
* import scala.concurrent.ExecutionContext
* import com.thoughtworks.dsl.keywords.Shift.implicitShift
* def myTask: Task[Assertion] = _ {
* val originalThread = Thread.currentThread
* !Task.switchExecutionContext(ExecutionContext.global)
* Thread.currentThread should not be originalThread
* }
*
* Task.toFuture(myTask)
*
* }}}
*/
@inline
def switchExecutionContext(executionContext: ExecutionContext): Task[Unit] = { continue => failureHandler =>
executionContext.execute(new Runnable {
@inline
private def stackSafeRun(): TailRec[Unit] = {
val protectedContinuation =
try {
continue(())
} catch {
case NonFatal(e) =>
return failureHandler(e)
}
protectedContinuation(failureHandler)
}
def run(): Unit = stackSafeRun().result
})
TailCalls.done(())
}
@enableMembersIf(scala.util.Properties.versionNumberString.matches("""^2\.1(1|2)\..*$"""))
private[task] object Scala211Or212 {
type Factory[-A, +C] = scala.collection.generic.CanBuildFrom[Nothing, A, C]
@inline
def newBuilder[A, C](implicit factory: Factory[A, C]): Builder[A, C] = {
factory()
}
}
@enableMembersIf(scala.util.Properties.versionNumberString.matches("""^2\.13\..*$"""))
private[task] object Scala213 {
@inline
def newBuilder[A, C](implicit factory: Factory[A, C]): Builder[A, C] = {
factory.newBuilder
}
}
import Scala211Or212._
import Scala213._
def join[Element, That](element: Element)(implicit factory: Factory[Element, That]): Task[That] @reset = now {
(newBuilder[Element, That] += element).result()
}
def onComplete[A](task: Task[A])(continue: Try[A] => Unit) = {
Continuation
.toTryContinuation(task) { result =>
TailCalls.done(continue(result))
}
.result
}
@enableIf(c => !c.compilerSettings.exists(_.matches("""^-Xplugin:.*scalajs-compiler_.*\.jar$""")))
def blockingAwait[A](task: Task[A], timeout: Duration = Duration.Inf): A = {
val syncVar = new SyncVar[Try[A]]
Continuation
.toTryContinuation(task) { result =>
syncVar.put(result)
TailCalls.done(())
}
.result
if (timeout.isFinite) {
syncVar.take(timeout.toMillis).get
} else {
syncVar.take.get
}
}
/** Converts a [[Task]] to a [[scala.concurrent.Future]].
*
* @see [[keywords.Await]] for converting a [[scala.concurrent.Future]] to a [[Task]].
*/
def toFuture[A](task: Task[A]): Future[A] = {
val promise = Promise[A]()
Continuation
.toTryContinuation(task) { tryResult =>
promise.complete(tryResult)
TailCalls.done(())
}
.result
promise.future
}
}
}